从 ipaddress 获取地理代码的 Java 库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11298462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java library to get geo-code from ipaddress
提问by Nambi
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.
我的应用程序知道用户的 IP 地址。我们需要识别用户所在的城市、州、国家以及用户的经纬度。
Is there a java library that can do this?
有没有可以做到这一点的java库?
If there is none, what is algorithm or data-source used to convert IP address to geo-graphic location?
如果没有,用于将 IP 地址转换为地理位置的算法或数据源是什么?
回答by Nambi
http://www.maxmind.com/app/developersprovides a local database and java classes to calculate the location information from IP address. I did some testing. The results seem to be good.
http://www.maxmind.com/app/developers提供了一个本地数据库和 java 类来从 IP 地址计算位置信息。我做了一些测试。结果似乎很好。
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
这是我使用的 junit 测试用例的链接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
MaxMind 提供 java 类,但不提供 jars。因此,我创建了一个可以构建代码并生成 jar 的 Maven 项目。这是 GitHub 项目https://github.com/snambi/GeoIP
If you need to use this library in your maven project, add the following dependency
如果你需要在你的maven项目中使用这个库,添加如下依赖
<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
在 pom.xml 中的“repositories”下添加以下存储库配置
<repository>
<id>geoip</id>
<url>http://snambi.github.com/maven/</url>
</repository>
回答by mnuss
Yup: http://www.maxmind.com/app/developers
是的:http: //www.maxmind.com/app/developers
They provide a Java API (among others).
他们提供了一个 Java API(等等)。
回答by Brian
I wrote a simple library in Scala that uses IPInfoDBto 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.
我在 Scala 中编写了一个简单的库,它使用IPInfoDB来获取地理位置。只要您在类路径中包含 scala-library.jar,这也可以从 Java 中使用。您将需要来自 IPInfoDB 的 API 密钥。您可以获得 JSON、原始或 XML 格式的输出。
The code and some more documentation are on my Githuband there is also a jarfor download.
代码和更多文档在我的Github 上,还有一个jar可供下载。
Here's a Java snippet that will get the geo information with "city" resolution:
这是一个 Java 片段,它将以“城市”分辨率获取地理信息:
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.
此示例输出OK;;0.0.0.0;US;UNITED STATES;DISTRICT OF COLUMBIA;WASHINGTON;20001;38.9048;-77.0354;-05:00
原始输出,然后可以对其进行解析。
Also see this question Best way to get geo-location in Java