2011-01-21 2 views
1

Я использую google maps-направления и геокодирование. Геокодирование помещает маркер при загрузке страницы. Можно ли удалить этот маркер, когда пользователь нажимает кнопку отправки?как удалить геокод google при щелчке

Вот код:

var address = document.getElementById("address").textContent.replace(/\n/g, " "); 
geocoder.geocode({ 'address': address}, 
function(results, status) 
{ 
    if (status == google.maps.GeocoderStatus.OK) { 
    map.setCenter(results[0].geometry.location); 
    var marker = new google.maps.Marker({ 
     map: map, 
     position: results[0].geometry.location 
    }); 
    } 
    else 
    { 
    alert("Geocode was not successful for the following reason: " + status); 
    } 
}); 




var from = document.getElementById("from").value; 
var to = document.getElementById("to").value; 
var request = { 
    origin: from, 
    destination: to, 
    travelMode: google.maps.DirectionsTravelMode.DRIVING 
}; 
document.getElementById("map").style.width = "70%"; 
directionsService.route(request, function(response, status) { 
    if (status == google.maps.DirectionsStatus.OK) { 
    directionsDisplay.setDirections(response); 
    } 
}); 

ответ

1

Предполагая, что вы используете v3 API, попробуйте:

marker.setMap(null); 

Это удалит его с карты.

http://code.google.com/apis/maps/documentation/javascript/reference.html#Marker

+0

где бы я разместил этот код? Я попробовал: var marker = new google.maps.Marker (null); –

+0

Когда вы сначала создаете свой маркер, сохраните его в глобальной переменной «marker», поэтому, когда нажата кнопка отправки, вы вызываете «маркер» .setMap (null); –

Смежные вопросы