Search code examples
react-nativegeolocation

get city with react-native geolocation?


Is it possible to get the city the user is in using the react native geolocation API?

For example, running after running getCurrentPosition(), using the longitude and latitude to get the city?


Solution

  • The navigator geolocation API is built on the Geolocation API.

    Therefore you will probably require a Google API to get the other details such as city country etc ,by reverse geocoding and calling the api as

    https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${long}&key=YOUR_API_KEY
    

    Check for more details in the docs

    Alternatively, I would suggest you to use react-native-device-info and get IP Address getIPAddress method

    DeviceInfo.getIPAddress().then(ip => {
      // "92.168.32.44"
    });
    

    and check the location based on IP using IPStack

    https://api.ipstack.com/134.201.250.155?access_key = YOUR_ACCESS_KEY