3

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.getAutocompletePredictions(googleApiClient, query, bounds, null)
            .setResultCallback(
                    new ResultCallback<AutocompletePredictionBuffer>() {
                        @Override
                        public void onResult(AutocompletePredictionBuffer buffer) {
                            if (buffer == null)
                                return;

                            if (buffer.getStatus().isSuccess()) {
                                for (AutocompletePrediction prediction : buffer) {
                                    // How to get cityName here
                                }
                            }
                            buffer.release();
                        }
                    }, 15, TimeUnit.SECONDS);

Is this possible? How can I implement it? If I look for place details by placeId, I can't get that I want neither:

Places.GeoDataApi.getPlaceById(googleApiClient, placeId)
        .setResultCallback(new ResultCallback<PlaceBuffer>() {
            @Override
            public void onResult(PlaceBuffer places) {
                if (places.getStatus().isSuccess()) {
                    // How to get cityName here
                }
                places.release();
            }
        });
NewUser
  • 156
  • 1
  • 9
  • 2
    You can probably use a place ID to retrieve the coordinates, and use the coordinates to retrieve a city name. You can see [this StackOveflow answer](http://stackoverflow.com/a/2296416/4195406) for more details – ztan Nov 23 '15 at 22:27
  • I've already seen that post, but I'm interested in a more direct way. Thanks for your answer! :) – NewUser Nov 24 '15 at 09:42
  • did you find any more direct way? – Darpan Oct 26 '16 at 16:51
  • Not yet, I'm getting the place details in two steps.. – NewUser Oct 27 '16 at 15:38
  • Currently, you have to do at least two requests. Please upvote this feature request - https://issuetracker.google.com/issues/35828699 to make it with a single request. – AlexKorovyansky Feb 01 '18 at 07:17

1 Answers1

5

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();
        }
    });
Michael
  • 9,639
  • 3
  • 64
  • 69