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

时间:2017-06-26 17:33:53

标签: maps bing bing-maps

前段时间, map.layers.clear()方法不会从地图中删除折线,但是现在,在一些Bing更新后,当地图时删除折线调用.layers.clear()。我该如何解决这个问题?

  

地图初始化

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

这里添加图钉

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

这是更新方向回调

// On update directions callback
function onUpdateDirections() {
    map.layers.clear();
}
  

调用addPushpins函数

addPushpins();
  

这是Bing Maps Direction Manager示例代码

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);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

这是可以预料的。过去没有清算会被认为是一个错误。如果要清除图层,但保留说明,则有两个选项。在计算方向之前清除图层,或清除单个图层而不是地图中的所有图层。

答案 1 :(得分:1)

您可以检查您的图层,从中获取图元并仅删除图钉。如下所示:

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);
    }
}