react native 官网介绍了这个 api Geolocation 但是这个 api 只能返回 经纬度 所以要把经纬度 通过逆地理位置转义 http://recode.ditu.aliyun.com/jsdoc/geocode_api.html 可通过这个阿里的开放接口
具体实现
- 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 请求 记得开启 位置访问权限 打印结果如下:
image.png
有点尴尬 好吧 给人家 大碗菜做了广告
来源: http://www.jianshu.com/p/fd2ff4a0f9d7