Get separate address fields from Google API?
Asked Answered
S

0

6

Google Places API can return details about a particular place, given its ID:

Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId)

The data returned in the callback implements the Place interface, providing access to the address as a comma-separated string using method:

Place.getAddress()

Parsing this string into fields for street, suburb, province, town etc is not reliable as string is not in a consistent format:

  • Ranelagh Rd, Cape Town, South Africa (street, suburb, country)
  • 1st Ave, Kenilworth, Cape Town, 7708, South Africa (street, suburb, town, post code, country)
  • SW 23rd Terrace, Cape Coral, FL 33991, USA (street, town, post code, country)

Is there a reliable way to get parsed address info from Google?

Sanborne answered 19/10, 2015 at 12:51 Comment(5)
I think because there are different place types, so the place format are different, also different countries has slightly different kind of place format. Or you can use the Address class, it has getCountryName() and getPostalCode() methods etc.Testudo
@Testudo any idea how to populate Address by reverse geocoding a lat/lng pair? I see you have a fair amount of Google Maps experience.Sanborne
You can first make a Geocoder object by doing Geocoder geocoder = new Geocoder(this, Locale.getDefault());, then call addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); You put 1 here, so that it will only return 1 result, you can see this documentation for more details.Testudo
@RichardLeMesurier Have you found solution for this?Cudgel
@Cudgel best thing we found was to write a server API that calls the cloud rest functions and parses the fields out. So the mobile device does not call Google directly, but via our server. The experience is actually better this way because we can augment the results with app-specific info.Sanborne

© 2022 - 2024 — McMap. All rights reserved.