当我加载多条路线时,不会删除上一条路线。
许多人说要将directionsDisplay.setMap
设置为null
,但这并不起作用。
看起来路线在地图上而不在directionsDisplay
上。
这是我的代码不起作用:
NgMap.getMap('mapShop').then(function(map) {
$scope.googleMaps = map;
var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();
directionsDisplay.setMap(null);
directionsDisplay.setDirections(null);
directionsDisplay.setMap($scope.googleMaps);
// get geolocation for center map and marker
NavigatorGeolocation.getCurrentPosition().then(function(position) {
$scope.latitude = $routeParams.latitude;
$scope.longitude = $routeParams.longitude;
$scope.urlLocalCard = "http://maps.apple.com/?daddr=" + $routeParams.latitude + "," + $routeParams.longitude + "&saddr=" + position.coords.latitude + "," + position.coords.longitude + "&ll=" + $routeParams.latitude + "," + $routeParams.longitude + "&z=10&t=s";
var request = {
origin: position.coords.latitude + ',' + position.coords.longitude,
destination: $routeParams.latitude + ',' + $routeParams.longitude,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
} else {
alert('Google route unsuccesfull!');
}
});
});
答案 0 :(得分:1)
因为您通过将后一个变量分配给directionsDisplay
的新实例而失去对旧google.maps.DirectionsRenderer
的引用。将directionsDisplay
和directionsService
移至全局范围。
var directionsDisplay,
directionsService;
// Update global reference upon Google map loads
function initMap(){
directionsDisplay = new google.maps.DirectionsRenderer();
directionsService = new google.maps.DirectionsService();
}
NgMap.getMap('mapShop').then(function(map) {
$scope.googleMaps = map;
directionsDisplay.setMap(null);
directionsDisplay.setDirections(null);
// rest of code
});