Best way to get geo-location in Java

我的梦境 提交于 2019-11-26 15:27:46

An easy way is with GeoLite (http://dev.maxmind.com/geoip/legacy/geolite/). Because it uses a local database no web service calls are needed and it's much faster for geocoding large numbers of IPs.

Here is how:

Add this Maven artifact:

<dependency>
    <groupId>com.maxmind.geoip</groupId>
    <artifactId>geoip-api</artifactId>
    <version>1.2.11</version>
</dependency>

Download the geolocation data file from http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

Unpack the file into any folder. Then do:

LookupService cl = new LookupService("/var/geolite/GeoLiteCity.dat",
                    LookupService.GEOIP_MEMORY_CACHE | LookupService.GEOIP_CHECK_CACHE);

Location location = cl.getLocation("some ip address");

The result will be in the Location object in the latitude, longitude, city, region and countryCode properties.

Please take a look at their accuracy estimates to ensure it meets the needs of your project: http://www.maxmind.com/en/geolite_city_accuracy .

The biggest database of WiFi-to-location is probably SkyHook Wireless, used by Apple and Google for their geo-location. There are SDKs available - see http://skyhookwireless.com/developers/.

The best open-source library for Geolocation in Java is now http://geo-google.sourceforge.net/index.html, based on the google maps API (you just need a google maps API key to use it)

If you need a local database, you can check out what IPInfoDB has to offer.

If you can use a web service, there are a number out there, but I'm not sure what their Terms of Service allow. A Google search for "ip geolocation lookup web service" turns up some stuff, but many of them are commercial products and you would have to read their ToS carefully to make sure you use them within the guidelines.

Here's an example based on the ipdata.co API which is free upto 1500 requests/day.

Replace 8.8.8.8 with the ip you'd like to lookup

// Maven : Add these dependecies to your pom.xml (java6+)
// <dependency>
//     <groupId>org.glassfish.jersey.core</groupId>
//     <artifactId>jersey-client</artifactId>
//     <version>2.8</version>
// </dependency>
// <dependency>
//     <groupId>org.glassfish.jersey.media</groupId>
//     <artifactId>jersey-media-json-jackson</artifactId>
//     <version>2.8</version>
// </dependency>

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.MediaType;

Client client = ClientBuilder.newClient();
Response response = client.target("https://api.ipdata.co/8.8.8.8?api-key=test")
  .request(MediaType.TEXT_PLAIN_TYPE)
  .header("Accept", "application/json")
  .get();

System.out.println("status: " + response.getStatus());
System.out.println("headers: " + response.getHeaders());
System.out.println("body:" + response.readEntity(String.class));

If I understand well enough, you want to get the geographic location of visitors to your site or web application. The alternatives I know are:

Google Gears has some functions to make GeoLocation(http://code.google.com/apis/gears/api_geolocation.html), but need to install Gears

Using JavaScript functions that call an online service such as IP Location Tools (www dot iplocationtools dot com). The site has examples and even have a video tutorial.

Thys Andries Michels

If you want to know how Firefox 3.5 (or Google Chrome) gets the geolocation, then please take a look here: How Google/Firefox Geolocation API works

Basically, what Firefox 3.5 (as well as Chrome) does is to get the list of nearby Wi-Fi networks and send that list using JSON to a Google webservice, which will then return the approximate coordinates.

By the way, there is no Java involved in this process. To get geolocation from Firefox/Chrome, you just call a few JavaScript methods. (I really hope that you know that Java is different from JavaScript)

You say you're OK with web services, so you could use my API http://ipinfo.io. Here's example JSON output for an IP address:

$ curl ipinfo.io
{
  "ip": "24.6.61.239",
  "hostname": "c-24-6-61-239.hsd1.ca.comcast.net",
  "city": "Mountain View",
  "region": "California",
  "country": "US",
  "loc": "37.3845,-122.0881",
  "org": "AS7922 Comcast Cable Communications, LLC",
  "postal": "94040"
}

See https://ipinfo.io/developers for more details.

Another RESTful web service that I can recommend is ip-api.com. It supports different formats and query types, e.g. http://ip-api.com/json/88.130.57.69 returns

{
    "query": "88.130.57.69",
    "status": "success",
    "country": "Germany",
    "countryCode": "DE",
    "region": "HH",
    "regionName": "Hamburg",
    "city": "Hamburg",
    "district": "",
    "zip": "22041",
    "lat": 53.5741,
    "lon": 10.076,
    "timezone": "Europe/Berlin",
    "isp": "Versatel Deutschland",
    "org": "1&1 Versatel Deutschland GmbH",
    "as": "AS8881 1&1 Versatel Deutschland GmbH",
    "mobile": false,
    "proxy": false
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!