Когда мне нужно очистить слой карты в directionUpdated при установке нового маршрута или изменить маршрут путем перетаскивания, функция map.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
});

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

    Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', onUpdateDirections);
});

function onUpdateDirections() {
    map.layers.clear();
    addPushpins();
}

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);
}
0
Edgar Leite 14 Июл 2017 в 21:21

1 ответ

Лучший ответ

Это сделано намеренно. Данные маршрута визуализируются с использованием собственного слоя. Если вы очистите все слои на карте, исчезнут и направления. Для ваших данных используйте слой повторно следующим образом:

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

var layer = new Microsoft.Maps.Layer();
map.layers.insert(layer);

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

    Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', onUpdateDirections);
});

function onUpdateDirections() {
    layer.clear();
    addPushpins();
}

function addPushpins() {
    // Generate an array of 10 random pushpins within current map bounds
    var pushpins = Microsoft.Maps.TestDataGenerator.getPushpins(10, map.getBounds());
    layer.add(pushpins);
}
0
rbrundritt 15 Июл 2017 в 02:39