In my android app, I find the address of locations using Geocoder.getFromLocation()
. I can get an Address
object and get the address lines of an address with Address.getAddressLine()
.
I am only interested in the first address line, which I can get easily with Address.getAddressLine(0)
.
Also, app allows user to choose locations using PlaceAutocomplete
. When user chooses a location, I receive a Place
object and I can get the address by Place.getAddress()
.
The problem is, Place.getAddress()
returns a string instead of an Address
object. This string contains the full address, so I cannot easily get the first address line, as I did with Address
object.
How should I find the first address line from string returned by Place.getAddress()
?
Getting startedGo to the Google Cloud Console. Click the Select a project button, then select the same project you set up for the Maps JavaScript API and click Open. From the list of APIs on the Dashboard, look for Places API. If you see the API in the list, you're all set.
The short story is you need to do: Geocoder geocoder = new Geocoder(this, Locale. getDefault()); List<Address> addresses = geocoder. getFromLocation(lat, lng, 1);
Now it is impossible with this API only. But you can use Geocoder with latitude and longitude from Place object
Short answer is that you can't get structured data from that API. For some reason, Google has decided that is not something people would want on Android.
A colleague of mine has logged this issue with Google to request the same level of information as is provided to iOS and JavaScript users:
Longer answer is that your options are:
I believe that Google only intends to give us this sentence for the purposes of picking an address, rather than for us to get structured information about the address. In order to get better information, you need to get the lat/lng values by querying the place ID.
From those lat/lng values, you can then reverse-geolocate to get the correct address.
Unfortunately in this technique, Google's APIs fail us once more.
I have had a fair amount of experience with these calls, and I wish there was another answer I could provide. If you wish to stick with Google's location APIs, then you can't get what you are asking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With