Javascript 未能清除Google Map API 3中方向服务的路由
当我单击按钮通过清除标记和方向路线来重置地图时,我不确定用户是否已经创建了路线。如果在单击事件期间包含以下代码,我的功能将失效。但如果这三个代码被删除,它就能正常工作Javascript 未能清除Google Map API 3中方向服务的路由,javascript,html,google-maps-api-3,google-maps-markers,google-directions-api,Javascript,Html,Google Maps Api 3,Google Maps Markers,Google Directions Api,当我单击按钮通过清除标记和方向路线来重置地图时,我不确定用户是否已经创建了路线。如果在单击事件期间包含以下代码,我的功能将失效。但如果这三个代码被删除,它就能正常工作 directionsDisplay.setMap(null); directionsDisplay.setPanel(null); directionsDisplay.setDirections({routes: []}); 在执行上述代码之前,是否需要确定directionsDisplay是否为空?我的代码摘录如下供您参考。。
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;
}
采用以下代码可解决此问题
if(directionsDisplay != null) {
directionsDisplay.setMap(null);
directionsDisplay = null; }
采用以下代码可解决此问题
if(directionsDisplay != null) {
directionsDisplay.setMap(null);
directionsDisplay = null; }
为了标记和语法突出显示,您应该使用您使用的编程语言标记您的问题。当你在它修复你的代码缩进。非常感谢你的建议。。希望这个问题可以解决…现在的方向是什么?这在你的代码中是未定义的,就像更新一样。谢谢。。。currentDirections是在html的开头定义的,我只是在上面的编码中再次添加了它。。UpdateLatLng函数是另一个与目录服务无关的函数。。。此项目中的所有其他功能都正常工作,除了我在单击地图时无法删除路线。可能与方向服务无关,但如果没有它,您的代码将失败…您应该使用您使用的编程语言标记您的问题,以便标记和语法突出显示。当你在它修复你的代码缩进。非常感谢你的建议。。希望这个问题可以解决…现在的方向是什么?这在你的代码中是未定义的,就像更新一样。谢谢。。。currentDirections是在html的开头定义的,我只是在上面的编码中再次添加了它。。UpdateLatLng函数是另一个与目录服务无关的函数。。。此项目中的所有其他功能都正常工作,除了单击地图时无法删除路线。可能与方向服务无关,但如果没有它,您的代码将失败。。。