In Google Maps API v2, if I wanted to remove all the map markers, I could simply do:
map.clearOverlays();
How do I do this in Google Maps A
The solution is quite easy. You may use the method: marker.setMap(map);
. Here, you define on which map the pin will appear.
So, if you set null
in this method (marker.setMap(null);
), the pin will disappear.
Now, you can write a function witch while make disappear all markers in your map.
You just add to put your pins in an array and declare them with markers.push (your_new pin)
or this code for example:
// 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);
}
This is a function witch can set or disappear all the markers of your array in the map:
// Sets the map on all markers in the array.
function setMapOnAll(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
To disappear all your markers, you should call the function with null
:
// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
setMapOnAll(null);
}
And, to remove and disappear, all your markers, you should reset your array of markers like this:
// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
clearMarkers();
markers = [];
}
This is my complete code. It’s the simplest I could reduce to.
Be care full you may replace YOUR_API_KEY
in the code by your key google API:
Remove Markers
Click on the map to add markers.
You may consult google developer or the complete documentation on, also, google developer web site.