Home >Web Front-end >JS Tutorial >How Do You Remove Markers from a Google Maps API v3 Map?
Removing Markers in Google Maps API v3: A Comprehensive Guide
Unlike its predecessor, Google Maps API v3 lacks a clear method for removing all markers from the map. This article will explore an effective approach to achieve this goal.
Solution:
The key to clearing markers in v3 lies in initializing a global array called "markersArray" to store all the markers as they are created. A custom function named "clearOverlays()" can then be defined to iterate through this array and set the map property of each marker to "null," effectively removing them from the map.
Here's the detailed implementation:
Declare a Global Array:
var markersArray = [];
Define the "clearOverlays()" Function:
function clearOverlays() { for (var i = 0; i < markersArray.length; i++ ) { markersArray[i].setMap(null); } markersArray.length = 0; }
Push Markers into the "markersArray":
Before calling the "clearOverlays()" function, push all the markers into the "markersArray." Use the following code for each marker:
markersArray.push(marker); google.maps.event.addListener(marker,"click",function(){});
The above is the detailed content of How Do You Remove Markers from a Google Maps API v3 Map?. For more information, please follow other related articles on the PHP Chinese website!