Android Places API: get addres strings from Place.getAddress ()

In my Android app, I found the location address using Geocoder.getFromLocation() . I can get the Address object and get the address lines of the address using Address.getAddressLine() .

I'm only interested in the first line of the address, which I can easily get with Address.getAddressLine(0) .

In addition, the application allows the user to select locations using PlaceAutocomplete . When the user selects a location, I get a Place object, and I can get the address of Place.getAddress() .

The problem is that Place.getAddress() returns a string instead of an Address object. This line contains the full address, so I cannot easily get the first address line, as it was with the Address object.

How do I find the first line of an address from the line returned by Place.getAddress() ?

+6
source share
2 answers

Short answer: you cannot get structured data from this API . For some reason, Google has decided that this is not what people want on Android.

My colleague reported this issue with Google in order to request the same level of information that is provided to iOS and JavaScript users:


The longer answer is that your options are:

  • You understand the clever way to parse this line. Unfortunately, the actual structure of streets / suburbs / cities / provinces is not consistent in different areas (for example, Cape Town in South Africa has a different proposal structure in Johannesburg in South Africa). Therefore, your parsing rules should be very smart.
  • You are using a different Google API. The JavaScript API provides structured data for the corresponding call. This related question shows more details about this API. Unfortunately, Google recommends using this technique.

I believe that Google intends to give us this offer for the purpose of selecting an address, and not for obtaining structured address information. To get the best information, you need to get the lat / lng values ​​by requesting a place id.

From these lat / lng values, you can flip the geolocation to get the correct address.

Unfortunately, Google APIs fail us again in this method.

  • often you can take address A, resolve it to the given lat / lng, and resolve it to another address B, which may be close to A, but that’s not the case. The API call is not commutative, as you might expect

I have had sufficient experience with these calls, and I would like for me to get another answer. If you want to stick with the Google Location APIs, you won’t be able to get what you ask for.

+7
source

Now this is not possible using this API. But you can use Geocoder with latitude and longitude from the Place object

+1
source

All Articles