最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

react native 获取地理位置的方法示例

来源:动视网 责编:小采 时间:2020-11-27 22:08:57
文档

react native 获取地理位置的方法示例

react native 获取地理位置的方法示例:react native 官网介绍了这个 api Geolocation 但是这个api只能返回 经纬度 所以要把经纬度 通过逆地理位置转义 http://recode.ditu.aliyun.com/jsdoc/geocode_api.html 可通过这个阿里的开放接口 在 react native 中,我们所用的是rea
推荐度:
导读react native 获取地理位置的方法示例:react native 官网介绍了这个 api Geolocation 但是这个api只能返回 经纬度 所以要把经纬度 通过逆地理位置转义 http://recode.ditu.aliyun.com/jsdoc/geocode_api.html 可通过这个阿里的开放接口 在 react native 中,我们所用的是rea


react native 官网介绍了这个 api Geolocation 但是这个api只能返回 经纬度 所以要把经纬度 通过逆地理位置转义 http://recode.ditu.aliyun.com/jsdoc/geocode_api.html 可通过这个阿里的开放接口

在 react native 中,我们所用的是react native 自带的api定位功能,无需引入第三方js。

react native 定位是通过Geolocation这个模块来实现的。想了解更多关于Geolocation的知识请点击下面 Geolocation自行了解,这里我们主要将他的几个方法。

static getCurrentPosition(geo_success, geo_error?, geo_options?)
Invokes the success callback once with the latest location info. Supported options: timeout (ms), maximumAge (ms), enableHighAccuracy (bool) On Android, this can return almost immediately if the location is cached or request an update, which might take a while.

static watchPosition(success, error?, options?)
Invokes the success callback whenever the location changes. Supported options: timeout (ms), maximumAge (ms), enableHighAccuracy (bool), distanceFilter(m)

static clearWatch(watchID)

第一个方法是获取第一次定位时的位置信息,第一个为成功时的回掉函数,还有error时的回掉,第三个是传状态的。
在请求成功函数中有以下属性:

  • 经度 : coords.longitude
  • 纬度 : coords.latitude
  • 准确度 : coords.accuracy
  • 海拔 : coords.altitude
  • 海拔准确度 : coords.altitudeAcuracy
  • 行进方向 : coords.heading
  • 地面速度 : coords.speed
  • 时间戳 : new Date(position.timestamp)
  • 在请求失败函数中有4种情况(err.code状态值):

    1为用户拒绝定位请问
    2暂时获取不到位置信息
    3为请求超时
    4未知错误

    第三个options是可选参数,属性如下:

    enableHighAccuracy——指示浏览器获取高精度的位置,默认为false。当开启后,可能没有任何影响,也可能使浏览器花费更长的时间获取更精确的位置数据。

    timeout——指定获取地理位置的超时时间,默认不限时。单位为毫秒。

    maximumAge——最长有效期,在重复获取地理位置时,此参数指定多久再次获取位置。默认为0,表示浏览器需要立刻重新计算位置。

    static watchPosition(success, error?, options?) 

    是多次改变了位置信息时才会触发,一般触发的可能性可能用户多次刷新数据,如一个人行车到其他城市,这时如果设置一个监听函数,只要watchid不一样,就会不断的触发

    由于可能会出现缓存的情况,所以Geolocation 为我们提供了一个可以清除缓存的方法watchPosition(),改方法是 用于上一次的定位信息进行清除的。

    对了,要启动react native 的定位功能的话,如果你是android 用户,你需要先在AndroidManifest.xml中加入以下权限

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

    具体实现

    import Geolocation from 'Geolocation';
    
     ......
    
     getlocal() {
     Geolocation.getCurrentPosition(
     val => {
     let ValInfo =
     '速度:' +
     val.coords.speed +
     '\n经度:' +
     val.coords.longitude +
     '\n纬度:' +
     val.coords.latitude +
     '\n准确度:' +
     val.coords.accuracy +
     '\n行进方向:' +
     val.coords.heading +
     '\n海拔:' +
     val.coords.altitude +
     '\n海拔准确度:' +
     val.coords.altitudeAccuracy +
     '\n时间戳:' +
     val.timestamp;
     this.setState({ LocalPosition: ValInfo });
     console.log("打印地理位置:"+`${val.coords.longitude},${val.coords.latitude}`)
     GET_GPRS({
     "l":`${val.coords.latitude},${val.coords.longitude}`,
     "type":111,
     }).then(res => {
     console.log(JSON.stringify(res))
     })
     },
     val => {
     let ValInfo = '获取坐标失败:' + val;
     this.setState({ LocalPosition: ValInfo }); //如果为空的话 没允许开启定位服务
    
     },
     );
     }
    
    

    这里的 GET_GPRS 是自己封装的 fech请求

    记得开启 位置访问权限

    打印结果如下:

    文档

    react native 获取地理位置的方法示例

    react native 获取地理位置的方法示例:react native 官网介绍了这个 api Geolocation 但是这个api只能返回 经纬度 所以要把经纬度 通过逆地理位置转义 http://recode.ditu.aliyun.com/jsdoc/geocode_api.html 可通过这个阿里的开放接口 在 react native 中,我们所用的是rea
    推荐度:
    标签: 位置 地址 方法
    • 热门焦点

    最新推荐

    猜你喜欢

    热门推荐

    专题
    Top