Skip to content

Commit

Permalink
[feat] added clustering
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-tam33 committed Oct 26, 2024
1 parent 221d6ec commit 2e43030
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
13 changes: 12 additions & 1 deletion api/maps/AddMarkers.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { useState } from 'react';
import { useMemo, useState } from 'react';
import { MarkerClusterer } from '@googlemaps/markerclusterer';
import { useMap } from '@vis.gl/react-google-maps';
import ProjectModal from '@/components/ProjectModal';
import { Project } from '../../types/schema';
import { MarkerInfoWindow } from './MarkerInfoWindow';
Expand All @@ -20,6 +22,14 @@ export default function AddMarker({
setSelectedProjectId(null); // close modal
};

const map = useMap();

const clusterer = useMemo(() => {
if (!map) return null;

return new MarkerClusterer({ map });
}, [map]);

return (
<>
{projects?.map((project: Project) => {
Expand All @@ -34,6 +44,7 @@ export default function AddMarker({
projectDev={project.developer}
projectId={project.id}
onMarkerClick={handleMarkerClick}
clusterer={clusterer}
/>
);
})}
Expand Down
26 changes: 19 additions & 7 deletions api/maps/MarkerInfoWindow.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useCallback, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { MarkerClusterer } from '@googlemaps/markerclusterer';
import {
AdvancedMarker,
InfoWindow,
Expand All @@ -11,33 +12,44 @@ export const MarkerInfoWindow = ({
projectName,
projectDev,
onMarkerClick,
clusterer,
}: {
position: { lat: number; lng: number };
projectId: number;
projectName: string;
projectDev: string;
onMarkerClick: (projectId: number) => void;
clusterer: MarkerClusterer | null;
}) => {
const [markerRef, marker] = useAdvancedMarkerRef();
const [infoWindowShown, setInfoWindowShown] = useState(false);

// clicking the marker will toggle the infowindow
const handleMarkerEnter = useCallback(
() => setInfoWindowShown(isShown => !isShown),
[],
);
// hovering over the marker will toggle the infowindow
const handleMarkerEnter = useCallback(() => {
setInfoWindowShown(isShown => !isShown);
}, []);

// if the maps api closes the infowindow, we have to synchronize our state
const handleClose = useCallback(() => setInfoWindowShown(false), []);

const handleMarkerClick = () => {
onMarkerClick(projectId);
};

useEffect(() => {
if (marker && clusterer) {
clusterer.addMarker(marker);
}
});

return (
<>
<AdvancedMarker
ref={markerRef}
position={position}
onMouseEnter={handleMarkerEnter}
onMouseLeave={handleClose}
onClick={() => onMarkerClick(projectId)}
onClick={handleMarkerClick}
/>

{infoWindowShown && (
Expand Down
5 changes: 3 additions & 2 deletions components/Map/map.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { APIProvider, Map as GoogleMap } from '@vis.gl/react-google-maps';
import addMarkers from '../../api/maps/AddMarkers';
import AddMarkers from '../../api/maps/AddMarkers';
import { Project } from '../../types/schema';
import './styles.css';

Expand All @@ -28,7 +28,8 @@ export default function Map(props: { projects: Project[] | null }) {
disableDefaultUI={true}
mapId={mapId}
>
{addMarkers({ projects: props.projects })}
<AddMarkers projects={props.projects} />
{/* // add clustered markers here */}
</GoogleMap>
</APIProvider>
);
Expand Down

0 comments on commit 2e43030

Please sign in to comment.