0

當我點擊一個按鈕通過清除標記和方向路線來重置地圖時,我不確定用戶是否已經創建了路線。如果在Click事件中包含以下代碼,我的功能將會失效。但它工作正常,如果這三個代碼被刪除...無法清除Google Map API中定向服務的路線3

directionsDisplay.setMap(null); 
directionsDisplay.setPanel(null); 
directionsDisplay.setDirections({routes: []}); 

是否有必要確定directionsDisplay是否爲空或不執行上述代碼之前?我的編碼是提取下面爲您出謀劃策......非常感謝......

var directionsDisplay; 
var directionsService = new google.maps.DirectionsService(); 
var StartMarker = null; 
var EndMarker = null; 
var directionsVisible = false; 
var oldDirections = []; 
var currentDirections = null; 

google.maps.event.addListener(map, 'click', function(event) { 
    if (!StartMarker) { 
     origin = event.latLng; 

     lat = origin.lat(); 
     lng = origin.lng(); 
     /*If the origin is null, run UpdateLatLng() function to convert 
      the Lat, Lng of the mouse click position to Northing, Easting and 
      assign the value to the textbox */ 
     UpdateLatLng(); 

     var startimage = 'images/Start4.png'; 
     StartMarker = new google.maps.Marker({ 
      map: map, 
      position: origin, 
      icon: startimage 
     }); 
    } else { 
     //Relocate the Starting point and assign the new position to Textbox 
     alert ("The starting point was relocated on screen"); 
     StartMarker.setMap(null); 
     if (EndMarker !==null) { 
      EndMarker.setMap(null); 
     }; 
     directionsDisplay.setMap(null); 
     directionsDisplay.setPanel(null); 
     directionsDisplay.setDirections({routes: []}); 
     var origin = event.latLng; 
     lat = origin.lat(); 
     lng = origin.lng(); 
     UpdateLatLng(); 
     //StartMarker.setPosition(origin.getposition()); 
     var startimage = 'images/Start4.png'; 
     StartMarker = new google.maps.Marker({ 
      map: map, 
      position: origin, 
      icon: startimage 
     }); 
    } 
}); 

google.maps.event.addListener(directionsDisplay, 'directions_changed', 
     function() { 
      if (currentDirections) { 
       oldDirections.push(currentDirections); 
       setUndoDisabled(false); 
      } 
      currentDirections = directionsDisplay.getDirections(); 
      calcRoute(); 
     }); 
function calcRoute() { 
    if (origin == null) { 
     alert("Please input the starting point"); 
     return; 
    } 

    var mode; 
    switch (document.getElementById("mode").value) { 
     case "driving": 
      mode = google.maps.DirectionsTravelMode.DRIVING; 
      break; 
     case "walking": 
      mode = google.maps.DirectionsTravelMode.WALKING; 
      break; 
     case "transit": 
      mode = google.maps.DirectionsTravelMode.TRANSIT; 
      break; 
    } 

    var request = { 
     origin: origin, 
     destination: destination, 
     waypoints: waypoints, 
     travelMode: mode, 
     optimizeWaypoints: document.getElementById('optimize').checked, 
     avoidHighways: document.getElementById('highways').checked, 
     avoidTolls: document.getElementById('tolls').checked 
    }; 

    directionsService.route(request, function(response, status) { 
     if (status == google.maps.DirectionsStatus.OK) { 
      directionsDisplay.setDirections(response); 

     } 
    }); 

    directionsVisible = true; 
} 
+0

你應該標記與你的問題您使用的編程語言,無論是爲了標記和語法高亮。當你在它修復你的代碼縮進。 – timss 2013-05-12 16:41:24

+0

非常感謝您的建議..希望問題能夠得到解決... – 2013-05-12 16:47:47

+0

什麼是currentDirections?在你的代碼中這是未定義的,就像UpdateLatLng一樣。 – geocodezip 2013-05-12 16:49:16

回答

3

問題是採用以下代碼固定...

if(directionsDisplay != null) { 
    directionsDisplay.setMap(null); 
    directionsDisplay = null; } 
0
directionsDisplay.setMap(null); 
map.setZoom(8); 
map.setCenter(); 
相關問題