Skip to content

Commit 183b6a2

Browse files
committed
feat: Navigate to map on click; show popups
1 parent 663f142 commit 183b6a2

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

package-lock.json

Lines changed: 30 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/components/PropertyMap.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import React, {
88
SetStateAction,
99
} from "react";
1010
import mapboxgl, { Map as MapboxMap, PointLike } from "mapbox-gl";
11+
import { Polygon } from 'geojson';
1112
import { mapboxAccessToken } from "../../config/config";
1213
import { useFilter } from "@/context/FilterContext";
1314
import LegendControl from "mapboxgl-legend";
@@ -241,11 +242,36 @@ const PropertyMap: React.FC<PropertyMapProps> = ({
241242
layers: ["vacant_properties"],
242243
});
243244
const mapItem = features.find(feature => feature.properties?.OPA_ID === id);
244-
console.log({ mapItem });
245-
// TODO: Focus the map on the given lat/long.
245+
246+
if (mapItem != null) {
247+
const coordinates = (mapItem.geometry as Polygon).coordinates[0];
248+
// Perform a simple average of the polygon coordinates, consider a more complicated algorithm
249+
const totalPoint = coordinates.reduce(
250+
(prevSum, position) => [prevSum[0] + position[0], prevSum[1] + position[1]],
251+
[0, 0],
252+
);
253+
const averagePoint = [totalPoint[0] / coordinates.length, totalPoint[1] / coordinates.length] as [number, number];
254+
map.flyTo({
255+
center: averagePoint,
256+
});
257+
setPopupInfo({
258+
longitude: averagePoint[0],
259+
latitude: averagePoint[1],
260+
feature: selectedProperty?.properties,
261+
});
262+
}
246263
}
247264
}, [id]);
248265

266+
useEffect(
267+
() => {
268+
if (id == null) {
269+
setPopupInfo(null);
270+
}
271+
},
272+
[id]
273+
);
274+
249275
const changeCursor = (e: any, cursorType: "pointer" | "default") => {
250276
e.target.getCanvas().style.cursor = cursorType;
251277
};

0 commit comments

Comments
 (0)