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?
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:
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;
}