首页 文章

从Android上的Google Place API获取城市名称和邮政编码

提问于
浏览
15

我正在使用带有自动完成功能的Google Place API for Android

一切正常,但当我得到如here所示的结果时,我没有城市和邮政编码信息 .

private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
        = new ResultCallback<PlaceBuffer>() {
    @Override
    public void onResult(PlaceBuffer places) {
        if (!places.getStatus().isSuccess()) {
            // Request did not complete successfully
            Log.e(TAG, "Place query did not complete. Error: " + places.getStatus().toString());

            return;
        }
        // Get the Place object from the buffer.
        final Place place = places.get(0);

        // Format details of the place for display and show it in a TextView.
        mPlaceDetailsText.setText(formatPlaceDetails(getResources(), place.getName(),
                place.getId(), place.getAddress(), place.getPhoneNumber(),
                place.getWebsiteUri()));

        Log.i(TAG, "Place details received: " + place.getName());
    }
};

Place类不包含该信息 . 我可以获得完整的人类可读地址,lat和long等 .

如何从自动填充结果中获取城市和邮政编码?

5 回答

  • 11

    您通常无法从地方检索城市名称,
    但你可以通过这种方式轻松获得它:
    1)从你的地方获取坐标(或者你得到它们);
    2)使用Geocoder按坐标检索城市 .
    它可以这样做:

    private Geocoder mGeocoder = new Geocoder(getActivity(), Locale.getDefault());
    
    // ... 
    
     private String getCityNameByCoordinates(double lat, double lon) throws IOException {
    
         List<Address> addresses = mGeocoder.getFromLocation(lat, lon, 1);
         if (addresses != null && addresses.size() > 0) {
             return addresses.get(0).getLocality();
         }
         return null;
     }
    
  • 9

    可以分两步检索城市名称和邮政编码

    1)对https://maps.googleapis.com/maps/api/place/autocomplete/json?key=API_KEY&input=your_inpur_char进行Web服务调用 . JSON包含 place_id 字段,可在步骤2中使用 .

    2)对https://maps.googleapis.com/maps/api/place/details/json?key=API_KEY&placeid=place_id_retrieved_in_step_1进行另一次Web服务调用 . 这将返回包含 address_components 的JSON . 循环通过 types 找到 localitypostal_code 可以给你城市名称和邮政编码 .

    实现它的代码

    JSONArray addressComponents = jsonObj.getJSONObject("result").getJSONArray("address_components");
            for(int i = 0; i < addressComponents.length(); i++) {
                JSONArray typesArray = addressComponents.getJSONObject(i).getJSONArray("types");
                for (int j = 0; j < typesArray.length(); j++) {
                    if (typesArray.get(j).toString().equalsIgnoreCase("postal_code")) {
                        postalCode = addressComponents.getJSONObject(i).getString("long_name");
                    }
                    if (typesArray.get(j).toString().equalsIgnoreCase("locality")) {
                        city = addressComponents.getJSONObject(i).getString("long_name")
                    }
                }
            }
    
  • 0

    很遗憾,目前无法通过Android API获取此信息 .

    它可以使用Places API Web服务(https://developers.google.com/places/webservice/) .

  • 24
    try{
        getPlaceInfo(place.getLatLng().latitude,place.getLatLng().longitude);
    catch (Exception e){
        e.printStackTrace();
    }
    

    // ......

    private void getPlaceInfo(double lat, double lon) throws IOException {
            List<Address> addresses = mGeocoder.getFromLocation(lat, lon, 1);
            if (addresses.get(0).getPostalCode() != null) {
                String ZIP = addresses.get(0).getPostalCode();
                Log.d("ZIP CODE",ZIP);
            }
    
            if (addresses.get(0).getLocality() != null) {
                String city = addresses.get(0).getLocality();
                Log.d("CITY",city);
            }
    
            if (addresses.get(0).getAdminArea() != null) {
                String state = addresses.get(0).getAdminArea();
                Log.d("STATE",state);
            }
    
            if (addresses.get(0).getCountryName() != null) {
                String country = addresses.get(0).getCountryName();
                Log.d("COUNTRY",country);
            }
        }
    
  • 1

    不是最好的方法,但以下内容可能很有用:

    Log.i(TAG, "Place city and postal code: " + place.getAddress().subSequence(place.getName().length(),place.getAddress().length()));
    

相关问题