react native定位时请求开启位置权限 - Go语言中文社区

react native定位时请求开启位置权限


react native 在使用 Geolocation 定位获取经纬度的时候w,如果用户不开启位置信息,不打开gps,则会报错,所以一开始应该判断用户有没有开启位置信息,如果没有的话请求用户打开位置信息
我在android react-native-android-location-services-dialog-box这个组件
React Native Android Location Services Dialog Box
https://github.com/webyonet/react-native-android-location-services-dialog-box

配置:
1.yarn add react-native-android-location-services-dialog-box

2.react-native link react-native-android-location-services-dialog-box

3.android/settings.gradle

include ':react-native-android-location-services-dialog-box'
project(':react-native-android-location-services-dialog-box').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-android-location-services-dialog-box/android')

4.android/app/build.gradle

dependencies {
   ...
   compile project(':react-native-android-location-services-dialog-box')
}

项目中使用

import LocationServicesDialogBox from "react-native-android-location-services-dialog-box"

如果要获取经纬度的话还需要引入Geolocation

import Geolocation from 'Geolocation'
 componentDidMount() {
        this.getLocation()
    }
  getLocation(){
          LocationServicesDialogBox.checkLocationServicesIsEnabled({
            message: "<h2>开启位置服务</h2>开启位置服务,获取精准定位<br/><a href='#'>了解更多</a>",
            ok: "去开启",
            cancel: "取消",
            enableHighAccuracy: true, 
            showDialog: true, 
            openLocationServices: true, 
            preventOutSideTouch: false, 
            preventBackClick: false, 
            providerListener: true 
        }).then(function(success) {
         
                Geolocation.getCurrentPosition((location) => {
                    let coordinate = [location.coords.longitude,location.coords.latitude]
                    this.props.saveLocation(coordinate);
                    this.setState({
                        currentLocation:coordinate
                    })
              });
            }.bind(this)
        ).catch((error) => {
            console.log(error.message);
        });
        DeviceEventEmitter.addListener('locationProviderStatusChange', function(status) { 
            console.log(status); 
        });
     }
 componentWillUnmount() {
        LocationServicesDialogBox.stopListener(); 
   } 

在这里插入图片描述

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/wyw223/article/details/86309352
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-04-11 23:32:48
  • 阅读 ( 911 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