diff --git a/src/components/MapView/MapView.tsx b/src/components/MapView/MapView.tsx index 7b3d73479dde..f7a15ba10d2c 100644 --- a/src/components/MapView/MapView.tsx +++ b/src/components/MapView/MapView.tsx @@ -29,6 +29,7 @@ const MapView = forwardRef( const [isIdle, setIsIdle] = useState(false); const [currentPosition, setCurrentPosition] = useState(cachedUserLocation); const [userInteractedWithMap, setUserInteractedWithMap] = useState(false); + const hasAskedForLocationPermission = useRef(false); useFocusEffect( useCallback(() => { @@ -36,6 +37,11 @@ const MapView = forwardRef( return; } + if (hasAskedForLocationPermission.current) { + return; + } + + hasAskedForLocationPermission.current = true; getCurrentPosition( (params) => { const currentCoords = {longitude: params.coords.longitude, latitude: params.coords.latitude}; diff --git a/src/components/MapView/MapView.website.tsx b/src/components/MapView/MapView.website.tsx index 778ef66449d4..82909001fefd 100644 --- a/src/components/MapView/MapView.website.tsx +++ b/src/components/MapView/MapView.website.tsx @@ -5,7 +5,7 @@ import {useFocusEffect} from '@react-navigation/native'; import mapboxgl from 'mapbox-gl'; import 'mapbox-gl/dist/mapbox-gl.css'; -import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useState} from 'react'; +import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState} from 'react'; import Map, {MapRef, Marker} from 'react-map-gl'; import {View} from 'react-native'; import {withOnyx} from 'react-native-onyx'; @@ -52,6 +52,7 @@ const MapView = forwardRef( const [userInteractedWithMap, setUserInteractedWithMap] = useState(false); const [shouldResetBoundaries, setShouldResetBoundaries] = useState(false); const setRef = useCallback((newRef: MapRef | null) => setMapRef(newRef), []); + const hasAskedForLocationPermission = useRef(false); useFocusEffect( useCallback(() => { @@ -59,6 +60,11 @@ const MapView = forwardRef( return; } + if (hasAskedForLocationPermission.current) { + return; + } + + hasAskedForLocationPermission.current = true; getCurrentPosition( (params) => { const currentCoords = {longitude: params.coords.longitude, latitude: params.coords.latitude}; @@ -66,14 +72,15 @@ const MapView = forwardRef( setUserLocation(currentCoords); }, () => { - if (cachedUserLocation) { + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + if (cachedUserLocation || !initialState) { return; } setCurrentPosition({longitude: initialState.location[0], latitude: initialState.location[1]}); }, ); - }, [cachedUserLocation, isOffline, initialState.location]), + }, [cachedUserLocation, initialState, isOffline]), ); // Determines if map can be panned to user's detected