4

My application knows the IP address of the user. We need to identify the city, state, country of the user and latitude & longitude of the user.

Is there a java library that can do this?

If there is none, what is algorithm or data-source used to convert IP address to geo-graphic location?

Nambi
  • 2,688
  • 8
  • 28
  • 37

3 Answers3

5

http://www.maxmind.com/app/developers provides a local database and java classes to calculate the location information from IP address. I did some testing. The results seem to be good.

bbc.co.uk: 212.58.241.131 : 51.283295,-0.23330688;Tadworth,N7,United Kingdom,null
home: 76.126.242.196 : 37.369705,-122.0214;Sunnyvale,CA,United States,94086
google.com: 74.125.224.133 : 37.419205,-122.0574;Mountain View,CA,United States,94043
tcs.in: 202.71.129.225 : 28.666702,77.216705;Delhi,07,India,null
ebay.com: 66.135.205.13 : 37.280304,-121.956696;Campbell,CA,United States,95008
etrade.com: 12.153.224.22 : 34.091797,-84.2209;Alpharetta,GA,United States,30005
whitehouse.gov: 72.247.136.110 : 42.362595,-71.0843;Cambridge,MA,United States,02142

Here is the link to the junit testcase I used, GeoLiteCityTest.java

MaxMind provides java classes, but not jars. So, I have created a maven project that can build the code and generate jars. Here is the GitHub Project https://github.com/snambi/GeoIP

If you need to use this library in your maven project, add the following dependency

<dependency>
   <groupId>org.geomind</groupId>
   <artifactId>geoip</artifactId>
   <version>1.2.8</version>
</dependency>

Add the following repository configuration, under "repositories" in the pom.xml

<repository>
   <id>geoip</id>
   <url>http://snambi.github.com/maven/</url>
</repository>
Nambi
  • 2,688
  • 8
  • 28
  • 37
2

Yup: http://www.maxmind.com/app/developers

They provide a Java API (among others).

mnuss
  • 104
  • 2
  • (This will only get you lat/long to the city level, though.) – mnuss Jul 02 '12 at 17:40
  • WOW, upvote for this and how do i bookmark this so i can look at it when I get home? I've been working on a network security package that this would be awesome to go with – Matt Westlake Jul 02 '12 at 17:40
  • @mnuss thanks for the link. any idea how they decode the ip address to location? – Nambi Jul 02 '12 at 17:50
  • I did some tests, it is a good library. I'll post the findings as answer. – Nambi Jul 05 '12 at 06:47
2

I wrote a simple library in Scala that uses IPInfoDB to get a geo-location. This can also be used from Java as long as you include scala-library.jar on the classpath. You'll need an API key from IPInfoDB. You can get output in JSON, raw, or XML format.

The code and some more documentation are on my Github and there is also a jar for download.

Here's a Java snippet that will get the geo information with "city" resolution:

GeoIP geoip = new GeoIP("<API Key>");
//Note the $.MODULE$ syntax to refer to the case class in Scala.
System.out.println(geoip.getGeoRaw("<IP Address>", City$.MODULE$));

This example outputs OK;;0.0.0.0;US;UNITED STATES;DISTRICT OF COLUMBIA;WASHINGTON;20001;38.9048;-77.0354;-05:00 which is the raw output which can then be parsed.

Also see this question Best way to get geo-location in Java

Community
  • 1
  • 1
Brian
  • 20,195
  • 6
  • 34
  • 55
  • IPInfoDB is purely a web API. It would be nice if can be done within the application. The previous answer provided a good database. I'll post the details as a separate answer. Thanks for providing another stackoverflow thread. It has good information. It would be good to consolidate everything into one place. – Nambi Jul 05 '12 at 06:54