[英]How can I get the longitude and latitude of a specific place using Google's GeoCoder Java Client?
I am trying to get the longitude and latitude of a specific address.我正在尝试获取特定地址的经度和纬度。
This is the result of the GeoCoded response with the address I am using as a parameter.这是我使用的地址作为参数的 GeoCoded 响应的结果。
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Pkwy",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4224764,
"lng" : -122.0842499
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4238253802915,
"lng" : -122.0829009197085
},
"southwest" : {
"lat" : 37.4211274197085,
"lng" : -122.0855988802915
}
}
},
"place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
"plus_code": {
"compound_code": "CWC8+W5 Mountain View, California, United States",
"global_code": "849VCWC8+W5"
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
I need to specifically get the longitude and latitude of the place.I am following the code here我需要专门获取该地点的经度和纬度。我正在按照此处的代码进行操作
I need the specific JSON field so that I can use it in my Java Code我需要特定的 JSON 字段,以便我可以在我的 Java 代码中使用它
I've found the solution.我找到了解决方案。
The code in the github page shows that: github页面中的代码显示:
GeoApiContext context = new GeoApiContext.Builder().apiKey("AIza...").build();
GeocodingResult[] results = GeocodingApi.geocode(context,
"1600 Amphitheatre Parkway Mountain View, CA 94043").await();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(results[0].addressComponents));
The 4th line only accesses the "addressComponents" of the JSON String.第 4 行仅访问 JSON 字符串的“addressComponents”。 To get the latitudes, I just had to use the corresponding field names and replace the 4th line of the code above with:
为了获得纬度,我只需要使用相应的字段名称并将上面代码的第 4 行替换为:
System.out.println(gson.toJson(results[0].geometry.location.lat));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.