StackOverflow: 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?

by hippietrail

JavaScript

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