How to convert longitude and latitude to street address

邮差的信 提交于 2019-11-28 09:05:46

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.

amiramir

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
}

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

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>
var geocoder  = new google.maps.Geocoder();             // create a geocoder object
var location  = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);    // turn coordinates into an object          
geocoder.geocode({'latLng': location}, function (results, status) {
if(status == google.maps.GeocoderStatus.OK) {           // if geocode success
var add=results[0].formatted_address;         // if address found, pass to processing function
document.write(add);

source from https://gist.github.com/marchawkins/9406213/download# it works me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!