Bing Maps API V8-map.layers.clear()方法从地图视图中删除多段线

Bing Maps API V8-map.layers.clear()方法从地图视图中删除多段线,maps,bing,bing-maps,Maps,Bing,Bing Maps,不久前,map.layers.clear()方法不会从地图中删除多段线,但现在,在一些Bing更新之后,当调用map.layers.clear()时,将删除多段线。我怎样才能解决这个问题 映射初始化 在这里添加图钉 这里是更新方向回调 调用addPushpins函数 下面是Bing地图方向管理器示例代码 更新-删除部分图钉 // Map initialize var map, pushpins, layer; map = new Microsoft.Maps.Map(document.getE

不久前,map.layers.clear()方法不会从地图中删除多段线,但现在,在一些Bing更新之后,当调用map.layers.clear()时,将删除多段线。我怎样才能解决这个问题

映射初始化

在这里添加图钉

这里是更新方向回调

调用addPushpins函数

下面是Bing地图方向管理器示例代码

更新-删除部分图钉

// Map initialize
var map, pushpins, layer;

map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
    credentials: 'Your Bing Maps Key',
    center: new Microsoft.Maps.Location(47.606209, -122.332071),
    zoom: 12
});

// Here is the Bing Maps Direction Manager sample code
Microsoft.Maps.loadModule('Microsoft.Maps.Directions', function() {
    var directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);

    // Set Route Mode to driving
    directionsManager.setRequestOptions({
        routeMode: Microsoft.Maps.Directions.RouteMode.driving
    });

    var waypoint1 = new Microsoft.Maps.Directions.Waypoint({
        address: 'Redmond',
        location: new Microsoft.Maps.Location(47.67683029174805, -122.1099624633789)
    });
    var waypoint2 = new Microsoft.Maps.Directions.Waypoint({
        address: 'Seattle',
        location: new Microsoft.Maps.Location(47.59977722167969, -122.33458709716797)
    });

    directionsManager.addWaypoint(waypoint1);
    directionsManager.addWaypoint(waypoint2);

    // Callback for on update directions
    Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', onUpdateDirections);

    directionsManager.calculateDirections();
});

// On update directions callback
function onUpdateDirections() {
    clearLayers();

    window.setTimeout(function() {
        addPushpins();
    }, 2000);
}

// Add pushpins
function addPushpins() {
    // Generate an array of 10 random pushpins within current map bounds
    pushpins = Microsoft.Maps.TestDataGenerator.getPushpins(10, map.getBounds());
    layer = new Microsoft.Maps.Layer();
    layer.add(pushpins);
    map.layers.insert(layer);
}

