Trying out Google Client Location service

October 12, 2009

I played around with Google’s location service, their IP address to location lookup thingy that’s squirreled away inside the AJAX library. Quite easy to use, unless you’re behind a corporate firewall/proxy like us, where it doesn’t work. The code below worked fine for me at home though, telling me I’m in Melbourne, Victoria, Australia. Which is close enough for our purposes on Citysearch.

<html>
<head>
<title>google location test</title>
<script src="http://www.google.com/jsapi"></script> 
<script type="text/javascript">
            var city = latitude = longitude = country = region = "dunno";
            if (google.loader.ClientLocation) {
                latitude = google.loader.ClientLocation.latitude;
                longitude = google.loader.ClientLocation.longitude;
                city = google.loader.ClientLocation.address.city;
                region = google.loader.ClientLocation.address.region;
                country = google.loader.ClientLocation.address.country;
            }
</script>
</head>
<body>
<h1>You are in (roughly): <script type="text/javascript">
document.write(city);
</script></h1>
Other parts of the location include:

<script type="text/javascript">
            document.write("Latitude: "+latitude+"<br/>");
            document.write("Longitude: "+longitude+"<br/>");
            document.write("City: "+city+"<br/>");
            document.write("Region: "+region+"<br/>");
            document.write("Country: "+country+"<br/>");
</script>
</body>
</html>
Advertisements

2 Responses to “Trying out Google Client Location service”

  1. Ayesha Says:

    Hi,

    I tried using this code for my application but it gives all null values or “dunno” at all places.

    • csausdev Says:

      This article is 3 years old, things have most likely changed since then. HTML5 includes support for browser location determination now, you’ll probably be better off trying to use that.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: