Thursday, April 16, 2009

Get geographical location (geolocation) by IP address using jQuery

Today I came across this post called “IP Address Geolocation Javascript API : JSON”. The author provides you with a free geolocation query URL. The API returns the geographical location of the queried IP address with some additional information such as:

{
	'status':'ok',
	'IP': '74.125.45.100',
	'CountryCode': 'US',
	'CountryName': 'United States',
	'RegionName': 'California',
	'ZipPostalCode': '94043',
	'City': 'Mountain View',
	'Latitude': '37.4192',
	'Longitude': '-122.057'
}

// In case of an error
{
	'status':'parent server not responding'
}

Update: the URL has been changed!

The JSON geolocation querying API’s address is:

http://iplocationtools.com/ip_query.php?output=json&ip=80.80.214.93

The URL above is dead, instead use this one:
http://www.geoplugin.net/json.gp?jsoncallback=?

And the great thing is, you can identify your website visitor’s IP and Geo location by simply querying the API without any parameters like this:

http://iplocationtools.com/ip_query.php?output=json

Knowing your users’ IP and/or location, you might add a behavior to your website that is specific to some location. For example, offering some advertising to US only visitors, or popup with special offer to European users.

Anyway, here is a sample jQuery code to query the API:

// Build the URL to query
var url = "http://iplocationtools.com/ip_query.php?output=json&callback=?&ip=";

// Utilize the JSONP API
$.getJSON(url, function(data){
    if(data['status'] == 'ok'){
        // Do something with the data
        $('#profile #ip')
            .append(data['IP']);
        $('#profile #country')
            .append(data['CountryName']);
    }
});

Here we are not specifying any IP address in the url variable that is why it is getting current user’s data.

15 comments:

  1. thanks alot for the tutorial,
    really helpful, i was working on the same api using xml output in asp.net, but had many issues so i dropped the it. now i know how to do it in jQuery.

    Thanks again.

    ReplyDelete
  2. Very interesting! I have needed this way too many times. Thanks.

    ReplyDelete
  3. Great jQuery implementation!

    Do you know if the underlying service can scale if many people start using this service.

    ReplyDelete
  4. @Blake Schwendiman, I don't think so. It is a personal server as far as I've understood. I am planning to set up the same service to one of the free server I have access to... will give the alternative URL's once I set them up...

    Thanks for your comments everyone...

    ReplyDelete
  5. For those of you wanting a scalable solution to Geo IP locating,check out Google's new API Google.loader.ClientLocation

    ReplyDelete
  6. Thanks @TonyMiller, I came across this yesterday on Twitter. Thanks for sharing with other :)

    ReplyDelete
  7. @Flavio Camus, Thanks mate... Changed the service URL. Now it works fine and more reliable...

    ReplyDelete
  8. Can we integrate it in our websites for free.

    ReplyDelete
  9. @Anonymous, Sure. All services and libraries are free of charge...

    ReplyDelete
  10. If you want to combine more then one location API to get better results look here (its available with JavaScript an free): http://united-coders.com/christian-harms/ip2-geolocation-mashup-tutorial

    ReplyDelete
  11. how to use the same script for java??

    ReplyDelete
  12. Hey,
    http://iplocationtools.com/ip_query.php?output=json
    this link is not working :-(

    ReplyDelete