Java library to get geo-code from ipaddress

无人久伴 提交于 2019-12-30 10:45:16

问题


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?


回答1:


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>



回答2:


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

They provide a Java API (among others).




回答3:


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



来源:https://stackoverflow.com/questions/11298462/java-library-to-get-geo-code-from-ipaddress

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!