如何在点击时删除谷歌地理编码

时间:2011-01-21 01:01:26

标签: javascript google-maps google-maps-api-3

我正在使用谷歌地图 - 方向和地理编码。页面加载时,地理编码会放置标记。当用户点击提交按钮时,是否可以删除此标记?

以下是代码:

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 个答案:

答案 0 :(得分:1)

假设您正在使用v3 api,请尝试:

marker.setMap(null);

这会将其从地图中删除。

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