react-nativegeolocationreverse-geocoding

How to do Reverse Geocoding in react native


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?


Solution

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