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.
Hello Adam,
Thanks for the info about GeoLite City. However, I have been investigating the Google Geolocation API, and unfortunately it requires every user to have google gears installed and the geolocation gear enabled, therefore using this would greatly reduce the number of users you would have using your site. I think a database, however out of date would support a greater number of users. Hopefully soon this will be a moot point as I think it is the intention of HTML 5 Specification to encourage browser developers to have geolocation switched on by default.
Anyway, thanks for the info
Roger –
Thanks for reading. I think you’re looking at something different – try the demo on my site http://www.adam-mcfarland.net/demos/geolocation.php That works for me in every browser without Gears installed.
P.S – just noticed the syntax highlighting isn’t working on the post, I’ll fix that today…
Was in a bit of a rush this morning when I wrote that comment. I just fixed the syntax highlighting.
I do agree that HTML 5 *could* solve the problem, but you’re still likely going to need a backup for the people who don’t enable it by default in their browser.
One other thing about using GeoCity – performance when querying a huge info source like that can be a big deal. If you’re willing to utilize their binary format + API you can probably solve the problem, but if you just decide to install it as a massive MySQL database it will really slow down your server. That’s a much bigger downside to me (having any additional server load that you don’t need) as opposed to it being slightly out dated.
I was not able to get your code to work?
the code keeped coming up with “Google was not able to detect your location”. Any ideas on why.
Also your demo and “http://www.adam-mcfarland.net/demos/geolocation.php” came up with the same display.
:/
Hi Alex. The demo is working for me. For one reason or another Google can’t get a location from your IP. Try browsing from another location or on another network.
Hello Adam,
Since my last message back in March, I have tried the demo on doezens of pcs using chrome and IE and every time I get the “Google was not able to detect your location” message
I was wondering, could it be an issue with me being in the UK?
Thanks for the follow-up Roger. That’s unfortunate. It’s certainly possible that it’s a US only feature or that you have to do something differently to get it to work internationally. I’ve only tested it here in NY. It’s worked at multiple locations (home, work, etc). I haven’t used it in production yet, and I’m not sure I will with HTML5 geolocation available now.
PS – I didn’t go back and read the old comments before that post. Interesting to see how my thoughts on HTML5 have changed a bit. I think it depends on your application – most of the time you want to get permission from your users, in which case HTML5 is ideal and supported across more and more browsers, especially mobile browsers. If you’re doing something simple like showing a different piece of text on a page to different users (say changing an estimated shipping delivery date based upon distance), then it makes sense to use another solution like this JS or a database.
I just found this link by googling for a client side solution to show country based notice messages.
thanks for the example
The example page was very precise in locating my suburb in Sydney Australia. I don’t think it’s a US only feature, more likely that Roger is in the remaining 20%
Thanks for the insight Enrico. Glad to hear it worked for you in Australia!