@@ -8,6 +8,7 @@ import React, {
8
8
SetStateAction ,
9
9
} from "react" ;
10
10
import mapboxgl , { Map as MapboxMap , PointLike } from "mapbox-gl" ;
11
+ import { Polygon } from 'geojson' ;
11
12
import { mapboxAccessToken } from "../../config/config" ;
12
13
import { useFilter } from "@/context/FilterContext" ;
13
14
import LegendControl from "mapboxgl-legend" ;
@@ -241,11 +242,36 @@ const PropertyMap: React.FC<PropertyMapProps> = ({
241
242
layers : [ "vacant_properties" ] ,
242
243
} ) ;
243
244
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
+ }
246
263
}
247
264
} , [ id ] ) ;
248
265
266
+ useEffect (
267
+ ( ) => {
268
+ if ( id == null ) {
269
+ setPopupInfo ( null ) ;
270
+ }
271
+ } ,
272
+ [ id ]
273
+ ) ;
274
+
249
275
const changeCursor = ( e : any , cursorType : "pointer" | "default" ) => {
250
276
e . target . getCanvas ( ) . style . cursor = cursorType ;
251
277
} ;
0 commit comments