I want to remove all markers on map when the map is clicked.
I am following the documentation here:
I've included the function:
function clearMarkers() {
setMapOnAll(null);
}
to be triggered by a click event, but I just get the error:
Uncaught ReferenceError: setMapOnAll is not defined
There is no other information in the documentation that can help me.
Can anyone point me in the right direction?
I want to remove all markers on map when the map is clicked.
I am following the documentation here: https://developers.google./maps/documentation/javascript/examples/marker-remove
I've included the function:
function clearMarkers() {
setMapOnAll(null);
}
to be triggered by a click event, but I just get the error:
Uncaught ReferenceError: setMapOnAll is not defined
There is no other information in the documentation that can help me.
Can anyone point me in the right direction?
Share Improve this question asked Nov 10, 2015 at 1:39 MeltingDogMeltingDog 15.6k52 gold badges178 silver badges322 bronze badges2 Answers
Reset to default 3The error is indicating that setMapOnAll does not exist in the scope
// Sets the map on all markers in the array.
function setMapOnAll(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
Responding to your last question, you do not really need to add the markers in an array, but be aware that in the documentation example, they add the word var before adding it in the array,
var marker = new google.maps.Marker({
position: location,
map: map
});
markers.push(marker); ...
if you only want to work with just one marker then, do not add the var word
marker = new google.maps.Marker({...
Then use marker.setMap(null);
to get it out off the map.