We use the Google Places API prediction similar as in the example app. On selection of a result we get the details for the predicted location using:
Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId);
This works fine, my problem is that the result contains just a String as address.
When using the JS version of the API, the result contains this:
"address_components" : [
{
"long_name" : "522",
"short_name" : "522",
"types" : [ "street_number" ]
},
{
"long_name" : "Overtoom",
"short_name" : "Overtoom",
"types" : [ "route" ]
},
{
"long_name" : "Amsterdam-West",
"short_name" : "Amsterdam-West",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Amsterdam",
"short_name" : "Amsterdam",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Amsterdam",
"short_name" : "Amsterdam",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Noord-Holland",
"short_name" : "NH",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Nederland",
"short_name" : "NL",
"types" : [ "country", "political" ]
},
{
"long_name" : "1054",
"short_name" : "1054",
"types" : [ "postal_code_prefix", "postal_code" ]
}
],
But the above information inside address_components
is missing when using the API on Android.
How do I get this information on Android using the Google places API?
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.
Save this answer. Show activity on this post. In order to have access to more than 5 reviews with the Google API you have to purchase Premium data Access from Google. That premium plan will grant you access to all sorts of additional data points you have to shell out a pretty penny.
Unfortunately the answer is "You can't."
Google does not supply structured response data to the Android implementation of that API.
You could try parsing the address text, but as per my answer to a related post, this is far from trivial:
- You figure out a clever way to parse that string. Unfortunately the actual street/suburb/town/province structure is not consistent across different areas (e.g. Cape Town in South Africa has a different sentence structure to Johannesburg in South Africa). So your parsing rules need to be very clever.
- You use a different Google API. The JavaScript API provides structured data for the related call. Unfortunately Google recommends against using this technique.
A colleague of mine has logged this issue with Google:
Places SDK for Android now supports ADDRESS_COMPONENTS in Field... :-)
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