Skip to content

Instantly share code, notes, and snippets.

@Sigmus
Created August 31, 2015 21:55
Show Gist options
  • Save Sigmus/a9a65c4af3f3c8f68d42 to your computer and use it in GitHub Desktop.
Save Sigmus/a9a65c4af3f3c8f68d42 to your computer and use it in GitHub Desktop.
Country Name
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
var geocoder = new google.maps.Geocoder;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: {lat: 41.879, lng: -87.624} // Center the map on Chicago, USA.
});
map.addListener('click', extractCountry);
function extractCountry(data) {
geocoder.geocode({'location': data.latLng}, function(results, status) {
var country = null;
_.forEach(results[0].address_components, function(item, addressIndex) {
_.forEach(item.types, function(type, typeIndex) {
if ('country' === type) {
country = results[0].address_components[addressIndex].short_name;
}
})
});
alert(country)
});
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?signed_in=true&callback=initMap"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment