Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GeoLocaton code not working in Chrome, why??

Status
Not open for further replies.

GUJUm0deL

Programmer
Jan 16, 2001
3,676
US
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.

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>
 
Are you running it through a web server (using http:// or
I've found that when using file based access (file://), Chrome does not allow this to function.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[blue]@[/blue] Code Couch:
[blue]@[/blue] Twitter:
 
I was testing it from my local HD...should I upload somewhere and see if Chrome works? Had not considered that as the issue.
 

should I upload somewhere and see if Chrome works

Yes, I would. When I ran your code through a web server, it at least attempted to use the geolocation feature in Chrome.

The code then went on to thrown an error on this line, however:

Code:
for (var i=0; i<results[j].address_components.length; i++) {

As you are attempting to use "j" outside of the loop, it has a value greater than the last index in results[], and so produces an error. Did you mean to use "indice" instead?

Dan


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top