如何 fixPermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION 未授予 React Native 位置权限

How to fixPermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION Location Permission Not Granted React Native

我做的react native应用使用geolocation,但是定位权限不允许弹出

我在 Android Manifest 上添加了权限并尝试使用该库,但结果是一样的

  async componentDidMount() {

    if (Platform.OS === 'android') {

      await request_device_location_runtime_permission()

    }

    this.getLongLat = navigator.geolocation.watchPosition(
      (position) => {
        this.setState({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
          error: null,
        })
      },
      (error) => this.setState({ error: error.message }),
      { enableHighAccuracy: true, timeout: 2000, maximumAge: 100, distanceFilter: 10 },
    )

  }


我希望权限位置可以激活,并且可以显示当前位置的经纬度

您可以使用expo-permissions

你可以运行expo install expo-permissions

您应该获得以下两项权限:

ACCESS_FINE_LOCATION: 'android.permission.ACCESS_FINE_LOCATION' ACCESS_COARSE_LOCATION: 'android.permission.ACCESS_COARSE_LOCATION'

例子

import * as Permissions from 'expo-permissions';

    const permission = await Permissions.getAsync(Permissions.LOCATION);
    if (permission.status !== "granted") {
      const newPermission = await Permissions.askAsync(Permissions.LOCATION);

      if (newPermission.status === "granted") {
          console.log("Permissions granted");
       }else {
         console.log("newPermission not granted");
      }

}else{
 console.log("Permissions granted");
}