Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Country name for GPS coordinates

Is there an easy way to get the name of the country on whose territory a given point is located?

I don't care too much about accuracy nor about ambiguities given by political disputes. Just need a good enough approximation.

like image 776
ibz Avatar asked Sep 03 '10 06:09

ibz


People also ask

What are GPS coordinates called?

GPS coordinates are most commonly expressed as latitude and longitude. This system divides the earth into latitude lines, which indicate how far north or south of the equator a location is, and longitude lines, which indicate how far east or west of the prime meridian a location is.


5 Answers

EDIT: The Yahoo Where API is no longer available.

Try the Yahoo! APIs.

Latitude and Longitude

Latitude and longitude can be specified for the location parameter. Latitude and longitude may be expressed as decimal degrees or degrees-minutes-seconds, with either leading or trailing directionals or leading signs. If directionals are provided, longitude may appear before latitude. If directionals are not provided, longitude may appear before latitude if it is outside the range -90 to 90. Otherwise, the latitude must appear first. Punctuation marks (commas, degrees, minutes, seconds) are ignored.

Examples: 

•50.3 -120.5
•50.3, -120.5
•-120.5 50.3
•50.3 N 120.5 W
•120.5 W 50.3 N
•50 18 0 -120 30 0
•50 18 0 N 120 30 0 W
•50° 18' 0" N 120° 30' 0" W

The response element will give you the country, as well as many other elements.

Don't forget to send as parameter gflags=R, to make the reverse geocoding. If you want the output in json, send the parameter flags=J as well.

like image 68
GalacticJello Avatar answered Oct 19 '22 22:10

GalacticJello


Just for the reference, geonames.org also has a nice webservice, but it's rate limited (which was an issue in my case since I had to look up a big batch of coordinates).

Example: http://ws.geonames.org/findNearbyPlaceName?lat=47.3&lng=9

like image 5
ibz Avatar answered Oct 19 '22 20:10

ibz


How about using google's reverse geo-encoding service?

like image 5
Kent Boogaart Avatar answered Oct 19 '22 20:10

Kent Boogaart


You could also look at the services from tinygeocoder.

Another alternative is to download the country maps from natural earth (or another source). Using Geotools you could search whether the point is inside one of the countries.

like image 1
Joachim Van der Auwera Avatar answered Oct 19 '22 21:10

Joachim Van der Auwera


Here's some JavaScript code to get the name of the country from latitude and longitude coordinates:


  1. Using Google's geocoding services [jsfiddle]:

    var lookupCountryWithGoogle = function(lat, lng) { var latlng = new google.maps.LatLng(lat, lng);

    var geoCoder = new google.maps.Geocoder();
    
    geoCoder.geocode({
        location: latlng
    }, function(results, statusCode) {
        var lastResult = results.slice(-1)[0];
    
        if (statusCode == 'OK' && lastResult && 'address_components' in lastResult) {
            alert('coordinates: ' + lat + ', ' + lng + '\n' + 'country: ' + lastResult.address_components.slice(-1)[0].long_name);
        } else {
            alert('coordinates: ' + lat + ', ' + lng + '\n' + 'failed: ' + statusCode);
        }
    });
    

    };

    lookupCountryWithGoogle(43.7534932, 28.5743187); // will succeed lookupCountryWithGoogle(142, 124); // will fail

(You'll need to load Google's JavaScript support for this from a URL such as maps.google.com/maps/api/js?sensor=false&fake=.js)


  1. Using ws.geonames.org [jsfiddle]:

    var lookupCountryWithGeonames = function(lat, lng) {

    $.getJSON('http://ws.geonames.org/countryCode', {
        lat: lat,
        lng: lng,
        type: 'JSON'
    }, function(results) {
        if (results.countryCode) {
            alert('coordinates: ' + lat + ', ' + lng + '\n' + 'country: ' + results.countryName);
        } else {
            alert('coordinates: ' + lat + ', ' + lng + '\n' + 'failed: ' + results.status.value + ' ' + results.status.message);
        }
    });
    

    };

    lookupCountryWithGeonames(43.7534932, 28.5743187); // will succeed

    lookupCountryWithGeonames(142, 124); // will fail ​

like image 1
hippietrail Avatar answered Oct 19 '22 20:10

hippietrail