Skip to content

Commit

Permalink
Add zoom in on click
Browse files Browse the repository at this point in the history
  • Loading branch information
utas-raymondng committed Jul 2, 2024
1 parent 04ab803 commit 4bcd870
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/components/map/mapbox/layers/ClusterLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,6 @@ const ClusterLayer: FC<ClusterLayerProps> = ({
unclusterPointLayerMouseLeaveEventHandler
);
map?.on("mouseleave", clusterLayer, clusterLayerMouseLeaveEventHandler);

// If user click a cluster layer, zoom into it a bit
map?.on("click", clusterLayer, clusterLayerMouseClickEventHandler);
};

map?.once("load", createLayers);
Expand All @@ -363,7 +360,6 @@ const ClusterLayer: FC<ClusterLayerProps> = ({
unclusterPointLayerMouseLeaveEventHandler
);
map?.off("mouseleave", clusterLayer, clusterLayerMouseLeaveEventHandler);
map?.off("click", clusterLayer, clusterLayerMouseClickEventHandler);

// Clean up resource when you click on the next spatial extents, map is
// still working in this page.
Expand All @@ -388,6 +384,17 @@ const ClusterLayer: FC<ClusterLayerProps> = ({
// where you will add source and remove layer accidentally.
}, [map]);

useEffect(() => {
const layerId = getLayerId(map?.getContainer().id);
const clusterLayer = getclusterLayerId(layerId);

// If user click a cluster layer, zoom into it a bit
map?.on("click", clusterLayer, clusterLayerMouseClickEventHandler);
return () => {
map?.off("click", clusterLayer, clusterLayerMouseClickEventHandler);
};
}, [map, clusterLayerMouseClickEventHandler]);

useEffect(() => {
addSpatialExtentsLayer();

Expand All @@ -410,7 +417,7 @@ const ClusterLayer: FC<ClusterLayerProps> = ({
return () => {
map?.off("styledata", updateSource);
};
}, [updateSource]);
}, [map, updateSource]);

// Setup the event handler
useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/map/mapbox/layers/GeojsonLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const GeojsonLayer: FC<GeojsonLayerProps> = ({ collection, setPhotos }) => {
"fill-outline-color": "black",
},
});
}, [map, stringToColor, extent, collectionId]);
}, [map, extent, collectionId]);

// This is use to handle base map change that set style will default remove all layer, which is
// the behavior of mapbox, this useEffect, add the layer back based on user event
Expand Down

0 comments on commit 4bcd870

Please sign in to comment.