@@ -102,7 +102,8 @@ export default function MapWrapper({
102
102
const { mapContext, setMapContext } = useContext ( MapContext ) ;
103
103
104
104
// Refs
105
- const isInitialMount = useRef ( true ) ;
105
+ const isInitialMountLocation = useRef ( true ) ;
106
+ const isInitialMountRoute = useRef ( true ) ;
106
107
const mapElement = useRef ( ) ;
107
108
const mapRef = useRef ( ) ;
108
109
const popup = useRef ( ) ;
@@ -410,20 +411,23 @@ export default function MapWrapper({
410
411
resetHoveredStates ( null ) ;
411
412
} ) ;
412
413
413
- if ( ! camera ) {
414
- // if there is no parameter for shifting the view, pan to my location
415
- toggleMyLocation ( ) ;
416
- }
417
-
418
- loadData ( ) ;
414
+ loadData ( true ) ;
419
415
} ) ;
420
416
421
417
useEffect ( ( ) => {
422
418
if ( searchLocationFrom && searchLocationFrom . length ) {
423
- if ( locationPinRef . current ) {
424
- mapRef . current . removeOverlay ( locationPinRef . current ) ;
419
+ if ( isInitialMountLocation . current ) {
420
+ isInitialMountLocation . current = false ;
421
+
422
+ } else {
423
+ // Don't center/remove existing overlay on first load
424
+ if ( locationPinRef . current ) {
425
+ mapRef . current . removeOverlay ( locationPinRef . current ) ;
426
+ }
427
+
428
+ centerMap ( searchLocationFrom [ 0 ] . geometry . coordinates ) ;
425
429
}
426
- centerMap ( searchLocationFrom [ 0 ] . geometry . coordinates ) ;
430
+
427
431
setLocationPin (
428
432
searchLocationFrom [ 0 ] . geometry . coordinates ,
429
433
blueLocationMarkup ,
@@ -434,8 +438,8 @@ export default function MapWrapper({
434
438
} , [ searchLocationFrom ] ) ;
435
439
436
440
useEffect ( ( ) => {
437
- if ( isInitialMount . current ) { // Do nothing on first load
438
- isInitialMount . current = false ;
441
+ if ( isInitialMountRoute . current ) { // Do nothing on first load
442
+ isInitialMountRoute . current = false ;
439
443
return ;
440
444
}
441
445
@@ -444,7 +448,7 @@ export default function MapWrapper({
444
448
mapRef . current . removeLayer ( mapLayers . current [ 'routeLayer' ] ) ;
445
449
}
446
450
447
- loadData ( ) ;
451
+ loadData ( false ) ;
448
452
} , [ selectedRoute ] ) ;
449
453
450
454
useEffect ( ( ) => {
@@ -533,7 +537,7 @@ export default function MapWrapper({
533
537
// }
534
538
}
535
539
536
- const loadData = ( ) => {
540
+ const loadData = ( isInitialMount ) => {
537
541
if ( selectedRoute && selectedRoute . routeFound ) {
538
542
const routeLayer = getRouteLayer ( selectedRoute , mapRef . current . getView ( ) . getProjection ( ) . getCode ( ) ) ;
539
543
mapLayers . current [ 'routeLayer' ] = routeLayer ;
@@ -544,8 +548,10 @@ export default function MapWrapper({
544
548
loadEvents ( selectedRoute ) ;
545
549
loadFerries ( ) ;
546
550
547
- // Zoom/pan to route
548
- fitMap ( selectedRoute . route , mapView ) ;
551
+ // Zoom/pan to route on route updates
552
+ if ( ! isInitialMount ) {
553
+ fitMap ( selectedRoute . route , mapView ) ;
554
+ }
549
555
550
556
} else {
551
557
// Clear and update data
0 commit comments