【问题标题】:How to use geocoding API (JSON) in android?如何在 android 中使用地理编码 API (JSON)?
【发布时间】:2016-02-15 22:22:38
【问题描述】:

我是使用 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"
}

附言我在安卓中这样做。

【问题讨论】:

    标签: android json api geocoding google-geocoding-api


    【解决方案1】:

    我猜你只需要给定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();
                        }
    

    【讨论】:

    • 谢谢。 .getJSONObject(0) 是我忘记的。
    • 酷..很高兴帮助你:)
    【解决方案2】:

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

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

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

    【讨论】:

    • 好的,但我要访问的信息是城市、州和可能国家的名称。你知道怎么做吗?
    • @Jake 你是什么意思I'm trying to access is the name of the city, state and possibly country?所有这些都存在于您的 JSON 中。
    • 是的,我就是这个意思。我知道它存在于 JSON 中,但我不知道如何访问它。对我来说问题是有多个 address_components 数组和 long_name 键,我不知道如何获取特定的数据。
    • @Jake 不要把我当作粗略的,但在问这个之前你需要先接触 JSON 语言。至于我,我建议你看看 Gson 图书馆。它让你的生活更轻松:)
    • 我来看看 Gson。感谢您的建议
    猜你喜欢
    • 2023-03-29
    • 2018-11-24
    • 2015-03-28
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多