Skip to content Skip to sidebar Skip to footer

How Can I Get Place Details From Google Places Api For Android?

I want to get details of places (cityName, ZipCode, etc) from predictions that I get from Autocomplete Places service. My code is like the following: Places.GeoDataApi.getAuto

Solution 1:

You'll need to Geocode the place results to get back that information.


Places.GeoDataApi.getPlaceById(googleApiClient, placeId)
    .setResultCallback(new ResultCallback<PlaceBuffer>() {

        @Override
        public void onResult(PlaceBuffer places) {

            if (!places.getStatus().isSuccess()) {
                // Request did not complete successfully
                return;
            }

            // Setup Geocoder
            Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
            List<Address> addresses;

            // Attempt to Geocode from place lat & long
            try {

                addresses = geocoder.getFromLocation(
                    place.getLatLng().latitude, 
                    place.getLatLng().longitude, 
                    1);

                if (addresses.size() > 0) {

                    // Here are some results you can geocode
                    String ZIP;
                    String city;
                    String state;
                    String country;

                    if (addresses.get(0).getPostalCode() != null) {
                        ZIP = addresses.get(0).getPostalCode();
                        Log.d("ZIP", ZIP);
                    }

                    if (addresses.get(0).getLocality() != null) {
                        city = addresses.get(0).getLocality();
                        Log.d("city", city);
                    }

                    if (addresses.get(0).getAdminArea() != null) {
                        state = addresses.get(0).getAdminArea();
                        Log.d("state", state);
                    }

                    if (addresses.get(0).getCountryName() != null) {
                        country = addresses.get(0).getCountryName();
                        Log.d("country", country);
                    }

                }

            } catch (IOException e) {
                e.printStackTrace();
            }

            places.release();
        }
    });

Post a Comment for "How Can I Get Place Details From Google Places Api For Android?"