Java library to get geo-code from ipaddress

后端 未结 3 980
难免孤独
难免孤独 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("");
    //Note the $.MODULE$ syntax to refer to the case class in Scala.
    System.out.println(geoip.getGeoRaw("", 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

提交回复
热议问题