下面是我用来从给定点生成路线的函数。我有几个地方可以让用户请求方向......如果用户第二次请求方向,那么第一条路线仍然显示在地图上,显然这看起来不太好。我怎样才能删除它?
base.getRoute = function(start, la, lo) {
var directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(base.map);
directionsDisplay.setPanel(document.getElementById("drivingDirectionsPanel"));
start+=",uk";
var end = new google.maps.LatLng(la, lo);
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response,status) {
if(status == google.maps.DirectionsStatus.OK) {
$('#drivingDirectionsPanel').html('');
directionsDisplay.setDirections(response);
$('html, body').animate({
scrollTop: $('#drivingDirectionsPanel').offset().top
}, 1500);
} else {
alert("There was a problem finding directions");
}
});
};
我试过添加一个
directionsDisplay.setMap(null);
但这不起作用。
有什么想法吗?谢谢