Free IP to Geo Location script

Christian Harms's picture

We offer a simple IP geolocation javascript for free!

After including you will be able to determine the geo position (lat/long) of your visitor, get his hometown and country. The only precondition is to include a link to http://united-coders.com/.

If you want to include it on high traffic sites it could make problems because there are limitations due to the conditions of the used webservices. We have build some caching strategies so it won't be an issue in the most cases. If you want to do more requests please read about limitations in the article of making a ip2location mashup and deploy your instance directly.

Play with it and use some free proxy servers to change your own IP!

How to include?

  1. <script type="text/javascript" src="http://ip-geo.appspot.com/geo_data.js?key=#your_google_key#">
  2. </script>
  3. <script type="text/javascript">
  4.   document.write(com.unitedCoders.geo.generate_map(500, 400));
  5. </script>
  6. Powered by <a href="http://www.united-coders.com/christian-harms/free-ip-location-script">united-coders.com</a>.

The backlink is not optional!

The google key parameter is optional. You need it if you want to use the google ip location api and/or the static google map to show all found locations. After printing the generate_map function it will display the following map and the found location.

You are near these cities:

We are using more than one IP2geo API for a more fault-tolerant script and more accurate position determination. Internally we call ipinfodb and hostip and after including our geo location javascript there will added more javascripts (for more locations):

  1. maxwind.com
  2. the google ajax api - if the key parameter is set
  3. WIPmedia.com

Our javascript "ip-geolocation and data aggregator" includes all these scripts - so the initial loading may take a while. A second request (after reload or from a second page) of the same visitor will be cached. After all a global object with all data will generated based on the calling ip. In the javascript context all data are available and/or you can use the javascript html generator function to generate simple html code.

The com.unitedCoders.geo object

  1. com.unitedCoders.geo = {
  2.   // this object list can vary from 0 up to five positions!!!
  3.   ll : [ { country: 'Germany', city: 'Esslingen', long: 9.3, lat:48.75, name:'iplocationtools'},
  4.          { country: 'Germany', city: 'Waldenbuch', long: 9.1333, lat:48.6333, name:'maxwind'},
  5.          { country: 'GERMANY (DE)', city: 'Karlsruhe', long: 8.4, lat: 49.05, name: 'hostip' }],
  6.   getLat: function(),            // this is computed with the ll-coordinates
  7.   getLong: function(),           // this is computed with the ll-coordinates
  8.   getPosition: function(),       // the middle point as object { lat : 1.0, long: 1.0 }
  9.   getLocation: function(),       // get the middle position with city/country
  10.   //calculate the difference in kilometer from the user to (lat, long)
  11.   differenceFromUser: function(lat, long),
  12.   generate_map: function(width, height),
  13. }

the generated html-code

First you have to add to your page using our geo location script some css-styles if you want to design the following pure html lines. For simple usage there exists the id "local_map".

  1. <div id="local_map">
  2.   <img src="http://maps.google.com/staticmap?size=320x240&key=your_google_key&markers=48.75,9.3,midblue1%7C49.05,8.4,midgreen2%7C48.6333,9.1333,midred3%7C48.8111,8.94444,black"/>
  3. <ol>
  4.   <li>iplocationtools: Esslingen, Germany</li>
  5.   <li>hostip: Karlsruhe, GERMANY (DE)</li>
  6.   <li>maxmind: Waldenbuch, Germany</li>
  7. </ol>
  8. </div>

The google map example use the google static map api so there is only one large url with the geo location parameters and the text lines below with the geo location service provider data. Feel free to use the javascript data directly or build better html widgets. But don't forget to include a link to united-coders.com for free usage!

Wieviel Requests schafft so eine app engine?

Vor einiger Zeit hab ich ein Geo Location Aggregator auf united-coders.com vorgestellt. Lange wurde dies auf einer app engine nur mit wenigen Requests genutzt. Doch irgendwann nutzt doch jemanden einen freien Dienst und dann werden die freien Limits irgen

Comments

Anonymous's picture

Great article.

Is there a way to exclude the results provided by WIPmania?

Christian Harms's picture

After checking the last 1000 visitors the wipmania api call is removed. The black locator will better match the visitors location!

Dallas's picture

Im working on a directory kinda like yellow pages i want to use something similar to this so my site will find the city the person is in and place it in the location bar just like yellowpages does any suggestions thanks

Anonymous's picture

thanks for your free script.

corro.info

