问题
What is the best way to get geo-location in Java (freely if possible)?
Update: Not from a GPS device. Basically how Firefox 3.5 / HTML 5 does it
回答1:
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 .
回答2:
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/.
回答3:
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)
回答4:
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.
回答5:
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.
回答6:
you could try http://code.google.com/p/mygeoloc/
回答7:
I recommend using http://jgeocoder.sourceforge.net/ if you use svn jgeocoder - Free Java Geocoder
回答8:
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)
回答9:
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));
回答10:
One of the best client I used so far is from Ipregistry:
https://github.com/ipregistry/ipregistry-java
It has native support for caching, works with Java 8+ and dependencies are available on Maven Central. Also, error handling is nicely designed.
回答11:
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.
回答12:
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
}
来源:https://stackoverflow.com/questions/1415851/best-way-to-get-geo-location-in-java