Skip to content

Commit

Permalink
fix: on new permission correctly geolocate
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto Milla Martinez committed Nov 7, 2024
1 parent 06df445 commit 1c96f14
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/components/map/GeolocationMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function GeoLocationMap({
onPermissionStatusChanged,
zoom = 13,
}: GeoLocationMapProps) {
const isGeolocating = useRef(false);
const mapContainerRef = useRef<HTMLDivElement | null>(null);
const mapRef = useRef<maplibregl.Map | null>(null);

Expand All @@ -30,9 +31,19 @@ export default function GeoLocationMap({
showAccuracyCircle: true,
});

const triggerGeoLocate = () => {
setTimeout(() => {
if (!isGeolocating.current) {
if (geolocateControl.trigger()) {
isGeolocating.current = true;
}
}
});
};

const visibilityChangeEvent = () => {
if (document.visibilityState === 'visible' && geolocateControl) {
geolocateControl.trigger();
triggerGeoLocate();
}
};

Expand Down Expand Up @@ -80,9 +91,7 @@ export default function GeoLocationMap({
onPermissionStatusChanged(permissionStatus.state);
}

if (permissionStatus.state === 'granted') {
geolocateControl.trigger();
}
triggerGeoLocate();
};
});

Expand Down Expand Up @@ -123,7 +132,7 @@ export default function GeoLocationMap({
mapRef.current.addControl(geolocateControl);

// add to js queue so that the control is correctly added, then trigger the location detection
setTimeout(() => geolocateControl.trigger(), 200);
setTimeout(() => triggerGeoLocate(), 200);
return cleanup;
}, [zoom]);

Expand Down

0 comments on commit 1c96f14

Please sign in to comment.