3

我的项目以前在 Expo SDK 40 中,但我必须将其升级到 SDK 42。我的应用程序有一个组件,用户可能希望在地图上查看他们当前的位置,但我不断收到错误消息。在初始屏幕中授予权限,但该位置不会转移到新地图。下面是我的一些代码:

//此代码在用户进入地图之前正确地请求用户访问他们当前位置的权限

useFocusEffect( 
      React.useCallback(()=> {
        let isActive = true;

        async function getLocationAsync() {

          const { status } = await Location.requestForegroundPermissionsAsync()
              if (status !== 'granted'){
                  setErrorMsg('Permission to access location was denied')
                  return;      
                } 
                let location = await Location.getCurrentPositionAsync({});
                setLocation(location);
    }

//这是更新前的代码,请求并在 Expo SDK 40 中显示它们的位置

useFocusEffect( 
      React.useCallback(()=> {
        let isActive = true;
        
          const fetchGeoPosition = () => { 
            navigator.geolocation.getCurrentPosition(
              position => { 
                if (isActive){
                  setUserLatitude(position.coords.latitude);
                  setUserLongitude(position.coords.longitude);
                  setPositionError(null);
                  console.log('Location Accessed')
                }
                setIsLoading(false)
              }, 

              error => isActive && setPositionError(error.message),
              {enableHighAccuracy: true, timeout: 20000, maximumAge: 2000}
            ); 
              }
...
    fetchGeoPosition()

//位置如何在地图上呈现

<MapView
       provider= {PROVIDER_GOOGLE}
       style={styles.map}
             region={{
                     latitude: user_latitude,
                     longitude: user_longitude,
                     latitudeDelta: 0.015,
                     longitudeDelta: 0.0121
                    }}
 >

//这是我更新当前位置代码的尝试,但还是告诉我之前的方法被贬值了

 useFocusEffect( 
      React.useCallback(()=> {
        let isActive = true;
        
          async function fetchGeoPosition() { 
            const {status} = await Location.requestForegroundAsync()
              if (status !== 'granted'){
                setErrorMsg('Permission to access location was denied')
                return;
              }
              let location = await Location.getForegroundPositionAsync({})
              setLocation(location)
              navigator.geolocation.getForegroundPosition(
              position => { 
                if (isActive){
                  setUserLatitude(position.coords.latitude);
                  setUserLongitude(position.coords.longitude);
                  setPositionError(null);
                  console.log('Location Accessed')
                }
                setIsLoading(false)
              }, 
              error => isActive && setPositionError(error.message),
              {enableHighAccuracy: true, timeout: 20000, maximumAge: 2000}
              )
     
              }
...
fetchGeoPosition()

<MapView
           provider= {PROVIDER_GOOGLE}
           style={styles.map}
                 region={{
                         latitude: user_latitude,
                         longitude: user_longitude,
                         latitudeDelta: 0.015,
                         longitudeDelta: 0.0121
                        }}
     >
4

0 回答 0