Java library to get geo-code from ipaddress

后端 未结 3 981
难免孤独
难免孤独 2021-01-14 06:41

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 l

相关标签:
3条回答
  • 2021-01-14 07:15

    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

    0 讨论(0)
  • 2021-01-14 07:35

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

    They provide a Java API (among others).

    0 讨论(0)
  • 2021-01-14 07:42

    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>
    
    0 讨论(0)
提交回复
热议问题