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?
HTML
<script src="http://maps.google.com/maps/api/js?sensor=false&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(-29.911936, 21.283713); // will succeed