How to do Reverse Geocoding in react native

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-08 07:39:04

问题


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.

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


回答1:


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.

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).

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



回答2:


No, you cannot get an accurate address without API key

if you want to get IP base location then you can use below IP-base-API with 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);
    });

but you can get reverse geocoding with free quota like colakollektiv answer. but you have to pay after using free quota

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


来源:https://stackoverflow.com/questions/61796917/how-to-do-reverse-geocoding-in-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!