// Clear layers
function clearLayers() {
    // map.layers.clear();
    if (layer !== undefined) {
        var currentPrimitives = layer.getPrimitives();

        /* remove those that are Pushpins */
        for (var i = 0; i < currentPrimitives.length; i++) {
            var entity = currentPrimitives[i];
            if (entity instanceof Microsoft.Maps.Pushpin){
                layer.remove(entity);
            }
        }
    }
}
//映射初始化
var图、图钉、图层;
map=new Microsoft.Maps.map(document.getElementById('myMap'){
凭据:“您的Bing地图密钥”,
中心:新Microsoft.Maps.Location(47.606209,-122.332071),
缩放:12
});
//下面是Bing地图方向管理器示例代码
Microsoft.Maps.loadModule('Microsoft.Maps.Directions',函数(){
var directionsManager=new Microsoft.Maps.Directions.directionsManager(map);
//将路线模式设置为驾驶模式
directionsManager.setRequestOptions({
routeMode:Microsoft.Maps.Directions.routeMode.driving
});
var waypoint1=新的Microsoft.Maps.Directions.Waypoint({
地址:'Redmond',
位置:新Microsoft.Maps.location(47.67683029174805,-122.1099624633789)
});
var waypoint2=新的Microsoft.Maps.Directions.Waypoint({
地址:西雅图,
位置:新的Microsoft.Maps.location(47.59977722167969,-122.33458709716797)
});
directionsManager.addWaypoint(航路点1);
directionsManager.addWaypoint(航路点2);
//关于更新方向的回调
Microsoft.Maps.Events.addHandler(DirectionManager,'DirectionsUpdate',OnUpdate方向);
DirectionManager.calculateDirections();
});
//关于更新方向回调
函数onUpdateDirections(){
clearLayers();
setTimeout(函数(){
添加图钉();
}, 2000);
}
//添加图钉
函数addPushpins(){
//在当前贴图边界内生成一个包含10个随机图钉的数组
pushpins=Microsoft.Maps.TestDataGenerator.getPushpins(10,map.getBounds());
layer=新的Microsoft.Maps.layer();
层。添加(图钉);
map.layers.insert(层);
}
//透明层
函数clearLayers(){
//map.layers.clear();
如果(图层!==未定义){
var currentPrimitives=layer.getPrimitives();
/*拆下这些是图钉*/
对于(var i=0;i
这是意料之中的。在过去,如果它不被清除,将被视为一个bug。如果要清除图层,但保留方向,则有两个选项。在计算方向之前清除图层,或者清除地图中的单个图层而不是所有图层。

这是意料之中的。在过去,如果它不被清除,将被视为一个bug。如果要清除图层,但保留方向,则有两个选项。在计算方向之前清除图层,或者清除地图中的单个图层而不是所有图层。

您可以检查图层,从中获取基本体,并仅移除图钉。如下所示:

var currentPrimitives =  layer.getPrimitives();

/* remove those that are Pushpins */
for (var i = 0; i < currentPrimitives.length; i++) {
    var entity = currentPrimitives[i];
    if (entity instanceof Microsoft.Maps.Pushpin){
        layer.remove(entity);
    }
}
var currentPrimitives=layer.getPrimitives();
/*拆下这些是图钉*/
对于(var i=0;i
您可以检查您的图层,从中获取基本体,然后仅移除管脚。如下所示:

var currentPrimitives =  layer.getPrimitives();

/* remove those that are Pushpins */
for (var i = 0; i < currentPrimitives.length; i++) {
    var entity = currentPrimitives[i];
    if (entity instanceof Microsoft.Maps.Pushpin){
        layer.remove(entity);
    }
}
var currentPrimitives=layer.getPrimitives();
/*拆下这些是图钉*/
对于(var i=0;i
感谢您的回复!我正在测试您的解决方案,但没有移除所有图钉。请参阅更新。感谢您的回复!我正在测试您的解决方案,但没有移除所有图钉。请参阅UPDATE.Ok。谢谢你的回复。但是,当我在管线更新(拖放)时清除图层时,map.layers.clear()会删除多段线。我试图清除图层图钉,但有些图钉仍保留在地图上。请参阅更新。执行此操作有什么问题:
函数clearLayers(){layer.clear();}
此函数没有从地图中删除所有图钉。请在Bing Maps Interactive SDK上测试以查看此行为。您是在谈论路由图钉吗?如果是这样,这些都是路线的一部分。使用directionManager.clearAll()或directionManager.clearDisplay()。这些都记录在这里:感谢再次回复。但我不想谈论路线图钉。我有一条路线,在地图上加了一些图钉。有时我需要清理这些图钉来放置其他图钉,而不需要移除我当前的路线。我有许多其他类型的层,像图钉,热图和集群层。当我通过拖放更改管线时,我需要清除这些元素。好的。谢谢你的回复。但是,当我在管线更新(拖放)时清除图层时,map.layers.clear()会删除多段线。我试图清除图层图钉,但有些图钉仍保留在地图上。请参阅更新。执行此操作有什么问题:
函数clearLayers(){layer.clear();}
此函数没有从地图中删除所有图钉。请在Bing Maps Interactive SDK上测试以查看此行为。您是在谈论路由图钉吗?如果是这样,这些都是路线的一部分。使用directionManager.clearAll()或directionManager.clearDisplay()。这些都记录在这里:感谢再次回复。但我不想谈论路线图钉。我有一条路线,在地图上加了一些图钉。我需要清理那些垃圾
addPushpins();
Microsoft.Maps.loadModule('Microsoft.Maps.Directions', function() {
    var directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);

    // Set Route Mode to driving
    directionsManager.setRequestOptions({
        routeMode: Microsoft.Maps.Directions.RouteMode.driving
    });

    // Callback for on update directions
    Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', onUpdateDirections);

    var waypoint1 = new Microsoft.Maps.Directions.Waypoint({
        address: 'Redmond',
        location: new Microsoft.Maps.Location(47.67683029174805, -122.1099624633789)
    });
    var waypoint2 = new Microsoft.Maps.Directions.Waypoint({
        address: 'Seattle',
        location: new Microsoft.Maps.Location(47.59977722167969, -122.33458709716797)
    });

    directionsManager.addWaypoint(waypoint1);
    directionsManager.addWaypoint(waypoint2);

    // Set the element in which the itinerary will be rendered
    directionsManager.setRenderOptions({
        itineraryContainer: document.getElementById('printoutPanel')
    });

    directionsManager.calculateDirections();
});
// Map initialize
var map, pushpins, layer;

map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
    credentials: 'Your Bing Maps Key',
    center: new Microsoft.Maps.Location(47.606209, -122.332071),
    zoom: 12
});

// Here is the Bing Maps Direction Manager sample code
Microsoft.Maps.loadModule('Microsoft.Maps.Directions', function() {
    var directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);

    // Set Route Mode to driving
    directionsManager.setRequestOptions({
        routeMode: Microsoft.Maps.Directions.RouteMode.driving
    });

    var waypoint1 = new Microsoft.Maps.Directions.Waypoint({
        address: 'Redmond',
        location: new Microsoft.Maps.Location(47.67683029174805, -122.1099624633789)
    });
    var waypoint2 = new Microsoft.Maps.Directions.Waypoint({
        address: 'Seattle',
        location: new Microsoft.Maps.Location(47.59977722167969, -122.33458709716797)
    });

    directionsManager.addWaypoint(waypoint1);
    directionsManager.addWaypoint(waypoint2);

    // Callback for on update directions
    Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', onUpdateDirections);

    directionsManager.calculateDirections();
});

// On update directions callback
function onUpdateDirections() {
    clearLayers();

    window.setTimeout(function() {
        addPushpins();
    }, 2000);
}

// Add pushpins
function addPushpins() {
    // Generate an array of 10 random pushpins within current map bounds
    pushpins = Microsoft.Maps.TestDataGenerator.getPushpins(10, map.getBounds());
    layer = new Microsoft.Maps.Layer();
    layer.add(pushpins);
    map.layers.insert(layer);
}

// Clear layers
function clearLayers() {
    // map.layers.clear();
    if (layer !== undefined) {
        var currentPrimitives = layer.getPrimitives();

        /* remove those that are Pushpins */
        for (var i = 0; i < currentPrimitives.length; i++) {
            var entity = currentPrimitives[i];
            if (entity instanceof Microsoft.Maps.Pushpin){
                layer.remove(entity);
            }
        }
    }
}
var currentPrimitives =  layer.getPrimitives();

/* remove those that are Pushpins */
for (var i = 0; i < currentPrimitives.length; i++) {
    var entity = currentPrimitives[i];
    if (entity instanceof Microsoft.Maps.Pushpin){
        layer.remove(entity);
    }
}