golf_nut
golf_nut

Reputation: 431

removing geocode marker after onclick

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? I asked earlier and somebody suggested using:

marker.setMap(null);

but I'm not too sure where to place that bit of code

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

Thanks in advance

Upvotes: 1

Views: 1459

Answers (1)

Julio Santos
Julio Santos

Reputation: 3895

Where is the submit button?

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

This is assuming marker.setMap(null); works, of course.

Upvotes: 1

Related Questions