【问题标题】:How to do Reverse Geocoding in react native如何在本机反应中进行反向地理编码
【发布时间】:2020-08-30 23:04:45
【问题描述】:

我正在尝试使用 react-native-geolocation 在本地反应中获取我当前的位置,我得到了我所在位置的纬度和经度。现在我想在不使用 Google API 密钥的情况下将它们转换为位置地址。

有没有什么方法可以在不使用 Google API 密钥的情况下将经纬度转换为地址?

【问题讨论】:

    标签: react-native geolocation reverse-geocoding


    【解决方案1】:

    ,没有 API 密钥,您无法获得准确的地址

    如果你想获取 IP 基础位置,那么你可以使用下面的 IP-base-API 和 fetch

    fetch('http://ip-api.com/json')
        .then((response) => response.json())
        .then((response) => {
          console.log('User\'s Location Data is ', response);
          console.log('User\'s Country ', response.country);
        })
        .catch((error) => {
          console.error(error);
        });
    

    但您可以使用 colakollektiv answer 等免费配额获得反向地理编码。但是使用免费额度后需要付费

    https://developer.here.com/documentation/geocoder/dev_guide/topics/example-reverse-geocoding.html

    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()
          })
      })
    }
    

    【讨论】:

      【解决方案2】:

      有很多方法可以在不使用 Google Maps API 的情况下将 lon/lat 转换为地址。搜索reverse geocoding api,你会发现一堆替代品。

      几个月前,Google 因反向地理编码 API 请求向我多收费用。所以我切换到Here。他们有一个 free tier 提供 250k 请求/月,这适用于我的应用程序。在此处查看文档:https://developer.here.com/documentation/examples/rest/geocoder/reverse-geocode 这将为您提供非常详细的地址数据(与 Muhammad 建议的 ip-api.com 不同)。

      这是我用来调用 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()
            })
        })
      }
      

      【讨论】:

      • 感谢您的回答,但可以告诉我这里的${AppConfig.HERE_REVERSE_GEOCODER_URL} 是什么吗?
      • @Iffat 我更新了答案以显示网址。这是您需要为反向地理编码 API 调用的端点。您也可以在我喜欢的 Here 文档中找到它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-02
      • 2021-01-26
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      相关资源
      最近更新 更多