javascript - PermissionsAndroid 未显示在 React Native 应用程序中

标签 javascript android google-maps react-native permissions

所以我更改了地理位置,我现在使用 'react-native-geolocation-service' 中的地理位置。所以我现在的问题是不确定我的 PermissionsAndroid 代码是否正确,因为没有显示位置权限对话框。每次我按下按钮时,console.log 中都会显示“未授予位置权限”,因此这意味着未授予权限。正如我所说,没有显示“权限”对话框。

我希望你们检查我的代码是否有错误。 这些是我的代码:

hasLocationPermission = () => {
try{
  const granted = PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
    {
      title: 'Location Permission',
      message: 'You must to accept this to make it work.'
    }
  )
  if(granted === PermissionsAndroid.RESULTS.GRANTED){
    console.log('Location permission accepted.')
  }else{
    console.log("Location permission denied")
  }
} catch (err) {
  console.warn(err)
}
}

getUserLocationHandler = () => {
if(this.hasLocationPermission){
  Geolocation.getCurrentPosition(
    position => {
      this.setState({
        userLocation: {
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
          latitudeDelta: 0.0622,
          longitudeDelta: 0.0421,
        },
      });
    },
    err => console.log(err),
    {
      enableHighAccuracy: true,
      timeout: 15000,
      maximumAge: 10000

    }
  );
}
}

 render() {
return (
  <View style={styles.container}>
    <FetchLocation onGetLocation={this.getUserLocationHandler}/>
    <UsersMap userLocation={this.state.userLocation}/>
  </View>
);
}

"FetchLocation" is the Button and the "UsersMap" is the map.

If you guys know how to show up the Permission Dialog before the app open like the Grab app, that would be better. Thank you!

If you want to see something else in my codes or in my build.gradles, feel free to ask. Thank you!

最佳答案

您是否在 AndroidManifest 文件中添加了权限?就像documentation说。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

关于javascript - PermissionsAndroid 未显示在 React Native 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52848830/

相关文章:

android - 如何在方向更改后重新创建传递给 DialogFragment 的监听器?

android - 我可以从服务内的共享首选项中获取数据吗?

android - 如何在Android.Equalizer中获取音频文件的频段级别

javascript - 使用 Google map MarkerCluster for api v3 时如何围绕制造商创建圆圈

android - 在谷歌地图上叠加图像 android api v2

java - 如何从服务器端的客户端计算到给定点的最近坐标

javascript - 使用 JavaScript 从数组中删除零值

javascript - Google Maps API v3 travelMode 作为变量

javascript - 在 HTML Canvas 元素上绘制文本的最佳方法是什么?

javascript - 在 Android 网页中禁用缩放输入焦点