的塞納里奧清除多種途徑:無法從谷歌地圖
我有一個谷歌地圖,其功能之一是要表明路由從一箇中心點信息在同一地圖上的幾個不同勢的位置。 我知道每個方向對象只能在一次顯示一組路線,所以我有一個函數可以在每次調用時創建一個渲染對象,並將路線放置在地圖上併爲每個位置調用它。
的代碼:
計算並顯示所述路線的功能:
function calculateRoot (startLocation,wayPoints,endLocation) {
var selectedMode = $("#travelMode").val();
// create a new directions service object to handle directions requests
var directionsService = new google.maps.DirectionsService();
// create a directions display to display route info on the map
var directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);
// Stops the default googlemarkers from showing
directionsDisplay.suppressMarkers = true;
directionsDisplay.setPanel(document.getElementById("directions"));
// create a request object
var request = {
origin:startLocation,
waypoints: wayPoints,
destination:endLocation,
travelMode: google.maps.TravelMode[selectedMode],
optimizeWaypoints:true,
provideRouteAlternatives:true,
transitOptions: {
departureTime: new Date()
}
};
directionsService.route(request, function(result,status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
return directionsDisplay;
}
else if (status == google.maps.DirectionsStatus.ZERO_RESULTS){
alert ('No routing can be found for this Journey');
return -1;
}
else {
alert ('The following error occoured while attempting to obtain directions information:'+'\n'+status + '\n' + 'For:'+ ' '+ request.destination);
return -1;
}
});
}
的所有位置功能:
function showAllRoutes(){
if (! directionsArray.length < 1) {
// if directions are already displayed clear the route info
clearRoots();
}
$('#directions').empty();
// craete an empty waypoint array just to pass to the function
var wayPoints = [];
for (var i = 0; i< markerArray.length; i++) {
var directions = calculateRoot(startLatLng,wayPoints,markerArray[i].position);
directionsArray.push(directions);
}
sizeMap();
$('#directions').show();
}
以清除路由功能(s)
function clearRoutes() {
if (directionsArray.length <1) {
alert ("No directions have been set to clear");
return;
}
else {
$('#directions').hide();
for (var i = 0;i< directionsArray.length; i ++) {
if (directionsArray [i] !== -1) {
directionsArray [i].setMap(null);
}
}
directionsArray.clear();
map.setZoom(5);
return;
}
}
問題:
雖然生成和顯示的路線似乎做工精細,無論我做什麼,我不能從地圖上清除路線,除非我刷新。 我是否錯過了一些簡單的東西,或者我不需要這樣做,我一直試圖讓這個工作超過一天,而且我很難過。 任何人都可以幫忙嗎? 在此先感謝
你有功能clearRoutes(),但你打電話clearRoots(); – Marcelo
對不起,這只是我的部分錯別字它不在實際的代碼 – user1079178
只是注意到,當我調用clearRoutes函數與對象在對象上的對象在routesArray的未定義方向Arrayray [i] .setMap (空值); – user1079178