我有一个地址名字我想得到一个准确的纬度
问题是,我得到的结果总是空的-不像我们得到的结果https://maps.google.com/.这总是让我得到一些结果,不像Geocoder。
我还尝试了另一种方式:"http://maps.google.com/maps/api/geocode/json?address="地址"
我的问题是:我们如何从代码中搜索https://maps.google.com/得到与java相同的latlong结果?
附加:关于使用"http://maps.google.com/maps/api/geocode/json?address="地址的api文档在哪里"
Albert,我认为你担心的是你的代码不起作用。下面的代码对我来说非常有效。我认为你缺少URIUtil. codeQuery
将字符串转换为URI。
我正在使用gson库,下载并将其添加到您的路径中。
要获得gson解析的类,您需要转到jsonschema2pojo。只需http://maps.googleapis.com/maps/api/geocode/json?address=Sayaji酒店附近balewadi体育场浦那
相信我,工作很容易。不要沮丧。
try {
URL url = new URL(
"http://maps.googleapis.com/maps/api/geocode/json?address="
+ URIUtil.encodeQuery("Sayaji Hotel, Near balewadi stadium, pune") + "&sensor=true");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String output = "", full = "";
while ((output = br.readLine()) != null) {
System.out.println(output);
full += output;
}
PincodeVerify gson = new Gson().fromJson(full, PincodeVerify.class);
response = new IsPincodeSupportedResponse(new PincodeVerifyConcrete(
gson.getResults().get(0).getFormatted_address(),
gson.getResults().get(0).getGeometry().getLocation().getLat(),
gson.getResults().get(0).getGeometry().getLocation().getLng())) ;
try {
String address = response.getAddress();
Double latitude = response.getLatitude(), longitude = response.getLongitude();
if (address == null || address.length() <= 0) {
log.error("Address is null");
}
} catch (NullPointerException e) {
log.error("Address, latitude on longitude is null");
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Geocode超文本传输协议有效,我刚刚启动它,结果如下
{
"results" : [
{
"address_components" : [
{
"long_name" : "Pune",
"short_name" : "Pune",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Pune",
"short_name" : "Pune",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Maharashtra",
"short_name" : "MH",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Pune, Maharashtra, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 18.63469650,
"lng" : 73.98948670
},
"southwest" : {
"lat" : 18.41367390,
"lng" : 73.73989109999999
}
},
"location" : {
"lat" : 18.52043030,
"lng" : 73.85674370
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 18.63469650,
"lng" : 73.98948670
},
"southwest" : {
"lat" : 18.41367390,
"lng" : 73.73989109999999
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
编辑
关于“答案不包含足够的细节”
从所有的研究中,你都期望谷歌地图对地方、地区、城市的每一个组合都有参考。但事实仍然是,谷歌地图在自己的上下文中包含地理和反向地理。你不能指望它有一个像Sayaji Hotel、balewadi体育场附近、浦那
这样的组合。网络谷歌地图会为你定位它,因为它使用了更广泛的搜索
丰富的谷歌后端。谷歌api从他们自己的api收到的唯一反向地理地址。对我来说,这似乎是一个合理的工作方式,考虑到我们的印度地址系统有多复杂,第二个十字路口可能离第一个十字路口有几英里远:)
希望这对你有帮助:
public static GeoPoint getGeoPointFromAddress(String locationAddress) {
GeoPoint locationPoint = null;
String locationAddres = locationAddress.replaceAll(" ", "%20");
String str = "http://maps.googleapis.com/maps/api/geocode/json?address="
+ locationAddres + "&sensor=true";
String ss = readWebService(str);
JSONObject json;
try {
String lat, lon;
json = new JSONObject(ss);
JSONObject geoMetryObject = new JSONObject();
JSONObject locations = new JSONObject();
JSONArray jarr = json.getJSONArray("results");
int i;
for (i = 0; i < jarr.length(); i++) {
json = jarr.getJSONObject(i);
geoMetryObject = json.getJSONObject("geometry");
locations = geoMetryObject.getJSONObject("location");
lat = locations.getString("lat");
lon = locations.getString("lng");
locationPoint = Utils.getGeoPoint(Double.parseDouble(lat),
Double.parseDouble(lon));
}
} catch (Exception e) {
e.printStackTrace();
}
return locationPoint;
}
我使用,按顺序:
-谷歌地理编码APIv3(在少数情况下,这里描述了一个问题)
-地理编码器(在某些情况下,这里描述了一个问题)。
如果我没有收到谷歌地理编码的结果API我使用地理编码器。