简体   繁体   English

如何使用 Google 的 GeoCoder Java Client 获取特定地点的经度和纬度?

[英]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.

相关问题 如何使用Google Place API或任何JAVA API从URL获取经度/纬度 - How to get longitude/latitude from an URL using Google Place API or any JAVA API 如何使用经度和纬度获取位置ID? - How do I get place id using latitude and longitude? 如何使用纬度,经度和半径(距离)将区域划分为更小尺寸的区域,在Google中使用Java搜索API? - How to divide the region into smaller size regions using latitude, longitude and radius(distance), in Google place Search API in Java? 如何在Java中使用经度和纬度获取位置? - how to get location using Latitude and Longitude in java? 如何在Android中获取经度和纬度? - How can I get Latitude and longitude in Android? 当我在java中拥有纬度和经度时,如何获取位置的地址? - How to get location's address when i have latitude and longitude in java? 在Java中获取用户当前的经度和经度 - Get user's current latitude and longitude in Java 使用Java和任何外部库,如何将纬度/经度点绘制到地球的图形表示中? - Using Java and any external libraries, how can I draw latitude/longitude points onto a graphical representation of the Earth? 如何在Android中获取经度和纬度方向? - How can I get a direction with latitude and longitude in Android? Google使用纬度和经度将导航映射到特定位置 - Google maps navigation to a particular place using latitude and longitude
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM