Places API for Android: get address lines from Place.getAddress()

元气小坏坏 提交于 2019-12-30 17:23:07

问题


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()?


回答1:


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:

  • Issue 10019: Expose address components in Place object

Longer answer is that your options are:

  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. This related question shows more details on that API. Unfortunately Google recommends against using this technique.

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.

  • often you can take address A, resolve it to a given lat/lng, and resolve that to another address B, which might be close to A, but is not. The API calls are not commutative as one would expect

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.




回答2:


Now it is impossible with this API only. But you can use Geocoder with latitude and longitude from Place object




回答3:


Note: This information was observed at this post. Repeated below.

Actually you can achieve this, you will just need to use the GeoCoder to do the lookup. Then simply use the Address object to get the data you need.

Here is some Java code to showcase the solution.

 List<Address> addresses = null;
addresses = geocoder.getFromLocation(latitude, longitude,1);
if(addresses != null && addresses.size() > 0 ){
   Address address = addresses.get(0);
   // Thoroughfare seems to be the street name without numbers
        String street = address.getThoroughfare();
}


来源:https://stackoverflow.com/questions/39181056/places-api-for-android-get-address-lines-from-place-getaddress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!