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

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

Posted by: admin June 15, 2020 Leave a comment

Questions:

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?

How to&Answers:

Places SDK for Android now supports ADDRESS_COMPONENTS in Field… 🙂

Answer:

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: