1
import MarkerClusterer from "@google/markerclusterer";

  const [markerClusterer1, setMarkerClusterer1] = useState<MarkerClusterer>();

  const handleApiLoaded = ({ map, maps }: Mapprops) => {
    mapRef.current = { map, maps };
    createMarker(mapRef.current);
  };

  const createMarker = ({ map, maps }: Mapprops) => {
    if (markerClusterer1) {
      markerClusterer1.clearMarkers();
    }
    const markers = fakedata.map(data => {
      const [lat, lng] = data.coords.coordinates;
      const marker = { lat, lng };
      return new maps.Marker({ position: marker });
    });
    const markerClusterer2 = new MarkerClusterer(map, markers, {
      imagePath: "img/m",
      gridSize: 0,
    });
    setMarkerClusterer1(markerClusterer2);
  };

  useEffect(() => {
    if (mapRef.current) {
       createMarker(mapRef.current);
      }
    }
  }, [mapRef]);

   interface Mapprops {
      map: google.maps.Map;
      maps: {
        Marker: new () => google.maps.Marker;
      };

the clustering is abrupt, with first landing on the page it does not work but works with refresh and zoom in and out. Any help is highly appreciated.

onGoogleApiLoaded= called handleApiLoaded method providing it the map props.

Tanu
  • 1,286
  • 4
  • 16
  • 35
  • Could you please provide a codesandbox or stackblitz so that we can reproduce this issue from our side? – evan Nov 25 '19 at 15:16
  • i am trying to create a sandbox @evan but there are too many functionalities. The issue is that when I copy my url and paste it on a new tab, clustering is not working on the first landing of the page, where as it starts working if I zoom in/zoom out or refresh. Thank you. – Tanu Nov 27 '19 at 17:29
  • @evan can it be that I have initialized the markerCluster incorrectly in the hook ? Thank you. – Tanu Nov 27 '19 at 17:41

0 Answers0