Hi, I came across this code online and made modification to fit my needs. It works in FF and IE9 but not in Chrome (Chrome ver 21).
I've spent a few days trying to figure out and why and so far no luck...
Thanks in advance.
I've spent a few days trying to figure out and why and so far no luck...
Thanks in advance.
Code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="[URL unfurl="true"]http://maps.googleapis.com/maps/api/js?sensor=false"></script>[/URL]
<script type="text/javascript">
var geocoder;
if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(successFunction, errorFunction); }
//Get the latitude and the longitude;
function successFunction(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
codeLatLng(lat, lng)
}
function errorFunction(){ alert("Geocoder failed"); }
function initialize() { geocoder = new google.maps.Geocoder(); }
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]) {
var indice=0;
for (var j=0; j<results.length; j++) {
if (results[j].types[0]=='street_address') {
indice=j;
break;
}
}
for (var i=0; i<results[j].address_components.length; i++) {
if (results[j].address_components[i].types[0] == "sublocality") {
city = results[j].address_components[i];
}
if (results[j].address_components[i].types[0] == "administrative_area_level_1") {
region = results[j].address_components[i];
}
if (results[j].address_components[i].types[0] == "country") {
country = results[j].address_components[i];
}
}
//city data
alert(city.long_name + " || " + region.long_name + " || " + country.short_name);
var cName = region.long_name;
document.getElementById("demo").innerHTML = cName;
} else { alert("No results found"); }
} else { alert("Geocoder failed due to: " + status); }
});
}
</script>
</head>
<body onload="initialize()">
<p>you are in <span id="demo"></span></p>
</body>
</html>