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.
i did the same yesterday ;)
ReplyDeletethanks alot for the tutorial,
ReplyDeletereally 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.
Very interesting! I have needed this way too many times. Thanks.
ReplyDeleteGreat jQuery implementation!
ReplyDeleteDo you know if the underlying service can scale if many people start using this service.
@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...
ReplyDeleteThanks for your comments everyone...
For those of you wanting a scalable solution to Geo IP locating,check out Google's new API Google.loader.ClientLocation
ReplyDeleteThanks @TonyMiller, I came across this yesterday on Twitter. Thanks for sharing with other :)
ReplyDeleteThanks for sharing ^
ReplyDeleteService overloaded.
ReplyDelete@Flavio Camus, Thanks mate... Changed the service URL. Now it works fine and more reliable...
ReplyDeleteCan we integrate it in our websites for free.
ReplyDelete@Anonymous, Sure. All services and libraries are free of charge...
ReplyDeleteIf 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
ReplyDeletehow to use the same script for java??
ReplyDeleteHey,
ReplyDeletehttp://iplocationtools.com/ip_query.php?output=json
this link is not working :-(