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

HTML

<script src="http://maps.google.com/maps/api/js?sensor=false&amp;fake=.js"></script>

JavaScript

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