通过Java中的邮政编码查找纬度和经度 [英] Finding Latitude and Longitude via Zip Codes in Java
问题描述
我无法从给定的邮政编码中检索纬度和经度值。我试图通过Servlet执行此操作,即将邮政编码值传递到Servlet,然后Java代码使用Google Geocode API检索纬度和经度值,最好是字符串。
I'm having trouble retrieving latitude and longitude values from a given zip code. I'm attempting to do this via a Servlet, i.e. a zip code value is passed into the Servlet, and the Java code then uses the Google Geocode API to retrieve the latitude and longitude values, preferably in a String.
我已经在网上漫游了一个简单的示例,但似乎有比J更多的Javascript和PHP方法。
I've roamed all over the net for a simple sample, but there seems to be more Javascript and PHP methods for this than Java.
有人可以粘贴一个如何以这种方式提取纬度/经度值的简单样本吗?
Could someone please paste a simple sample of how to extract the lat/long values in this manner?
提前致谢!!
-Rei
推荐答案
好的答案。这是我成功使用的一些代码,用于查询Google Geocode API。它需要与GSon一起工作,但如果你不想使用GSon,你可以手动解码答案:
OK long answer. This is some code I have used succesfully to interrogate Google Geocode API. It requires to work with GSon but alternatively you can probably decode the answers manually if you don't want to use GSon:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import org.slf4j.Logger;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
public class GeoCoder {
private Gson gson = new Gson();
private volatile long lastRequest = 0L;
public GeocodeResponse getLocation(String... addressElements) throws JsonSyntaxException, JsonIOException, MalformedURLException,
IOException {
StringBuilder sb = new StringBuilder();
for (String string : addressElements) {
if (sb.length() > 0) {
sb.append('+');
}
sb.append(URLEncoder.encode(string.replace(' ', '+'), "UTF-8"));
}
String url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" + sb.toString();
// Google limits this web service to 2500/day and 10 requests/s
synchronized (this) {
try {
long elapsed = System.currentTimeMillis() - lastRequest;
if (elapsed < 100) {
try {
Thread.sleep(100 - elapsed);
} catch (InterruptedException e) {
}
}
return gson.fromJson(new BufferedReader(new InputStreamReader(new URL(url).openStream())), GeocodeResponse.class);
} finally {
lastRequest = System.currentTimeMillis();
}
}
}
}
其他类:
GeocodeResponse:
And the other classes: GeocodeResponse:
import java.util.List;
public class GeocodeResponse {
public enum Status {
OK, ZERO_RESULTS, OVER_QUERY_LIMIT, REQUEST_DENIED, INVALID_REQUEST;
}
public static class Result {
public static enum Type {
street_address,
route,
intersection,
political,
country,
administrative_area_level_1,
administrative_area_level_2,
administrative_area_level_3,
colloquial_area,
locality,
sublocality,
neighborhood,
premise,
subpremise,
postal_code,
natural_feature,
airport,
park,
point_of_interest,
post_box,
street_number,
floor,
room;
}
public static class AddressComponent {
private String long_name;
private String short_name;
private Type[] types;
public String getLong_name() {
return long_name;
}
public void setLong_name(String long_name) {
this.long_name = long_name;
}
public String getShort_name() {
return short_name;
}
public void setShort_name(String short_name) {
this.short_name = short_name;
}
public Type[] getTypes() {
return types;
}
public void setTypes(Type[] types) {
this.types = types;
}
}
private String formatted_address;
private List<AddressComponent> address_components;
private Geometry geometry;
private Type[] types;
public Type[] getTypes() {
return types;
}
public void setTypes(Type[] types) {
this.types = types;
}
public String getFormatted_address() {
return formatted_address;
}
public void setFormatted_address(String formatted_address) {
this.formatted_address = formatted_address;
}
public List<AddressComponent> getAddress_components() {
return address_components;
}
public void setAddress_components(List<AddressComponent> address_components) {
this.address_components = address_components;
}
public Geometry getGeometry() {
return geometry;
}
public void setGeometry(Geometry geometry) {
this.geometry = geometry;
}
}
public static class Geometry {
public static enum LocationType {
ROOFTOP, RANGE_INTERPOLATED, GEOMETRIC_CENTER, APPROXIMATE;
}
public static class ViewPort {
private Location northeast;
private Location southwest;
public Location getNortheast() {
return northeast;
}
public void setNortheast(Location northeast) {
this.northeast = northeast;
}
public Location getSouthwest() {
return southwest;
}
public void setSouthwest(Location southwest) {
this.southwest = southwest;
}
}
private Location location;
private LocationType location_type;
private ViewPort viewport;
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public LocationType getLocation_type() {
return location_type;
}
public void setLocation_type(LocationType location_type) {
this.location_type = location_type;
}
public ViewPort getViewport() {
return viewport;
}
public void setViewport(ViewPort viewport) {
this.viewport = viewport;
}
}
private Status status;
private List<Result> results;
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
}
位置:
public class Location {
private double lat;
private double lng;
public Location() {
}
public Location(double lat, double lng) {
this.lat = lat;
this.lng = lng;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng;
}
}
这篇关于通过Java中的邮政编码查找纬度和经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!