问题
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?
回答1:
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:
- Issue 10019: Expose address components in Place object
来源:https://stackoverflow.com/questions/34381323/how-to-parse-address-from-google-places-geodataapi-for-android-address-compone