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?
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 the types to find locality and postal_code can give you the city name and postal code.
The autocomplete widget is a search dialog with built-in autocomplete functionality. As a user enters search terms, the widget presents a list of predicted places to choose from. When the user makes a selection, a Place instance is returned, which your app can then use to get details about the selected place.
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; }
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 the types
to find locality
and postal_code
can give you the city name and postal code.
Code to achieve it
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") } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With