13

I always end up needing the user's location on most web projects that I work on. I've used Maxmind's GeoIp, but it involves you importing their dataset and it needs constant updating. There are also other free and paid services, but I just wanted something simple that I could add to any site in a matter of seconds.

So this is sort of baited because I have a simple solution, detailed below, but I wanted to see if anyone else uses this technique and if there are any better solutions or if there are some unforeseen pitfalls.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Greg Roberts
  • 2,562
  • 1
  • 20
  • 23
  • 3
    Wow, that's...a little creepy. – Robert Harvey Oct 16 '09 at 06:29
  • Is there a way to capture this data on the server side rather than the client side? – Matt Huggins Oct 16 '09 at 06:36
  • I haven't seen any data from google on accuracy, but I would think there are a lot of factors in how good of info you get. In general ip to location data is somewhat unreliable for really low level accuracy (any thing more granular than city), but usually that's all I care about. – Greg Roberts Oct 16 '09 at 06:42
  • Matt, I guess it depends on what you are trying to do. Obviously if you have this on the client you can send it to your server, but if you want to actually make the call from server side code it may be tricky since google is just using the http get request's ip as it's input. – Greg Roberts Oct 16 '09 at 06:45
  • 1
    Accuracy isn't even at city level a lot of the time. Country may be the best you can get. IP->Location works on where the IP address is registered. For example my IP at home is registered to my IP, whose address is in Leeds. I'm around 400 miles away. AOL's IP addresses come from AOL HQ, but are used throughout the US (although they're broken down into state in some cases). It's never going to be very accurate. – blowdart Oct 16 '09 at 06:53
  • You can't rely even on country-level accuracy. Last week I was using a 3G modem to get online via a UK cellular network. For some reason, sites using GeoIP decided I was in Germany. Another example: a friend of mine works for a large company in their offices about thirty miles from London. However his office uses a proxy located in Spain, so his GeoIP location is wrong by about 800 miles. – NickFitz Oct 16 '09 at 12:17

3 Answers3

10

The implementation I currently use I've seen in a couple other questions, but I haven't seen it as the selected answer.

I've detailed this in my blog thenullreference.com, but here it is in short:


Essentially all you need to do is load Google's API loader script:

<script type="text/javascript" src="http(s)://www.google.com/jsapi"></script>

Then you have access to several properties that give you detailed location info.

The object you want to look at is google.loader.ClientLocation

  • ClientLocation.latitude
  • ClientLocation.longitude
  • ClientLocation.address.city
  • ClientLocation.address.country
  • ClientLocation.address.country_code - ISO 3166-1 country code
  • ClientLocation.address.region - country specific region in US this is state

*Note some of these can be null

For more info on this API check out here

Does anyone else use this? Does anyone have something that they use that is as simple and better? Are there issues with this approach?

I'm unaware of the coverage/accuracy of this solution, but I would think that Google keeps it updated and is probably pretty good.

Greg Roberts
  • 2,562
  • 1
  • 20
  • 23
8

I've used the wipmania free JSONP service, I'm not really sure about its accuracy but it's really simple to use:

// an example using jQuery
$.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) { 
  alert('Latitude: ' + data.latitude + 
        '\nLongitude: ' + data.longitude + 
        '\nCountry: ' + data.address.country); 
});

Check the snippet running here.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
2

I use the MaxMind GeoIP City/ISP/Organization (Web Service), this cost $20 for 50,000 queries and is always updated (see http://www.maxmind.com/app/web_services#city). I like the fact that I don't need to worry about database updates since it is all done on there server.

I'm using PHP to send the client IP address and in return I get the latitude, longitude as well as other useful like the region code, city, metropolitan code, area code, country, ISP, and organization name if known.

meme
  • 11,861
  • 2
  • 19
  • 20