I've tried looking at a large variety of code blocks here and on the Google Maps API documentation but STILL have not been able to figure out how to hide markers.
This is the current code that I am using, and it worked for one instance. Once I added the "for" loop in the function with markers.setMap(null) Firefox displays the following error:
Error: TypeError: markers.setMap is not a function
function removeMarkers(){
var markers;
alert(markers);
alert(locations.length);
for (i = 0; i<locations.length; i++){
markers = locations[i];
alert(markers.title);
markers.setMap(null);
}
}
Additional Information: Campus Map and full code (excluding Map API)
You need to keep an array of the
google.maps.Marker
objects to hide (or remove or run other operations on them).In the global scope:
Then push the markers on that array as you create them:
Then to remove them:
working example (toggles the markers)
code snippet:
You can try this
Hope it works.
According to Google documentation they said that this is the best way to do it. First create this function to find out how many markers there are/
Next create another function to take away all these markers
Then create this final function to erase all the markers when ever this function is called upon.
}
Hope that helped good luck