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)
Best Answer
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: