Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

オープン
#627 コメント 1 件 リアクション 2 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
30/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
react, typescript
領域
frontend

調査の方向性

まず、Map コンポーネント、reuseMaps={true}、および @googlemaps/markerclusterer を使用するマーカークラスタリングの例で説明されているナビゲーションフローを再現します。都市ビュー間のナビゲーション中にクラスター マーカーがクリーンアップされる過程を追跡し、前の都市のマーカーが残る理由を特定します。都市を変更しても、以前のビューのクラスター マーカーがキャッシュされたマップに残らなくなれば完了です。

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
TypeScript
スター
1.9k
フォーク
195
平均マージ
2日 18時間
マージ済み PR(30日)
15

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

visgl/react-google-maps のほかの issue

visgl/react-google-maps の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。