how to remove google geocode on click

南楼画角 提交于 2020-01-05 09:36:04

问题


I am using google maps- directions and geocoding. The geocode places a marker when the page loads. Is it possible to have this marker removed when a user clicks the submit button?

Here is the code:

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:


Assuming you're using the v3 api, try:

marker.setMap(null);

That will remove it from the map.

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



来源:https://stackoverflow.com/questions/4754407/how-to-remove-google-geocode-on-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!