I recently started looking into IP geolocation/geotargeting for some projects. By “geolocation” or “geotargeting” I mean serving different content to users based upon their physical location as determined by their IP address. Now, as we all know IP addresses are far from perfect so the best you’re going to get seems to be ~80% accuracy. That said, there are still a lot of great things you can do for those people if you know where they are coming from.
An example for Detailed Image around holiday time would be showing different content to people who are past the safe cutoff date to receive a shipment before Christmas. For instance, on 12/21 a person in California might see an ad hyping up our gift certificates, while a visitor in New York would see an ad that they can still receive a ground shipment prior to Christmas.
And that’s just one example. I’m planning on doing some other things with geolocation for a future project, so I though it was time to finally figure out how I was going to do it.
And once I started looking into it I realized that the only way to do this with any kind of accuracy is to query a very large, actively maintained database of IP addresses and their corresponding locations. I narrowed it down to two options:
- GeoLite City – a free downloadable database that is update monthly and claims to be 79% accurate to a city level (which is where I got the 80% number above). They also offer a premium version that’s slightly more accurate.
- Google’s API, which maintains this database on their end for Google Maps and probably fifty other things that we don’t realize.
There are some definite advantages of having the GeoLite City database on your server, namely that you can query it on the server side prior to loading a page. To do this on any scale, however, you really shouldn’t install it as a MySQL database. Instead you should use their binary format and corresponding API.
This seems like an awful lot of work. Plus it requires that someone update the database monthly when new releases come out. Worth it for some people and some projects I’m sure. Not me though. I also don’t like using my resources when working on a project when I can use someone else’s resources, especially when it’s Google and it’s free and reliable.
So then I started digging into Google and I had a really, really hard time finding documentation. Finally I found this page with one example way down on the bottom, and with some common sense was able to use that to get what I needed.
In hopes of saving someone else some time, here’s a really simple way to implement IP geotargeting on your site in a few minutes. The simple function below gets all of the important info from Google (latitude, longitude, city, country, region) and writes it out to the page. What you do from there is up to your imagination!
Here’s all you have to do:
- Sign-up for an AJAX Search API Key
- Use the Javascript below, replacing YOURKEY with your actual key from Google
- That’s it! Check out a demo on my site.
Javascript:
<script type="text/javascript" src="http://www.google.com/jsapi?key=YOURKEY"></script>
<script type="text/javascript">
function geoTest() {
if (google.loader.ClientLocation) {
var latitude = google.loader.ClientLocation.latitude;
var longitude = google.loader.ClientLocation.longitude;
var city = google.loader.ClientLocation.address.city;
var country = google.loader.ClientLocation.address.country;
var country_code = google.loader.ClientLocation.address.country_code;
var region = google.loader.ClientLocation.address.region;
var text = 'Your Location<br /><br />Latitude: ' + latitude + '<br />Longitude: ' + longitude + '<br />City: ' + city + '<br />Country: ' + country + '<br />Country Code: ' + country_code + '<br />Region: ' + region;
} else {
var text = 'Google was not able to detect your location';
}
document.write(text);
}
geoTest();
</script>


Demo is not workin!!can u chk tht out
Hi Josh – the demo works for me. What does it show for you? Could possibly be that Google cannot get your location based upon your IP…
Made a slight adjustment to the code. It now will display text if it cannot retrieve your location…one of the things you’d need to have in place if you were to use this in a production environment.
Works for me
Yeah, geolocation is a very cool thing. Just be careful, though. Whether it’s Google or anyone else, APIs usually have limits (especially ones like this which are meant to be used heavily). Be sure to always read the terms carefully, and use the paid version of the API if you need to. The last thing you want is to reach visitor number 1,001 in a day and have the API begin spitting out results you didn’t expect.
Definitely. I don’t plan on using this for any real application until some point in 2010, but I’ll be sure to read through all of the terms prior to starting development. Good practice for any API or service that you rely on heavily.