onclick后删除地理编码标记

时间:2011-01-21 07:01:00

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

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

marker.setMap(空);

但我不太确定在哪里放置那段代码

以下是代码:

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)

提交按钮在哪里?

<input type="Submit" value="Submit" onclick="marker.setMap(null);" />

这假设marker.setMap(null);当然有用。