我正在使用谷歌地图 - 方向和地理编码。加载页面时,地理编码会放置一个标记。当用户单击提交按钮时是否可以删除此标记?
这是代码:
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);
}
});
最佳答案
假设您使用的是 v3 api,请尝试:
marker.setMap(null);
这会将其从 map 上删除。
http://code.google.com/apis/maps/documentation/javascript/reference.html#Marker
关于javascript - 如何删除点击时的谷歌地理编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4754407/