Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert longitude and latitude to street address

Given the latitude and longitude, how do we convert it to street address using Javascript or Python?

like image 378
MrCooL Avatar asked Jul 04 '11 17:07

MrCooL


People also ask

How do you find an address from longitude and latitude?

To find a location using its latitude and longitude on any device, just open Google Maps. On your phone or tablet, start the Google Maps app. On a computer, go to Google Maps in a browser. Then enter the latitude and longitude values in the search field — the same one you would ordinarily use to enter an address.

Can you get an address from GPS coordinates?

Google Maps is an easy way to pull up coordinates if you want to use a familiar program to find your address. You can use longitude and latitude to find an address on Google Maps from your phone, tablet, or computer. Visit the website or open the app to start looking for your address.


4 Answers

99% of the time, most people link you to Google Maps's API. Not a bad answer. HOWEVER -- Beware of the prohibited uses, usage limits and Terms of Use! While a distributed app many not run afoul of the usage limit, it is quite limiting for a web app. The TOS does not allow you to repurpose Google's data into an app with your skin on it. You would hate to have your business plan derailed by a cease and desist letter from Google, no?

All is not lost. There are several open sources of data, including US Government sources. Here are a few of the best:

The US Census Tiger Database, in particular, supports reverse geocoding and is free and open for US addresses. Most other databases derive from it in the US.

Geonames and OpenStreetMap are user supported in the Wikipedia model.

like image 127
dawg Avatar answered Oct 24 '22 19:10

dawg


You could try https://mapzen.com/pelias it's open source and actively being developed.

For example: http://pelias.mapzen.com/reverse?lat=40.773656&lon=-73.9596353

returns (after formatting):

{
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "properties":{
            "id":"address-node-2723963885",
            "type":"osmnode",
            "layer":"osmnode",
            "name":"151 East 77th Street",
            "alpha3":"USA",
            "admin0":"United States",
            "admin1":"New York",
            "admin1_abbr":"NY",
            "admin2":"New York",
            "local_admin":"Manhattan",
            "locality":"New York",
            "neighborhood":"Upper East Side",
            "text":"151 East 77th Street, Manhattan, NY"
         },
         "geometry":{
            "type":"Point",
            "coordinates":[-73.9596265, 40.7736566]
         }
      }
   ],
   "bbox":[-73.9596265, 40.7736566, -73.9596265, 40.7736566],
   "date":1420779851926
}
like image 43
amiramir Avatar answered Oct 24 '22 20:10

amiramir


You could use the Google Maps API. It has an API function that does exactly this: http://code.google.com/intl/da-DK/apis/maps/documentation/javascript/services.html#ReverseGeocoding

like image 5
Mathias Schwarz Avatar answered Oct 24 '22 18:10

Mathias Schwarz


You could use Google Geo API. A sample code for API V3 is available on my blog

<HEAD>
  <TITLE>Convert Latitude and Longitude (Coordinates) to an Address Using Google Geocoding API V3 (Javascript)</TITLE>

  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script>

    var address = new Array();

    /*
    * Get the json file from Google Geo
    */
    function Convert_LatLng_To_Address(lat, lng, callback) {
            var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + lng + "&sensor=false";
            jQuery.getJSON(url, function (json) {
                Create_Address(json, callback);
            });     
    }

    /*
    * Create an address out of the json 
    */
    function Create_Address(json, callback) {
        if (!check_status(json)) // If the json file's status is not ok, then return
            return 0;
        address['country'] = google_getCountry(json);
        address['province'] = google_getProvince(json);
        address['city'] = google_getCity(json);
        address['street'] = google_getStreet(json);
        address['postal_code'] = google_getPostalCode(json);
        address['country_code'] = google_getCountryCode(json);
        address['formatted_address'] = google_getAddress(json);
        callback();
    }

    /* 
    * Check if the json data from Google Geo is valid 
    */
    function check_status(json) {
        if (json["status"] == "OK") return true;
        return false;
    }   

    /*
    * Given Google Geocode json, return the value in the specified element of the array
    */

    function google_getCountry(json) {
        return Find_Long_Name_Given_Type("country", json["results"][0]["address_components"], false);
    }
    function google_getProvince(json) {
        return Find_Long_Name_Given_Type("administrative_area_level_1", json["results"][0]["address_components"], true);
    }
    function google_getCity(json) {
        return Find_Long_Name_Given_Type("locality", json["results"][0]["address_components"], false);
    }
    function google_getStreet(json) {
        return Find_Long_Name_Given_Type("street_number", json["results"][0]["address_components"], false) + ' ' + Find_Long_Name_Given_Type("route", json["results"][0]["address_components"], false);
    }
    function google_getPostalCode(json) {
        return Find_Long_Name_Given_Type("postal_code", json["results"][0]["address_components"], false);
    }
    function google_getCountryCode(json) {
        return Find_Long_Name_Given_Type("country", json["results"][0]["address_components"], true);
    }
    function google_getAddress(json) {
        return json["results"][0]["formatted_address"];
    }   

    /*
    * Searching in Google Geo json, return the long name given the type. 
    * (if short_name is true, return short name)
    */

    function Find_Long_Name_Given_Type(t, a, short_name) {
        var key;
        for (key in a ) {
            if ((a[key]["types"]).indexOf(t) != -1) {
                if (short_name) 
                    return a[key]["short_name"];
                return a[key]["long_name"];
            }
        }
    }   

  </SCRIPT>
</HEAD>
like image 1
Moh Avatar answered Oct 24 '22 18:10

Moh