gpt4 book ai didi

javascript - 如何从 map 上删除旧路线?

转载 作者:行者123 更新时间:2023-11-28 02:04:17 24 4
gpt4 key购买 nike

那里。我在 map 上有一个标记(Google Maps APIV3),每次更改其位置时,我都会计算一条到 map 上最近点的新路线。这工作正常,但每次我更改标记的位置时, map 仍然保留旧路线。我尝试了一切,但没有任何效果。如何从 map 中删除旧路线?

您可以在此链接 http://mercurio.cafw.ufsm.br/~grupo1/ 上查看问题如果移动蓝色标记,它就可以正常工作。但如果你再次移动它,旧路线仍然存在。

这是代码!

var mapOptions = {
center: new google.maps.LatLng(-27.357246,-53.396022),
zoom: 14,
maxZoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById('map_canvas'),mapOptions);
var marcador = new google.maps.LatLng(-27.355379,-53.397773);
var seuMarcador = new google.maps.Marker({
position: marcador,
map: map,
icon: "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png",
title:"Mova essa marcador para seu endereço.",
draggable: true
});

google.maps.event.addListener(seuMarcador, 'mouseup', function(event) {
var addr = new Array(5);
addr[0] = new google.maps.LatLng(-27.352646,-53.384881);
addr[1] = new google.maps.LatLng(-27.344648,-53.395009);
addr[2] = new google.maps.LatLng(-27.365562,-53.388859);
addr[3] = new google.maps.LatLng(-27.366241,-53.401655);
addr[4] = new google.maps.LatLng(-27.360467,-53.397476);

//var a = new google.maps.LatLng(-27.357837,-53.395661);
var a = event.latLng;
var menorDistancia;
var destinoFinal;
var directionsDisplay;
directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);


function calcRoute(inicio,fim) {
var start = inicio;
var end = fim;
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
//I BELIVE THAT THE PROBLEM IS HERE
var directionsService = new google.maps.DirectionsService();
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}
});

最佳答案

不要为每次调用创建一个新的 DirectionsRenderer,而是重复使用同一个。从事件监听器中删除它:

directionsDisplay = new google.maps.DirectionsRenderer();

关于javascript - 如何从 map 上删除旧路线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18001585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com