@@ -13,6 +13,7 @@ import { Location } from "../../types";
1313interface MapProps {
1414 locations : Location [ ] ;
1515 zoom : number ;
16+ viewKey ?: string ;
1617}
1718
1819function toRadians ( degrees : number ) : number {
@@ -87,11 +88,12 @@ function separateOverlappingPoints(points: Location[], offset = 0.0001) {
8788 return adjustedPoints ;
8889}
8990
90- function Map ( { locations, zoom } : MapProps ) {
91+ function Map ( { locations, zoom, viewKey } : MapProps ) {
9192 const mapRef = useRef < MapRef | null > ( null ) ;
9293 const mapboxToken = process . env . NEXT_PUBLIC_MAPBOX_ACCESS_TOKEN ;
9394 const mapboxStyleId = process . env . NEXT_PUBLIC_MAPBOX_STYLE_ID || "mapbox/streets-v12" ;
9495 const [ cursor , setCursor ] = useState < string > ( "" ) ;
96+ const [ autoCenter , setAutoCenter ] = useState ( true ) ;
9597 const [ selected , setSelected ] = useState < {
9698 longitude : number ;
9799 latitude : number ;
@@ -152,14 +154,20 @@ function Map({ locations, zoom }: MapProps) {
152154 return `${ hours12 } :${ minutes . toString ( ) . padStart ( 2 , "0" ) } ${ period } ` ;
153155 } ;
154156
157+ // When the day/view changes, re-enable auto-centering.
155158 useEffect ( ( ) => {
156- if ( ! mapRef . current ) return ;
159+ setAutoCenter ( true ) ;
160+ setSelected ( null ) ;
161+ } , [ viewKey ] ) ;
162+
163+ useEffect ( ( ) => {
164+ if ( ! mapRef . current || ! autoCenter ) return ;
157165 mapRef . current . flyTo ( {
158166 center : [ center [ 1 ] , center [ 0 ] ] ,
159167 zoom,
160168 essential : true ,
161169 } ) ;
162- } , [ center , zoom ] ) ;
170+ } , [ autoCenter , center , zoom ] ) ;
163171
164172 if ( ! mapboxToken ) {
165173 return (
@@ -200,6 +208,10 @@ function Map({ locations, zoom }: MapProps) {
200208 maxPitch = { 70 }
201209 interactiveLayerIds = { [ "pcp-course-markers" ] }
202210 cursor = { cursor }
211+ onDragStart = { ( ) => setAutoCenter ( false ) }
212+ onZoomStart = { ( ) => setAutoCenter ( false ) }
213+ onRotateStart = { ( ) => setAutoCenter ( false ) }
214+ onPitchStart = { ( ) => setAutoCenter ( false ) }
203215 onMouseMove = { ( e ) => {
204216 const hovering = ( e . features ?. length || 0 ) > 0 ;
205217 setCursor ( hovering ? "pointer" : "" ) ;
@@ -212,6 +224,14 @@ function Map({ locations, zoom }: MapProps) {
212224 }
213225 const [ lng , lat ] = f . geometry . coordinates as [ number , number ] ;
214226 const props = ( f . properties || { } ) as Record < string , unknown > ;
227+
228+ // Center the map on the clicked marker (keep current zoom).
229+ setAutoCenter ( false ) ;
230+ mapRef . current ?. flyTo ( {
231+ center : [ lng , lat ] ,
232+ essential : true ,
233+ } ) ;
234+
215235 setSelected ( {
216236 longitude : lng ,
217237 latitude : lat ,
0 commit comments