-1
我在Google地圖上顯示路線,一切正常。問題是當我更新其中一個標記(出發或到達)時,新標記將被添加到地圖中,而不是刪除前一個標記。我試圖將directionsDisplay設置爲全局變量,但無法解決問題。在顯示新路線之前有沒有辦法清除地圖?更改路線時顯示多個標記的地圖
這裏是它的樣子:
我也試過這對API的建議:
directionDisplay = new google.maps.DirectionsRenderer();
directionDisplay.suppressMarkers = true;
directionDisplay.setMap(null);![enter image description here][1]
感謝您的幫助
我的代碼是
var map = drawMap(map, mapOptions);
var departure;
var arrival;
var marker = createMarker();
function mainMap(mapOptions) {
departure1 = '#autocomplete_departure';
arrival1 = '#autocomplete_arrival';
var placeDeparture = getAddress(departure1);
var placeArrival = getAddress(arrival1);
google.maps.event.addListener(placeDeparture, 'place_changed', function(){
placeMarker(placeDeparture, departure1);
});
google.maps.event.addListener(placeArrival, 'place_changed', function(){
placeMarker(placeArrival, arrival1);
});
};
function setMarkerPosition(marker, place){
marker.setPosition(place.geometry.location);
marker.setMap(map);
}
function drawMap(){
return new google.maps.Map(map, mapOptions);
}
function createMarker(){
var markerOptions = {
map: map
}
return new google.maps.Marker(markerOptions);
}
function placeMarker(place, stop){
var place = place.getPlace();
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17);
}
marker.setPosition(place.geometry.location);
marker.setMap(map);
if (stop == '#autocomplete_departure') {
departure = stop;
} else if (stop == '#autocomplete_arrival') {
arrival = stop;
}
if (arrival !== undefined && departure !== undefined) {
updateRouteOnMap(arrival, departure);
}
function updateRouteOnMap(departure, arrival){
var from = $(departure).val();
var to = $(arrival).val();
var request = {
origin: from,
destination: to,
travelMode: google.maps.TravelMode.DRIVING
};
var directionsDisplay;
if (directionsDisplay != undefined) directionsDisplay.setMap(null);
directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
$("#directions-panel").html()
directionsDisplay.setPanel(document.getElementById('directions-panel'));
}
else{
alert("there has been an error. Please try again!")
}
});
請提供[最小,完整,測試和可讀示例](http://stackoverflow.com/help/mcve)。在覆蓋之前,您需要從地圖上隱藏/刪除「標記」變量。 – geocodezip 2014-11-03 20:47:49
嗨,謝謝你的回答,這是我的第一篇文章,所以下次會試着多加註意。我把地圖的整個代碼放在地圖上,因爲我試圖在詢問但是沒有得到任何結果之前去除不同部分的市場,所以我對如何進行操作有點困惑。感謝您的幫助 – user3623363 2014-11-04 20:35:51
我已經清除了相關位的代碼。我試過在調用DirectionsRenderer()之前使用函數刪除標記,但沒有做任何結果。任何建議?謝謝 – user3623363 2014-11-07 10:20:02