在React Native中如何进行反向地理编码 [英] How to do Reverse Geocoding in react native

查看:74
本文介绍了在React Native中如何进行反向地理编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 react-native在React native中获取当前位置-geolocation 我得到了我所在位置的经度和纬度.现在,我想将其转换为位置的地址,而无需使用Google API密钥.

I am trying to get my current location in react native, using react-native-geolocation I get latitude and longitude of my location. Now I want to convert them into the location's address without using the Google API key.

是否可以在不使用Google API密钥的情况下将纬度经度转换为地址?

Is there any way to convert latitude longitude into an address without using the Google API key?

推荐答案

有很多方法可以在不使用Google Maps API的情况下将lon/lat转换为地址.搜索反向地理编码api ,您会找到很多替代方法.

There are many ways to convert lon/lat to address without using Google Maps API. Search reverse geocoding api and you'll find a bunch of alternatives.

几个月前,由于反向地理编码API请求,我被Google多收了.因此,我切换到了此处.他们有一个免费套餐,它可以提供每月25万个请求,适用于我的应用.在此处查看文档: https://developer.here.com/documentation/例子/休息/地理编码器/反向地理编码这将为您提供高度详细的地址数据(与Muhammad建议的ip-api.com不同).

A few months ago I was being overcharged by Google for reverse geocoding API requests. So I switched to Here. They have a free tier that offers 250k requests/months, which works for my app. See the docs here: https://developer.here.com/documentation/examples/rest/geocoder/reverse-geocode This will give you highly detailed address data (unlike ip-api.com suggested by Muhammad).

这是我用来调用API的包装函数:

Here is the wrapper function I use to call the API:

function getAddressFromCoordinates({ latitude, longitude }) {
  return new Promise((resolve) => {
    const url = `https://reverse.geocoder.ls.hereapi.com/6.2/reversegeocode.json?apiKey=${HERE_API_KEY}&mode=retrieveAddresses&prox=${latitude},${longitude}`
    fetch(url)
      .then(res => res.json())
      .then((resJson) => {
        // the response had a deeply nested structure :/
        if (resJson
          && resJson.Response
          && resJson.Response.View
          && resJson.Response.View[0]
          && resJson.Response.View[0].Result
          && resJson.Response.View[0].Result[0]) {
          resolve(resJson.Response.View[0].Result[0].Location.Address.Label)
        } else {
          resolve()
        }
      })
      .catch((e) => {
        console.log('Error in getAddressFromCoordinates', e)
        resolve()
      })
  })
}

这篇关于在React Native中如何进行反向地理编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