Determining if geolocation enabled with react native

笑着哭i 提交于 2019-12-23 07:12:32

问题


Building an app with RN I use the following to receive user's location :

 navigator.geolocation.getCurrentPosition(
  (position) => {
   //do stuff with location
  },
  (error) => {
    //error or locaiton not allowed
  },
  {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);

Which immediately invokes the inquiry for user to accept geolocation permissions. What I'm trying to achieve now is to have an explicit screen before prompting user to click on a button to accept those permissions. In order to get there, I need some way to check if he has accepted those permissions before or not. Caching it in storage would be a bad solution - it might go out of sync if user changes permissions in settings.

How can I check if user has accepted geolocation permissions without triggering the permission alert?


回答1:


You can use this library https://github.com/yonahforst/react-native-permissions to check if user has accepted location permission. Like this:

Permissions.getPermissionStatus('location')
  .then(response => {
    this.setState({ locationPermission: response })
  })


来源:https://stackoverflow.com/questions/38371987/determining-if-geolocation-enabled-with-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!