Maison > interface Web > js tutoriel > Comment effacer tous les marqueurs de l'API Google Maps v3 ?

Comment effacer tous les marqueurs de l'API Google Maps v3 ?

Susan Sarandon
Libérer: 2024-11-15 11:58:03
original
376 Les gens l'ont consulté

How to Clear All Markers from Google Maps API v3?

Removing All Markers from Google Maps API v3

In Google Maps API v3, clearing all markers is slightly different from the approach used in v2. While map.clearOverlays() is no longer available, the following steps provide an efficient way to achieve the same effect:

1. Declare a Global Marker Array

var markersArray = [];
Copier après la connexion

This array will store references to all markers on the map.

2. Define a Clear Overlays Function

There are two options for defining the clear overlays function:

Option A:

function clearOverlays() {
  for (var i = 0; i < markersArray.length; i++ ) {
    markersArray[i].setMap(null);
  }
  markersArray.length = 0;
}
Copier après la connexion

Option B (Extends Google Maps API):

google.maps.Map.prototype.clearOverlays = function() {
  for (var i = 0; i < markersArray.length; i++ ) {
    markersArray[i].setMap(null);
  }
  markersArray.length = 0;
}
Copier après la connexion

3. Add Markers to the Array

Before calling the clearOverlays() function, push each marker into the markersArray:

markersArray.push(marker);
google.maps.event.addListener(marker,"click",function(){});
Copier après la connexion

4. Call the Clear Overlays Function

Invoke the clearOverlays(); or map.clearOverlays(); function whenever needed to remove all markers from the map. This function iterates through the markersArray, sets the map property of each marker to null, and empties the array.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers articles par auteur
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal