javascript - 如何删除点击时的谷歌地理编码

标签 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);
  }
});

最佳答案

假设您使用的是 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/

相关文章:

javascript - 使用带有数字命名字段的 Angular.js 访问响应数据

javascript - 强制 javascript csv 格式字符串中的值成为文本

javascript - Spring Boot 谷歌地图 SAXParseException

javascript - 谷歌地图 API v3。如何根据日期和时间隐藏 map 上的标记?

javascript - indexOf() 和 search() 有什么区别?

javascript - Angular 应用程序中的 Google MAP API key

php - 如何获取谷歌地图点?

javascript - 防止谷歌地图下载不必要的图像/ Sprite

javascript - google.maps.places.Autocomplete 无法使用 SimpleHTTPServer

javascript - 在从 Node js 中的 mysql 中删除数据值之前,我如何显示警报并确认