Convert IP addresses to geolocation, latitude and longitude etc etc

2 August 2018 edit: has now been discontinued, but you might find some of the discussion and warnings about data quality in this post are still interesting

23 June 2016 edit: before you attempt any real work with IP geolocation, you must, must, must read this article: . Be aware of the limitations.

Whoa! Now this is cool. It turns out there is a database at which you can query for the location of a particular IP address. And as it has a neat little API for batches of IPs, you can get R to fetch them en masse. That’s exactly what Heuristic Andrew has just done with a function that uses the rjson package to pull down data from the JSON version of the database. If you wanted to run a lot of these, you could compile your own local version of the database provided you have a Python interpreter, as described on the GitHub page.

You know, I’m not sure that really is Kingston University’s servers. But it is within a few miles. Hang on, “Milk Marketing Board Sports Ground”? Is it still 1973?

I couldn’t resist playing around with it. As you see above, the Google Map on the home page looks up your own IP. In my case, I was lucky enough to be able to spot a problem. This location is a little residential street, and certainly doesn’t look like the location of some big university server. In fact, the database returns the “city” as Kingston (sounds good so far) and the “region_name” as St Helens (errr, not really, that’s a few hundred miles north west from here). But Google Maps has found an apartment block called St Helens, near Kingston. Oh dear.

So, lesson 1: don’t trust the Google Map search on the home page.

However, there are also latitudes and longitudes, so I went to my website at (WordPress jealously guard the IP addresses of visitors; as ever, data=$$$) and got all the recent IPs of visitors. I shoved these into Heuristic Andrew’s function and found four that returned an Error 404 from the database. That’s not Andrew’s fault, it seems the database just doesn’t know them. As he says, the function is very new and needs some better error handling. I’m sure that will come in time. For now, I just ditched those four and carried on. The country names supplied to me by my ISP matched the freegeoip ones perfectly, so I took the latitudes and longitudes and put them in a map:

How did I get the data into a JavaScript map so quickly? That's another story - coming soon.
How did I get the data into a JavaScript map so quickly? That’s another story – coming soon. Sorry this is a fixed image but that’s the price of easy blogging.

Now the Kingston University server is apparently located near Stoke-on-Trent, in fact near the village of Kingstone (hmmm…), at a location which is not exactly an internet hub:

Much nicer than a server room
Much nicer than a server room

So, lesson number 2: don’t trust the latitude and longitude too much, although most of them seem fine.

To take a less pathological example: a lot of traffic has come my way from Benton, Kentucky. (Thank you, whoever you are. I hope you found the website useful.) The (latitude,longitude) is given by freegeoip as (36.8596,-88.3367) which is in a field a mile out of town. The 4 decimal place precision is clearly rather spurious.



  1. Sounds like it’s one of those things that’s cool, but not really accurate enough to base business decisions on.

    1. Yes absolutely, although being able to quickly see that you’re in San Francisco and not just “USA” is handy. Matching to particular pages, times of day, could all give some handy extra insights.

  2. One of the things I discovered when I had a range of IPs that I used “Heuristic Andrew’s” code (Many thanks for the link) in Knitr to derive Lat/Long positions from, that the long list of varied IPs became a very short list of identical locations. Even though the locations tended to be out by some kilometers, they were precisely the same for different IP addresses. I am glad I saw this page.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s