@@ -6,6 +6,8 @@ import { Location } from "../../types";
66interface MapProps {
77 locations : Location [ ] ;
88 zoom : number ;
9+ focusedLocation ?: { lat : number ; lng : number } | null ;
10+ onMarkerClick ?: ( loc : { lat : number ; lng : number } ) => void ;
911}
1012
1113function toRadians ( degrees : number ) : number {
@@ -82,32 +84,47 @@ function separateOverlappingPoints(points: Location[], offset = 0.0001) {
8284interface InnerMapProps {
8385 locations : Location [ ] ;
8486 center : [ number , number ]
87+ focusedLocation ?: { lat : number ; lng : number } | null ;
88+ onMarkerClick ?: ( loc : { lat : number ; lng : number } ) => void ;
8589}
8690
8791// need inner child component to use useMap hook to run on client
88- function InnerMap ( { locations, center } :InnerMapProps ) {
92+ function InnerMap ( { locations, center, focusedLocation , onMarkerClick } :InnerMapProps ) {
8993 const map = useMap ( ) ;
9094
9195 useEffect ( ( ) => {
92- map . flyTo ( { lat : center [ 0 ] , lng : center [ 1 ] } )
93- } , [ center [ 0 ] , center [ 1 ] ] )
96+ if ( ! map ) return ;
97+ const target = focusedLocation ?? { lat : center [ 0 ] , lng : center [ 1 ] } ;
98+ map . flyTo ( target ) ;
99+ } , [ center [ 0 ] , center [ 1 ] , focusedLocation ?. lat , focusedLocation ?. lng ] ) ;
94100
95101 return (
96102 < >
97103 < TileLayer
98104 // @ts -ignore
99- attribution = '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
100- url = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
105+ attribution = '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>'
106+ url = "https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png"
107+ maxZoom = { 19 }
101108 />
102- { separateOverlappingPoints ( locations ) . map ( ( { lat, lng, color } , i ) => (
103- < Marker key = { i } lat = { lat } lng = { lng } color = { color } />
109+ { separateOverlappingPoints ( locations ) . map ( ( { lat, lng, color, id, start, end, room } , i ) => (
110+ < Marker
111+ key = { i }
112+ lat = { lat }
113+ lng = { lng }
114+ color = { color }
115+ id = { id }
116+ start = { start }
117+ end = { end }
118+ room = { room }
119+ onClick = { onMarkerClick ? ( ) => onMarkerClick ( { lat, lng } ) : undefined }
120+ />
104121 ) ) }
105122 </ >
106123 )
107124
108125}
109126
110- function Map ( { locations, zoom } : MapProps ) {
127+ function Map ( { locations, zoom, focusedLocation , onMarkerClick } : MapProps ) {
111128 const center = getGeographicCenter ( locations ) ;
112129
113130 return (
@@ -119,7 +136,12 @@ function Map({ locations, zoom }: MapProps) {
119136 scrollWheelZoom = { true }
120137 style = { { height : "100%" , width : "100%" } }
121138 >
122- < InnerMap locations = { locations } center = { center } />
139+ < InnerMap
140+ locations = { locations }
141+ center = { center }
142+ focusedLocation = { focusedLocation }
143+ onMarkerClick = { onMarkerClick }
144+ />
123145 </ MapContainer >
124146 ) ;
125147} ;
0 commit comments