I know there are more accurate geolocation methods, but I want to have it transparent to the user rather than ask them to "allow".
I am using a very simple method and it works great on most of the computers I have tried, but on some such as my grandmothers laptop with IE7 I think and Windows XP, as well as a friends MAC, it did not work.
I figured that even if the location database came up empty, the else statement should fill in for New York, no?

What am I missing? thanks.

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">// <![CDATA[
if (typeof(google.loader.ClientLocation.address.city) != null) {
  document.write(google.loader.ClientLocation.address.city);
} else {
  document.write("New York");
}
// ]]>
</script>

what value are you getting in those cases?

typeof(google.loader.ClientLocation.address.city)

maybe its some invisible char or who knows

also check out if

document.write("New York");

works at all (force the if statement call else by adding to the check that value would be false)

I just posted this on the joomla forums, since I am using version 1.7. It is possible that the editor is messing up the javascript. It is moving the closing script tag </script> to the line that includes // ]]>
This likely invalidates the code since it comments out the script tag.

// ]]></script>

I am not positive yet, but we will see. I am trying to find out how to stop it from modifying the code first.

what value are you getting in those cases?
typeof(google.loader.ClientLocation.address.city)
maybe its some invisible char or who knows
also check out if
document.write("New York");
works at all (force the if statement call else by adding to the check that value would be false)

I set != to == and the else fell right into place and worked, so I am still not sure why it would come up empty in some cases.

I know there are more accurate geolocation methods, but I want to have it transparent to the user rather than ask them to "allow".
I am using a very simple method and it works great on most of the computers I have tried, but on some such as my grandmothers laptop with IE7 I think and Windows XP, as well as a friends MAC, it did not work.
I figured that even if the location database came up empty, the else statement should fill in for New York, no?

What am I missing? thanks.

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">// <![CDATA[
if (typeof(google.loader.ClientLocation.address.city) != null) {
  document.write(google.loader.ClientLocation.address.city);
} else {
  document.write("New York");
}
// ]]>
</script>

your conditional is wrong if (typeof(google.loader.ClientLocation.address.city) != null) { try it with:

if ( typeof( google.loader.ClientLocation.address.city ) ) {...

instead.

! Nope, this wont work either! You'll need my special exists() method instead.

I think it might be Apple's and old browsers that are causing the problem.

Any idea how to get this working on a MAC?

I think it might be Apple's and old browsers that are causing the problem.

Any idea how to get this working on a MAC?

I think it was google's db that was causing the problem. I just switched to maxmind and i think it works.

Is your visitor click 'allow' and grant you the approval to read their location? The problem with this technique was it require explicit approval from your visitor, if your visitor refuse to do so, you won't have any result display.

The better solution would be checking their location using geo database or web service, such as http://www.ipinfodb.com (free) or http://www.ip2location.com (paid).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.