Converting latitude/longitude into city name? (reverse geolocating)

后端 未结 6 1758
礼貌的吻别
礼貌的吻别 2020-12-23 15:11

I\'m working on a job board in Codeigniter PHP + jQuery where employers enter their location and we use Google Maps API to plot it. While this has had awesome usability res

相关标签:
6条回答
  • 2020-12-23 15:51

    The Google Geocoder, which supports reverse geocoding returns address_components. You just need to pull out the component of the address tagged with locality and political if you just want the city name. If you want states as well they are tagged with administrative_area_level_1

    0 讨论(0)
  • 2020-12-23 15:52

    FREE SOURCES (e.g. Creative Commons)

    Geonames

    If you're looking for free (as freedom) sources, you can use Geonames API findNearbyPlaceName.

    For example the following returns nearest Placename:

    http://api.geonames.org/findNearbyPlaceName?lat=47.3&lng=9&username=demo

    More information is available here

    http://www.geonames.org/export/web-services.html#findNearbyPlaceName

    Freebase

    Instead of single point it takes bounded box. Call sample:

    http://api.freebase.com/api/service/geosearch?location=[30.2,50.4,30.6,50.8]&location_type=/location/citytown&inside=true&indent=1

    Geocoder.ca

    Reverse geocoding for North American addresses.

    Info: http://geocoder.ca/

    Sample call: http://geocoder.ca/?latt=40.70771000786733&longt=-74.0109443664550&reverse=1&allna=1&geoit=xml&corner=1&jsonp=1&callback=test

    Map Quest API

    Using Open Street Map nominatim service:

    http://open.mapquestapi.com/nominatim/v1/search?q=50.4,30.4&format=json

    NON FREE SOURCES

    Yahoo PlaceFinder

    Documentation http://developer.yahoo.com/geo/placefinder/

    Sample call:

    http://where.yahooapis.com/geocode?q=38.898717,+-77.035974&gflags=R&appid=[yourappidhere]

    Google Maps API

    Documentation:

    http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding

    0 讨论(0)
  • 2020-12-23 15:53

    Can't you just do it do full address, and then drop the street and number?

    I would say it'd be more valuable to use the full address, so as to get a more accurate distance (even if you only display the city,state).

    EDIT: If you do do it that way, at least it should be returned in a standard form, which you can then easily regex the data you want out of.

    0 讨论(0)
  • 2020-12-23 16:00

    Html5 geolocation requires user permission. If you don't want this behaviour, you can go for an external provider like https://geoip-db.com. They offer a free geolocation service based on ip-addresses.

    • JSON: https://geoip-db.com/json/
    • JSONP: https://geoip-db.com/json/geoip.php?jsonp=callback

    Try this example:

    <!DOCTYPE html> 
    <html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>Geo City Locator</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    </head> 
    <body > 
        <div>Country: <span id="country"></span></div>
        <div>State: <span id="state"></spa></div>
        <div>City: <span id="city"></span></div>
        <div>Latitude: <span id="latitude"></span></div>
        <div>Longitude: <span id="longitude"></span></div>
        <div>IP: <span id="ip"></span></div>    
        <script>
          $.getJSON('https://geoip-db.com/json/geoip.php?jsonp=?') 
             .done (function(location) {
                $('#country').html(location.country_name);
                $('#state').html(location.state);
                $('#city').html(location.city);
                $('#latitude').html(location.latitude);
                $('#longitude').html(location.longitude);
                $('#ip').html(location.IPv4);               
             });
        </script>
    </body> 
    </html> 
    
    0 讨论(0)
  • 2020-12-23 16:10

    Reverse geocoding services (such as Google, Yahoo, and GeoNames) will work well for this purpose with a few caveats:

    • Although I can't say for sure how the commercial services work, last time I checked GeoNames uses a closest point of interest strategy to determine the city. This can be a problem if your query point is somewhat near a border between two cities and the nearest entry in the GeoNames database is on the other side of that border. This actually happens a fair bit in my experience, so this is not just some technicality.
    • Also, if you are concerned about user experience, any web API solution adds latency and reduces reliability.

    I run a web site that offers a number of commercial geographic web APIs, and sells the Java libraries that we built to back those APIs. One of them (which only has US coverage) uses a proper polygonal map of the cities and towns in the US to return the result that contains your query point. It is accurate and fast, so if this is a mission critical component in your system, I suggest taking a look at our web site:

    http://askgeo.com

    The page that is specific to looking up cities from lat/lon is:

    http://askgeo.com/database/UsPlace2010

    0 讨论(0)
  • 2020-12-23 16:10

    I was searching for an API to convert lat/long to city. Seems like this was the original question on this thread as well.

    I saw the data "http://download.geonames.org/export/dump/" shared on another stackoverflow thread Given the lat/long coordinates, how can we find out the city/country?, and implemented my own Web Service.

    You can see it running at http://scatter-otl.rhcloud.com/location?lat=36&long=-78.9 Just change the latitude and longitude for your locations.

    It is deployed on OpenShift (RedHat Platform). First call after a long idle period may take sometime, but usually performance is satisfactory. Feel free to use this service as you like...

    Also, you can find the project source at https://github.com/turgos/Location.

    0 讨论(0)
提交回复
热议问题