Ritesh Ranjan's picture

Can you tell me how to get the ajax request working with

http://ipinfodb.com/ip_query.php?ip ,
i tried But its not working

the code i m using is like

var xmlhttp
function requestt()
{

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Your browser does not support XMLHTTP!");
return;
}
var url="http://ipinfodb.com/ip_query.php?ip";
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
var xmlDoc=xmlhttp.responseXML.documentElement;
alert(xmlhttp.responseXML);
// **** showing null **//
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}

Christian Harms's picture

Hi Ritesh,

you cant access with the XMLHttpRequest to an other domain than yours!

1. Use the JSONP-API from IPInfoDB to make cross-domain requests.

2. Or just include the offered Javascript like described in the example. Than there is no need to do the requests. Why do you try to access only one provider instead of let aggregating 4 provider form our script ?

Hoping this helps, Christian.

aasifnasim's picture

hi dear
thanx
its working and displaying near by map
so i would like to ask u
how do i pass a particular ip address and findout its location in this routine
thanx in advance

Christian Harms's picture

The live demo will use your calling ip directly and there is no parameter too set the ip. If you want to find the position for other IPs use the described API calls.

Anonymous's picture

what I didn't like in your comment to include your site link make me angry , you didn't include google link ! , here is all what you did without the red flag
http://code.google.com/p/gmaps-samples/source/browse/trunk/clientlocatio...

any one can just add the red flag , it was better to publish the code open source and don't make this restriction .

in the final you didn't do anything it is all done by google

Christian Harms's picture

If you use only the google client location you will get only one position (mostly without city/country). The aggregation script integrate up to five ip provider and run in my google app engine account with more optimizations (caching). Its not so clean to publish the code, but the application is good enough to get some backlinks for our blog. Feel free not to use it or implement the whole code - I wrote a php example too.

IPv6 request crashed my google app engine application | unit's picture

[...] IP geo location script cache requests coming from the same ip range (subnet mask 255.255.255.0) named as class-c ip [...]

Combining HTTP and JavaScript APIs with php | united-coders.'s picture

[...] implementation of the ip to geo location script started as a quick hack in php. Sometimes I use php scripts because everyone can include it in his [...]

Ömer Feyzoğlu's picture

ı am using the geo script provided by maxmind.com
what ı want to do is as follows: My base location is always Istanbul- Turkey and when a visitor visits my site I want to draw a line on the map from city to his city provided by geoscript ; kind of a truck moving goods from mine to his location, may be a little truck moving on the map to his.
It should be possible ?

Please let me know how and the fee.

info@groupenerji.com

best regards
Ömer Feyzoğlu

Tutorial for a IP to geolocation aggregator script | united-'s picture

[...] If you provide a restaurant guide it would be great to show the next restaurant based on webpage visitors position or the local beer garden specials if its sunny weather? Or offer geo targeted ads on your page? And this could be offered without registration or connection to a social network? I will describe the api/implementation details and offer directly our free "ip to geolocation aggregator"-script. [...]

Combining HTTP and JavaScript APIs with python on google app's picture

[...] this part I will introduce the python implementation of the ip to geolocation script. It's more object oriented and hopefully better to read. In the first part of this article I [...]

3 caching steps to boost your webservice by x10 | united-cod's picture

[...] am running a IP-Geo-location demo in my appengine. After many silent months I got some traffic on it (and hitting the daily CPU [...]

Anonymous's picture

looks good, do you have a wordpress plugin? if so please email me. i have no problem sending a backlink. thanks

DMG's picture

There is something wrong with http://ip-geo.appspot.com/geo_data.js
It doesn't handle Google API key:

First, before doing:

document.write('

');

it should check if the jsapi is already loading.
Second, this line is wrong as it concatenates API key two times and the API Key is not the one provided!

Thanks,
Dawid

Christian Harms's picture

Yes, you are right. The google api key was not part of the memcache key - fixed.

The second part is correct: I added an if (!window['google'] && !google.loader) before including the google api loading part. If you dont need the google part keep the key-query param empty and you will only get the location part of our data.

Christian Harms's picture

What should the wordpress plugin do? Should it offer the link to the short map with the position of the visitor or only a javascript wrapper as wordpress plugin?

Distinguishing between UK/US/Int visitors's picture

[...] geotracking is what you need. after doing quick google i found some Free IP to Geo Location script | united-coders.com just google the phrase "website geolocation script" __________________ Received: Too [...]