如何在 android 中使用地理编码 API (JSON)?

How to use geocoding API (JSON) in android?

我刚开始使用 json 数据,对如何从该地理编码数据中获取我想要的内容感到困惑。我想要做的是使用用户的经纬度坐标获取城市和州的名称。下面是一些示例数据:

{
   "results" : [
  {
     "address_components" : [
        {
           "long_name" : "277",
           "short_name" : "277",
           "types" : [ "street_number" ]
        },
        {
           "long_name" : "Bedford Avenue",
           "short_name" : "Bedford Ave",
           "types" : [ "route" ]
        },
        {
           "long_name" : "Williamsburg",
           "short_name" : "Williamsburg",
           "types" : [ "neighborhood", "political" ]
        },
        {
           "long_name" : "Brooklyn",
           "short_name" : "Brooklyn",
           "types" : [ "sublocality_level_1", "sublocality", "political" ]
        },
        {
           "long_name" : "Kings County",
           "short_name" : "Kings County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "New York",
           "short_name" : "NY",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "11211",
           "short_name" : "11211",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "277 Bedford Ave, Brooklyn, NY 11211, USA",
     "geometry" : {
        "location" : {
           "lat" : 40.714232,
           "lng" : -73.9612889
        },
        "location_type" : "ROOFTOP",
        "viewport" : {
           "northeast" : {
              "lat" : 40.7155809802915,
              "lng" : -73.9599399197085
           },
           "southwest" : {
              "lat" : 40.7128830197085,
              "lng" : -73.96263788029151
           }
        }
     },
     "place_id" : "ChIJd8BlQ2BZwokRAFUEcm_qrcA",
     "types" : [ "street_address" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Grand St/Bedford Av",
           "short_name" : "Grand St/Bedford Av",
           "types" : [ "point_of_interest", "establishment" ]
        },
        {
           "long_name" : "Williamsburg",
           "short_name" : "Williamsburg",
           "types" : [ "neighborhood", "political" ]
        },
        {
           "long_name" : "Brooklyn",
           "short_name" : "Brooklyn",
           "types" : [ "sublocality_level_1", "sublocality", "political" ]
        },
        {
           "long_name" : "Kings County",
           "short_name" : "Kings County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "New York",
           "short_name" : "NY",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        },
        {
           "long_name" : "11211",
           "short_name" : "11211",
           "types" : [ "postal_code" ]
        }
     ],
     "formatted_address" : "Grand St/Bedford Av, Brooklyn, NY 11211, USA",
     "geometry" : {
        "location" : {
           "lat" : 40.714321,
           "lng" : -73.961151
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 40.71566998029149,
              "lng" : -73.95980201970849
           },
           "southwest" : {
              "lat" : 40.7129720197085,
              "lng" : -73.96249998029151
           }
        }
     },
     "place_id" : "ChIJi27VXGBZwokRM8ErPyB91yk",
     "types" : [
        "bus_station",
        "transit_station",
        "point_of_interest",
        "establishment"
     ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Williamsburg",
           "short_name" : "Williamsburg",
           "types" : [ "neighborhood", "political" ]
        },
        {
           "long_name" : "Brooklyn",
           "short_name" : "Brooklyn",
           "types" : [ "sublocality_level_1", "sublocality", "political" ]
        },
        {
           "long_name" : "New York",
           "short_name" : "New York",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Kings County",
           "short_name" : "Kings County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "New York",
           "short_name" : "NY",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Williamsburg, Brooklyn, NY, USA",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 40.7251773,
              "lng" : -73.936498
           },
           "southwest" : {
              "lat" : 40.6979329,
              "lng" : -73.96984499999999
           }
        },
        "location" : {
           "lat" : 40.7081156,
           "lng" : -73.9570696
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 40.7251773,
              "lng" : -73.936498
           },
           "southwest" : {
              "lat" : 40.6979329,
              "lng" : -73.96984499999999
           }
        }
     },
     "place_id" : "ChIJQSrBBv1bwokRbNfFHCnyeYI",
     "types" : [ "neighborhood", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Brooklyn",
           "short_name" : "Brooklyn",
           "types" : [ "sublocality_level_1", "sublocality", "political" ]
        },
        {
           "long_name" : "New York",
           "short_name" : "New York",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Kings County",
           "short_name" : "Kings County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "New York",
           "short_name" : "NY",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Brooklyn, NY, USA",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 40.739446,
              "lng" : -73.83336509999999
           },
           "southwest" : {
              "lat" : 40.551042,
              "lng" : -74.05663
           }
        },
        "location" : {
           "lat" : 40.6781784,
           "lng" : -73.94415789999999
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 40.739446,
              "lng" : -73.83336509999999
           },
           "southwest" : {
              "lat" : 40.551042,
              "lng" : -74.05663
           }
        }
     },
     "place_id" : "ChIJCSF8lBZEwokRhngABHRcdoI",
     "types" : [ "sublocality_level_1", "sublocality", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "New York",
           "short_name" : "New York",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "New York",
           "short_name" : "NY",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "New York, NY, USA",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 40.91525559999999,
              "lng" : -73.70027209999999
           },
           "southwest" : {
              "lat" : 40.4960439,
              "lng" : -74.25573489999999
           }
        },
        "location" : {
           "lat" : 40.7127837,
           "lng" : -74.0059413
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 40.91525559999999,
              "lng" : -73.70027209999999
           },
           "southwest" : {
              "lat" : 40.4960439,
              "lng" : -74.25573489999999
           }
        }
     },
     "place_id" : "ChIJOwg_06VPwokRYv534QaPC8g",
     "types" : [ "locality", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "11211",
           "short_name" : "11211",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "Brooklyn",
           "short_name" : "Brooklyn",
           "types" : [ "sublocality_level_1", "sublocality", "political" ]
        },
        {
           "long_name" : "New York",
           "short_name" : "New York",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "New York",
           "short_name" : "NY",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Brooklyn, NY 11211, USA",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 40.7280089,
              "lng" : -73.9207299
           },
           "southwest" : {
              "lat" : 40.7008331,
              "lng" : -73.9644697
           }
        },
        "location" : {
           "lat" : 40.7093358,
           "lng" : -73.9565551
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 40.7280089,
              "lng" : -73.9207299
           },
           "southwest" : {
              "lat" : 40.7008331,
              "lng" : -73.9644697
           }
        }
     },
     "place_id" : "ChIJvbEjlVdZwokR4KapM3WCFRw",
     "types" : [ "postal_code" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "Kings County",
           "short_name" : "Kings County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "New York",
           "short_name" : "NY",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Kings County, NY, USA",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 40.7391407,
              "lng" : -73.83336509999999
           },
           "southwest" : {
              "lat" : 40.5702197,
              "lng" : -74.04195919999999
           }
        },
        "location" : {
           "lat" : 40.6528762,
           "lng" : -73.95949399999999
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 40.7391407,
              "lng" : -73.83336509999999
           },
           "southwest" : {
              "lat" : 40.5702197,
              "lng" : -74.04195919999999
           }
        }
     },
     "place_id" : "ChIJOwE7_GTtwokRs75rhW4_I6M",
     "types" : [ "administrative_area_level_2", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "New York",
           "short_name" : "NY",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "New York, USA",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 45.015865,
              "lng" : -71.85620639999999
           },
           "southwest" : {
              "lat" : 40.496091,
              "lng" : -79.76214379999999
           }
        },
        "location" : {
           "lat" : 43.2994285,
           "lng" : -74.21793260000001
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 45.015859,
              "lng" : -71.85620639999999
           },
           "southwest" : {
              "lat" : 40.496091,
              "lng" : -79.76214379999999
           }
        }
     },
     "place_id" : "ChIJqaUj8fBLzEwRZ5UY3sHGz90",
     "types" : [ "administrative_area_level_1", "political" ]
  },
  {
     "address_components" : [
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "United States",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 71.3867745,
              "lng" : -66.9502861
           },
           "southwest" : {
              "lat" : 18.9106768,
              "lng" : 172.4458955
           }
        },
        "location" : {
           "lat" : 37.09024,
           "lng" : -95.712891
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 49.38,
              "lng" : -66.94
           },
           "southwest" : {
              "lat" : 25.82,
              "lng" : -124.39
           }
        }
     },
     "place_id" : "ChIJCzYy5IS16lQRQrfeQ5K5Oxw",
     "types" : [ "country", "political" ]
  }
  ],
   "status" : "OK"
}

p.s。我在 android 中这样做。

实际上这是您的地理编码数据。

    "viewport" : {
       "northeast" : {
          "lat" : 40.71566998029149,
          "lng" : -73.95980201970849
       },
       "southwest" : {
          "lat" : 40.7129720197085,
          "lng" : -73.96249998029151
       }
    }

我想这是地图的TOP, RIGHTBOTTOM, RIGHT视点。

我猜你只需要给定 JSON 中的 formatted_address 值,所以试试这个

                 try {
                        JSONObject jsonObject = new JSONObject(resultantJSONString);
                        if (jsonObject.getString("status").equals("OK")) {
                            jsonObject = jsonObject.getJSONArray("results")
                                    .getJSONObject(0);
                            String formattedAddress = jsonObject.getString("formatted_address");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }