Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse Address from Google Places GeoDataApi for Android, (address_components missing)

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?

like image 436
Tom Bevelander Avatar asked Dec 20 '15 13:12

Tom Bevelander


People also ask

How do I get a city from 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.

How do I get more than 5 reviews on Google Places API?

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.


2 Answers

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:

  1. 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.
  2. 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:

  • Issue 10019: Expose address components in Place object
like image 107
Richard Le Mesurier Avatar answered Sep 22 '22 21:09

Richard Le Mesurier


Places SDK for Android now supports ADDRESS_COMPONENTS in Field... :-)

like image 20
Ido Sofi Avatar answered Sep 22 '22 21:09

Ido Sofi