How to Geolocate your website vistors.

Where in the World are you coming from?Where in the World are you coming from?

Got a website?

Want to restrict / customise your site based on where in the World they have come from?

Don’t want to pay or host your own IP / Country database table.

Then maybe this is the solution for you.

Geolocating an individual based on there IP address is not an exact science, there are various things that can get in the way e.g. proxies, javascript turned off or even a spoofed IP addresses.

Therefore always assume this might not work and have a fail back solution handy.

There are several service based solutions to cross reference visitor IP address against location, some are better than others and as you would expect the paid for services are more accurate – particularly when dealing with ISPs like AOL (remember them). These service based solutions are easier to implement as they do not require you to download and maintain a local database.

I was original going to suggest using the Google javascript method Google AJAX Loader API, unfortunately when testing this solution I found it only worked from some locations. i.e. it didnt recognise O2 in the UK, which is a reasonable size ISP, nor did it work for Jerry.

NB We are not using the new Google Gears Geolocation API as this needs the user to install Gears (and throws lots of prompts).

Instead I suggest you take a look at MaxMind who offer various open source / free APIs. The one we are going to look at is a Javascript API.

This API is free to use but the results are not as accurate as the paid for service so your mileage may vary, particualry if your looking for city accurate information.

To demonstrate what properties are returned by the API just click on this Demo .

This will open a floating iframe which calls a simple page which attempts to use this API.

The javascript is quite simple:

First within the Head of the document include a call to the main library.

<br></br>  
<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js" /><br></br>```

We are also going to reference [JQuery](http://www.jquery.com) and a plugin for striping the results table.

Within this Javascript block we also have an initialize method which is called automatically once the page has been loaded.

> We do not use the JQuery document ready function as we need to ensure the Maxmind script has loaded, therefore we use the traditional window.onload event handler.

Here’s the code block, though it might be easier to open the [Demo page](http://www.markbarton.com/geolocation/demo.htm) and view the source.




// Call this function when the page has been loaded

function initialize() {


//Stripe the table

$('#results').colorize( { hiliteColor:'none' } );```

try{
if(geoipcountryname()){
$("#latitude").html(geoiplatitude());
$("#longitude").html(geoip
longitude());
$("#addresscity").html(geoipcity());
$("#addresscountry").html(geoip
countryname());
$("#addresscountrycode").html(geoip
countrycode());
$("#addressregion").html(geoip
region());
$("#results").fadeIn('slow');
}
else{
$("#errorDisplay").show()
}
}
catch(err){$("#errorDisplay").html(err).show()}
}

//Use the window onload rather than the Jquery document ready to ensure the external script has completed
window.onload = initialize;

The rest of the HTML is just simply to display the values which have been returned and if there was an error or no results then display an error DIV.

Hope this is of use to someone.