1

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

您可以在此链接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);
            }
        });
    }
});
4

1 回答 1

1

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

directionsDisplay = new google.maps.DirectionsRenderer();
于 2013-08-01T18:58:43.693 回答