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