Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

[Bug] Map instance caching has buggy behavior when used with clustering

Aperta
#627 1 commento 2 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
30/100
Tipo di issue
Bug
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
react, typescript
Ambito
frontend

Direzione di ricerca

Inizia riproducendo il flusso di navigazione descritto con il componente Map, reuseMaps={true} e l'esempio di clustering dei marker che utilizza @googlemaps/markerclusterer. Traccia la pulizia dei marker dei cluster durante la navigazione tra le viste delle città e determina perché i marker della città precedente rimangono. Il lavoro è completato quando il cambio di città non lascia più marker dei cluster delle viste precedenti sulla mappa memorizzata nella cache.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug
Description

I'm implementing an application that has a view /cities which allows you to view all cities on a google map. You can then navigate to view a specific cities at /cities/[id]. Each city then has markers for EV chargers in the city. The EV charger markers are clustered.

If I enable map instance caching via reuseMaps={true} I run into issues where cluster markers remain on the map as you navigate from one city to another. It's not obvious as you have to zoom out in order to see cluster markers from multiple cities.

I'm using @googlemaps/markerclusterer and the implementation follows the marker-clustering example in this repo.

I'm storing the cluster markers in an array stored in useRef in an attempt to be able to remove them when the component unmounts.

    return new MarkerClusterer({
      map,
      renderer: {
        render: (cluster, stats, map) => {
          const markerElem = renderClusterMarker({
            cluster,
            map,
            mapContentType,
          });
          // cluster markers are added to the array here
          clusterMarkerElems.current.push(markerElem);
          return markerElem;
        },
      },
    });

Then on unmount I'm attempting to remove the cluster markers:

  useEffect(() => {
    return () => {
      // This does not throw an error, but it does not seem to work 
      clusterMarkerElems.current.forEach((clusterMarkerElem) => {
        clusterMarkerElem.setMap(null);
      });
      setMarkers({});
      // markerClusterer is always null
      if (markerClusterer) {
        markerClusterer?.clearMarkers();
      }
    };
  }, []);

The code in the cleanup function does not throw an error, but it also doesn't work. I have a guess that the <Map> component is already removed by the time the cleanup function runs. If I run markerClusterer.clearMarkers() when the view loads it doesn't remove the stranded markers as I don't think it knows they exist.

Steps to Reproduce

See description.

Environment
  • Library version: 1.4.2
  • Google maps version: weekly
  • Browser and Version: Firefox latest
  • OS: macOS
Logs

No response

Lingua principale
TypeScript
Stelle
1.9k
Fork
195
Merge medio
2g 11h
PR unite (30g)
14

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di visgl/react-google-maps

Tutte le issue di visgl/react-google-maps

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.