2013-08-01 28 views
1

。 我在地圖上有一個標記(Google Maps APIV3),每次我改變它的位置時,我都會計算一條到達地圖上最近點的新路線。 而且工作正常,但每次我改變Marker的位置時,地圖仍然保持舊路線。 我嘗試了一切,沒有任何工作。如何從地圖中刪除舊的路線?如何從地圖中刪除舊的路線?那裏有

你可以在這個鏈接上看到問題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); 
      } 
     }); 
    } 
}); 
+0

請:1.刪除不相關的代碼(distanceMatrix與此問題無關)。 2.添加所有相關的代碼(在代碼段中沒有定義的地圖,但它被使用)。 3.一個jsfiddle或指向展示該問題的現場版本的鏈接將非常有用。 – geocodezip

回答

1

不要爲每個調用創建一個新的DirectionsRenderer,重複使用同一個。從你的事件監聽器中刪除:

directionsDisplay = new google.maps.DirectionsRenderer(); 
+0

非常感謝你,它工作。 – ViniBiso

+0

如果它解決了您的問題,請[接受答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – geocodezip

+0

它也適用於我。 – DS9