Search code examples
javascriptgeolocation

How to do Convert Latitude/Longitude To Address


I wrote some code this codes worked but only get latitude and longitude but ı want convert latitude and longitude from Street adress.

<!DOCTYPE html>
<html>

<head>
  <title>location</title>
</head>
<body>
  <script type="text/javascript">
    navigator.geolocation.getCurrentPosition(function(position) {
      document.write("latitude= " + position.coords.latitude);
      document.write("longitude= " + position.coords.longitude);
    });
  </script>
</body>

</html>


Solution

  • If you want to get latitude and longitude from an address, then the process is called geocoding and can be completed using the Google Maps API. Here is sample python code to run this conversion:

    import requests
    import json
    
    def convert(address):
        URL = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + address + '&key=[INSERT API KEY]&callback=initMap'
        r = requests.get(URL).json()
        return r['results'][0]['geometry']['location']
    

    If instead you would like to get the address based on the latitude and longitude, reverse geocoding, also from the Google Maps API, is the way to go.