Alb*_*ing 9 java android google-maps
我有一个地址名称,我希望得到一个准确的纬度和经度.我知道我们可以使用Geocoder的getFromLocationName(address,maxresult)来获取它.
问题是,我得到的结果总是为空 - 与我们通过https://maps.google.com/获得的结果不同.与Geocoder不同,这总能让我得到一些结果.
我还尝试了另一种方式:( "http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false"这是一个链接!)它提供比地理编码器更好的结果,但经常返回错误(java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer).这很无聊.
我的问题是:如何通过在java代码中搜索https://maps.google.com/获得相同的latlong结果?
附加:关于使用" http://maps.google.com/maps/api/geocode/json?address="+地址+"&sensor = false" 的api文档在哪里
Sid*_*rth 20
艾伯特,我认为你担心的是你的代码不起作用.在这里,下面的代码非常适合我.我认为你缺少URIUtil.encodeQuery将你的字符串转换为URI.
我正在使用gson库,下载并将其添加到您的路径中.
要获得用于gson解析的类,您需要转到jsonschema2pojo.只需在浏览器上点击http://maps.googleapis.com/maps/api/geocode/json?address=Sayaji+Hotel+Near+balewadi+stadium+pune&sensor=true,即可获得结果并将其粘贴到此网站上.它会为你生成你的pojo.您可能还需要添加annotation.jar.
相信我,很容易上班.不要感到沮丧.
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();
}
Run Code Online (Sandbox Code Playgroud)
Geocode http工作,我刚解雇它,结果如下
{
"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"
}
Run Code Online (Sandbox Code Playgroud)
编辑
在'答案不包含足够的细节'
从所有的研究中,您期望谷歌地图可以参考当地,地区,城市的每个组合.但事实仍然是谷歌地图在其自己的背景下包含地理和反向地理.你不能指望它有像这样的组合Sayaji Hotel, Near balewadi stadium, pune.Web谷歌地图将为您找到它,因为它使用更广泛的Search丰富的谷歌后端.谷歌api唯一的反向地理地址是从他们自己的api收到的.对我而言,这似乎是一种合理的工作方式,考虑到我们的印度地址系统有多复杂,第二条十字路可以远离第一条十字路:)
| 归档时间: |
|
| 查看次数: |
35601 次 |
| 最近记录: |