Detecting Location Using Google AJAX API

by on 13th July 2009 with 9 Comments

mapThere are times when one needs to find out which part of the world a particular visitor is coming from. There are plenty of IP-to-Location lookup providers out there, who offer this service at a reasonable cost (depending on how much detail you require).

Google’s AJAX Maps API offers this look up for you free of cost (so long your needs are non-commercial). You can even use the latitude and longitude information returned by the API to plot the user’s location on a Google Map. Nifty eh? Let’s now look at a simple example – we will be detecting the user’s location based on his IP address and rendering it on a map.

Before we begin, you might want to check out the demo.

You will first need to obtain an AJAX Search API Key (it’s free, just sign up using your domain name, and you will be given a unique key, which is applicable for all pages hosted within your domain). Once you have that ready, fire up your favorite editor. We don’t need any server side code to be able to use the Maps API – so a plain HTML document is enough.

First, let’s place the “hooks” in the page, where the map and a caption will appear.

The Google AJAX API consists of a collection of individual APIs. By using a <script> tag in the head of the document, we will first load a single method, google.load, using which we can load any or all of the APIs we need individually.


In our case, we just need the Maps API, so we do:

google.load("maps", "2", {callback: initialize});

The load function above takes the API name, its version and an optional callback function (which is called once the Maps API has finished loading) as parameters. In the init() function, we will first define a default location to be displayed on the map, in case the Maps API is not able to decode the IP address of the visitor. Since the Maps API basically matches the user’s IP address to a location, there might be instances when this mapping is not possible, and hence the need for a default.

var zoom = 4;
var latlng = new google.maps.LatLng(50, -10);
var location = "Showing default location for map.";

Next, we check if the client location was filled in by the API loader. If properly filled, the google.loader.ClientLocation object has the following properities:

  • ClientLocation.latitude
  • ClientLocation.longitude
  • ClientLocation.address.city
  • ClientLocation.address.country
  • ClientLocation.address.country_code
  • ClientLocation.address.region

Using this information, we can easily load a map, centred on the user’s location.

document.getElementById("location").innerHTML = location;		// location as text caption
var map = new google.maps.Map2(document.getElementById('map'));
map.setCenter(latlng, zoom);
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());

That’s it, see it in action. As I have already stated above, the client location information returned by this API is not perfect, as IP mapping can often be tricky, and might result in incorrect, or partially accurate information. While you should consider going for an enterprise ready solution for serious uses, I hope this API will be handy for your personal needs!

Comments & Discussion

9 Comments

  • http://clements.za.net ColtonCat

    I had forgotten that you could do this with the Google API, and was beginning to implement a similar system for my employer (http://africanchildrenschoir.com) when I happened accross this post.

    You mention in the final paragraph that IP location is not perfect – one instance I have discovered where it really is not perfect is if you are using a mobile device. My cellphone company, Rogers in Canada provide a Toronto located IP, regardless where you are accessing their HSPDA network from. This would obviously lead to misleading results, and would be a bigger problem if your site has a large number of mobile clients.

    Thanks…

  • http://www.webmasterlinkdegisim.com Link Degisimi

    I had forgotten that you could do this with the Google API, and was beginning to implement a similar system for my employer (http://africanchildrenschoir.com) when I happened accross this post.

  • http://bytekno.com bytekno.com
  • http://www.wisataciamis.com/ Melayu

    This post is very good for me.. i can get many tips and trick on this blog.. thank’s for u’r information my friend! this is my first time to visiting to your blog..

  • Fernando

    It doesnt work. I’m in Buenos Aires and it show the default location.

  • http://bonniexubaxter.livejournal.com/711.html#6924224352 Auto Mass Traffic

    qoqkmdrchohtbtsokooactkqodpqrppbve

  • http:///user/view/915568 Iesha Sannon

    These became remarkable yet again exercise routines where means that we can relax this vertebrae in addition to sleep accordingly without the working experience in regard to stress of your once more.

  • http://yatra-pay-later-domestic-flights19.onsugar.com/posts/feed yatra flight ticket

    This is getting a extra subjective, but I considerably prefer the Zune Industry. The interface will be colorful, has much more flair, and some awesome features like �Mixview� than enable you to quickly see associated albums, songs, and other users related to precisely what you�re listening to. Clicking on among those will center on in which item, and another pair of �neighbors� will come into look at, allowing you to navigate all around exploring by related artists, songs, or even users. Speaking of customers, the Zune �Social� can also be great fun, letting you discover others with discussed tastes and becoming pals with them. You then may listen to a playlist produced based on an amalgamation of the items all your friends are generally listening to, which is also pleasurable. Those concerned with level of privacy will be relieved to understand you can prevent the open public from seeing your own listening habits should you so choose.

  • http://cheapratedomain.com/ Himu

    thanks for the post.
    get the answer i was looking for.

Subscribe
Membership
About the Author