I'm using Google Place API for Android with autocomplete
Everything works fine, but when I get the result as shown here, I don't have the city and postal code information.
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());
}
};
The Place class doesn't contain that information. I can get the full human readable address, the lat and long, etc.
How can I get the city and postal code from the autocomplete result?
Not the best way, but the following can be useful:
Unfortunately this information isn't available via the Android API at this time.
It is available using the Places API Web Service (https://developers.google.com/places/webservice/).
You can not normally retrieve city name from the Place,
but you can easily get it in this way:
1) Get coordinates from your Place (or however you get them);
2) Use Geocoder to retrieve city by coordinates.
It can be done like this:
//......
onCreate
onActivityResult
City name and postal code can be retrieved in 2 steps
1) Making a web-service call to https://maps.googleapis.com/maps/api/place/autocomplete/json?key=API_KEY&input=your_inpur_char. The JSON contains the
place_id
field which can be used in step 2.2) Make another web-service call to https://maps.googleapis.com/maps/api/place/details/json?key=API_KEY&placeid=place_id_retrieved_in_step_1. This will return a JSON which contains
address_components
. Looping through thetypes
to findlocality
andpostal_code
can give you the city name and postal code.Code to achieve it