获取纬度和放大器;经度从指定的地址名称。不是地理codeR [英] Get latitude & longitude from given address name. NOT Geocoder
问题描述
我有一个地址的名字,我希望得到一个准确的纬度和放大器;经度它。我知道我们能得到这个使用地理codeR的 getFromLocationName(地址,maxresult)的。
I have an address name and I want to get an accurate latitude & longitude for it. I know we can get this using Geocoder's getFromLocationName(address,maxresult).
现在的问题是,结果我得到的总是空 - 我们与 https://maps.google.com/ <得到的结果不同/ A>。这总是让我获得了一些成果,不像地理codeR。
The problem is, the result I get is always null - unlike the result that we get with https://maps.google.com/. This always allows me to get some results, unlike Geocoder.
我也试过另一种方式:http://maps.google.com/maps/api/geo$c$c/json?address=+地址+&放大器,传感器= FALSE
(这里是链接!)它提供了比地理codeR更好的效果,但往往会返回错误(java.net.SocketException异常:recvfrom的失败:ECONNRESET(连接复位)
这是无聊的。
I also tried another way: "http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false"
(Here's a link!) It gives better results than geocoder, but often returns the error (java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
. It's boring.
我的问题是,:怎样才能得到相同的经纬度结果,我们会通过搜索 HTTPS得到://地图.google.com / 从Java code?
My question is: How can we get the same latlong result we would get by searching on https://maps.google.com/ from within java code?
其他:哪里是在的 API文档的有关使用<一href="http://maps.google.com/maps/api/geo$c$c/json?address=">http://maps.google.com/maps/api/geo$c$c/json?address=" +地址+&放大器,传感器=假
additional:where is the api document about using "http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false"
推荐答案
伟业,我觉得你的问题是,您code不工作。这里有云,在code以下工作对我真的很好。我认为你缺少 URIUtil.en codeQuery
您的字符串转换为一个URI。
Albert, I think your concern is that you code is not working. Here goes, the code below works for me really well. I think you are missing URIUtil.encodeQuery
to convert your string to a URI.
我使用GSON库,下载它,并把它添加到您的路径。
I am using gson library, download it and add it to your path.
要获取类的为您GSON解析,您需要到 jsonschema2pojo 。刚刚火的 <一个href="http://maps.googleapis.com/maps/api/geo$c$c/json?address=Sayaji+Hotel+Near+balewadi+stadium+pune&sensor=true">http://maps.googleapis.com/maps/api/geo$c$c/json?address=Sayaji+Hotel+Near+balewadi+stadium+pune&sensor=true您的浏览器,得到的结果,然后粘贴到这个网站。它会生成你的POJO的为您服务。您可能还需要添加一个annotation.jar。
To get the class's for your gson parsing, you need to goto jsonschema2pojo. Just fire http://maps.googleapis.com/maps/api/geocode/json?address=Sayaji+Hotel+Near+balewadi+stadium+pune&sensor=true on your browser, get the results and paste it on this site. It will generate your pojo's for you. You may need to also add a annotation.jar.
相信我,很容易获得工作。不要沮丧呢。
Trust me, it is easy to get working. Don't get frustrated yet.
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();
}
中的地缘code HTTP的作品,我刚刚解雇了,下面
The Geocode http works, I just fired it, results below
{
"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"
}
修改
在答案不包含足够的detail`
On 'answers do not contain enough detail`
从所有研究,你期待的谷歌地图有提及地方,区域,城市的每个组合。但事实是,谷歌地图包含地缘和反向地理在自己的范围内。你不能指望它有像 Sayaji的酒店,近balewadi体育场,浦
的组合。因为它采用了更广泛的搜索
丰富的谷歌后端Web谷歌地图会找到它。从自己的API收到的谷歌API的唯一反向地理地址。对我来说,这似乎是一个合理的方式进行工作,考虑到我们的印度广播系统多么复杂,第二个跨路可以很远,从第一次十字道:)
From all the research you are expecting that google map have a reference to every combination of locality, area, city. But the fact remains that google map contains geo and reverse-geo in its own context. You cannot expect it to have a combination like Sayaji Hotel, Near balewadi stadium, pune
. Web google maps will locate it for you since it uses a more extensive Search
rich google backend. The google api's only reverse geo address received from their own api's. To me it seems like a reasonable way to work, considering how complex our Indian address system is, 2nd cross road can be miles away from 1st Cross road :)
这篇关于获取纬度和放大器;经度从指定的地址名称。不是地理codeR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!