// <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
var geocoder = new google.maps.Geocoder();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
codeLatLng(lat, lng);
});
}
function codeLatLng(lat, lng) {
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results)
if (results[1]) {
console.log('>>> ' + results[0].formatted_address);
for(var i = 0; i < results[0].address_components.length; i++){
var addressType = results[0].address_components[i].types[0];
var addressContent = results[0].address_components[i];
switch(addressType) {
case 'street_number': var numero = addressContent.short_name; break;
case 'route': var logradouro = addressContent.long_name; break;
case 'sublocality_level_1': var bairro = addressContent.long_name; break;
case 'administrative_area_level_2': var cidade = addressContent.long_name; break;
case 'administrative_area_level_1': var estado = addressContent.short_name; break;
case 'country': var pais = addressContent.short_name; break;
case 'postal_code': var cep = addressContent.short_name; break;
}
}
alert('>>>' + bairro + ' ' + estado + ' ' + cidade + ' ' + pais);
} else {
console.log(">>> No results found");
}
} else {
console.log(">>> Geocoder failed due to: " + status);
}
});
}