React-native 获取当前定位地理位置

0.60以下的RN通过高德的逆地理编码获取当前所在位置的信息
不需要任何插件!

// 获取位置并逆地理转换
  handleGetLocation=()=>{
    // 当前定位经纬度
    navigator.geolocation.getCurrentPosition(
      (position) => {
        const initialPosition = position
        console.log(initialPosition)
        const { longitude } = initialPosition.coords
        const { latitude } = initialPosition.coords
        console.log(`${longitude},${latitude}`)
        //通过调用高德地图逆地理接口,传入经纬度获取位置信息
        fetch(`http://restapi.amap.com/v3/geocode/regeo?key=你得高德KEY&location=${longitude},${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) => {
            try {
              console.log(jsonData)
            }catch (e) {

            }
          })
          .catch((error) => {
            console.error(error);
          })
      },
      (error) => console.log(error),
      {  timeout: 20000, maximumAge: 1000 },
    )
  }

0.60版本RN以上的需要下载@react-native-community/geolocation
官网地址:
https://reactnative.cn/docs/0.62/geolocation

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容