如何获取当前用户位置错误对象(评估导航器地理定位获取 CurrentPosition)

How can I get Current user location error object (evaluating navigator geolocation get CurrentPosition)

当前用户位置进入本机反应

您可以使用以下代码片段访问 Expo 的位置。

import * as Location from "expo-location";


export const hasLocationServiceEnable = async () =>
  await Location.hasServicesEnabledAsync();

const getUserLocation = async () => {


  let result = null;

  const isLocationAccessEnabled = await hasLocationServiceEnable();

  /** Discard request coordinates when user location disabled. This required to avoid
   * continuously request location in contrary to the user preference
   */



  if (!isLocationAccessEnabled) {

    return result;
  } else {

    // Request location access permission 
    let { status } = await Location.requestForegroundPermissionsAsync();

  

    if (status !== "granted") {
  
      return result;
    } else {
      try {
        let { coords } = await Location.getCurrentPositionAsync({});

       
        result = coords || null;
      } catch (error) {

        console.error(error)
       
      }

      return result;
    }
  }
};

export default getUserLocation;