Home    Articles

 

How to reset/remove Markers in google map JavaScript API v3

 

When you need to manage markers on a google map the main ideea is that you have to store all these markers in a javascript array.

As you can see in this js function:

// Adds a marker to the map and push to the array.
function addMarker(location) {
  var marker = new google.maps.Marker({
    position: location,
    map: map
  });
  markers.push(marker);
}

Then each marker has to be set on the map, like this:

// Sets the map on all markers in the array.
function setMapOnAll(map) {
  for (var i = 0; i < markers.length; i++) {
    markers[i].setMap(map);
  }
}

In case you want to delete a marker, all you have to do is to remove that specific marker from the js array and to set them on map again.

Here you can find a complete example on this: Google Example

 

by