React Native 获取地理位置

实现原理:

1、用  navigator.geolocation.getCurrentPosition 获取到坐标信息

2、调用 高德地图 接口,解析位置数据

本文所用RN 版本为 0.57.8

实现代码如下:

import React, {Component} from 'react';
import {StyleSheet, Text, View} from 'react-native'; export default class TestGeo extends Component { state = {
longitude: '',//经度
latitude: '',//纬度
city: '',
district: '',
street: '',
position: '',//位置名称
}; componentWillMount = () => {
this.getPositions();
}; getPositions = () => {
return new Promise(() => {
/** 获取当前位置信息 */
navigator.geolocation.getCurrentPosition(
location => {
this.setState({
longitude: location.coords.longitude,//经度
latitude: location.coords.latitude,//纬度
});
//通过调用高德地图逆地理接口,传入经纬度获取位置信息
fetch(`http://restapi.amap.com/v3/geocode/regeo?key=97c933e33025b3843b40016900074704&location=${this.state.longitude},${this.state.latitude}&radius=1000&extensions=all&batch=false&roadlevel=0`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: ``
})
.then((response) => response.json())
.then((jsonData) => {
// console.log(jsonData)
try {
this.setState({
city: jsonData.regeocode.addressComponent.city,
district: jsonData.regeocode.addressComponent.district,
street: jsonData.regeocode.addressComponent.township,
position: jsonData.regeocode.formatted_address,
});
} catch (e) { }
})
.catch((error) => {
console.error(error);
});
},
error => {
console.error(error);
}
); })
} render() {
return (
<View style={styles.container}>
<Text style={styles.instructions}>经度:{this.state.longitude}</Text>
<Text style={styles.instructions}>纬度:{this.state.latitude}</Text>
<Text style={styles.instructions}>城市:{this.state.city}</Text>
<Text style={styles.instructions}>区域:{this.state.district}</Text>
<Text style={styles.instructions}>街道:{this.state.street}</Text>
<Text style={styles.instructions}>详细位置:{this.state.position}</Text>
</View>
);
}
} const styles = StyleSheet.create({
container: {
flex: ,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: ,
},
});

附:

高德 接口文档:

https://lbs.amap.com/api/webservice/summary

本博客地址: wukong1688

本文原文地址:https://www.cnblogs.com/wukong1688/p/11062409.html

转载请著名出处!谢谢~~

[RN] React Native 获取地理位置的更多相关文章

  1. [RN] React Native 获取验证码 按钮

    React Native 获取验证码 按钮 效果如图: 实现方法: 一.获取验证码 按钮组件 封装 CountDownButton.js "use strict"; import ...

  2. [RN] React Native 幻灯片效果 Banner

    [RN] React Native 幻灯片效果 Banner 1.定义Banner import React, {Component} from 'react'; import {Image, Scr ...

  3. [RN] React Native 实现 类似QQ 登陆页面

    [RN] React Native 实现 类似QQ 登陆页面 一.主页index.js 项目目录下index.js /** * @format */ import {AppRegistry} from ...

  4. [RN] React Native 实现图片预览

    [RN] React Native 实现图片预览 效果预览: 代码如下: 'use strict'; import React, {Component} from 'react'; import {I ...

  5. [RN] React Native 常见基本问题归纳总结

    [RN] React Native  常见基本问题归纳总结 本问题总结涉及到版本为: "react": "16.8.3","react-native& ...

  6. [RN] React Native 关闭所有黄色警告

    [RN] React Native 关闭所有黄色警告 console.ignoredYellowBox = ['Warning: BackAndroid is deprecated. Please u ...

  7. [RN] React Native 下实现底部标签(支持滑动切换)

    上一篇文章 [RN] React Native 下实现底部标签(不支持滑动切换) 总结了不支持滑动切换的方法,此篇文章总结出 支持滑动 的方法 准备工作之类的,跟上文类似,大家可点击上文查看相关内容. ...

  8. [RN] React Native 常用命令行

    [RN] React Native 常用命令行 1.查看当前版本 react-native --version 或 react-native -v 2.创建指定版本的React Native项目 1) ...

  9. [RN] React Native 让 Flatlist 支持 选中多个值,并获取所选择的值

    React Native 让 Flatlist  支持  选中多个值,并获取所选择的值 实现效果如下: 实现代码: import React, {Component} from 'react'; im ...

随机推荐

  1. 学习《Linux网络安全技术与实现》,正文为1、2章的笔记,后面的等待更新

    博客地址:http://www.cnblogs.com/zengjianrong/p/3280276.html

  2. 微信小程序起步

    微信小程序 文档 微信小程序开发文档 本质 so微信小程序到底是什么?是原生的app还是H5应用? 简单来说,小程序是一种应用,运行的环境是微信(App)进程中,使用了部分的H5技术 目录介绍 app ...

  3. sublime-text-3-build-3207 破解+注册码

    按照以下步骤依次进行 打开 https://hexed.it/ 单机 "Open file" 并选择 "sublime_text.exe" 可执行文件. 转到 ...

  4. 【转帖】分布式事务之解决方案(XA和2PC)

    分布式事务之解决方案(XA和2PC) https://zhuanlan.zhihu.com/p/93459200 ​ 博彦信息技术有限公司 java工程师 3. 分布式事务解决方案之2PC(两阶段提交 ...

  5. Spring Boot 运行原理 - 核心注解

    https://www.jianshu.com/p/23f504713b94 核心注解 打开上面任意一个AutoConfiguration文件,一般都有下面的条件注解,在spring-boot-aut ...

  6. EWS:邮箱的一个开放的接口服务

    https://3gstudent.github.io/3gstudent.github.io/Exchange-Web-Service(EWS)%E5%BC%80%E5%8F%91%E6%8C%87 ...

  7. SoapException: Timed out while processing web services request

    情形:动态调用WebService时,语句method.Invoke异常. 异常信息为调用目标发生异常,从异常信息并不能看出问题所在,需要查看InnerException,如标题所述:处理web请求超 ...

  8. 在普通网页显示正常,加Https报This request has been blocked; the content must be served over HTTPS.,https网站加载http资源时,http资源被block

    解决办法 :在html头加<meta http-equiv="Content-Security-Policy" content="upgrade-insecure- ...

  9. Java NIO学习系列一:Buffer

    前面三篇文章中分别总结了标准Java IO系统中的File.RandomAccessFile.I/O流系统,对于I/O系统从其继承体系入手,力求对类数量繁多的的I/O系统有一个清晰的认识,然后结合一些 ...

  10. QT POST/GET HTTP操作

    工程文件 Qt += network 举例 Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui-> ...