How to do Reverse Geocoding in react native

Viewed 13334

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?

2 Answers

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

there are many alternatives you can search reverse geocoding API

Solution 1:

By using Google map key

const myApiKey="Key Received from Google map"

function getAddressFromCoordinates({latitude, longitude}) {
  return new Promise((resolve, reject) => {
    fetch(
      'https://maps.googleapis.com/maps/api/geocode/json?address=' +
        latitude +
        ',' +
        longitude +
        '&key=' +
        myApiKey,
    )
      .then(response => response.json())
      .then(responseJson => {
        if (responseJson.status === 'OK') {
          resolve(responseJson?.results?.[0]?.formatted_address);
        } else {
          reject('not found');
        }
      })
      .catch(error => {
        reject(error);
      });
  });
}

Solution 2:

By using Here Plateform key

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

They have a free tier which gives us 250k requests/months free quota

const HERE_API_KEY="Key Received from Here Plateform"

function getAddressFromCoordinates({latitude, longitude}) {
  return new Promise((resolve, reject) => {
    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) => {
        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 {
          reject('not found')
        }
      })
      .catch((e) => {
        reject(e);
      })
  })
}
Related