我正在尝试获取当前位置的街道名称,但我似乎无法得到它.
我用这个方法来检索地址:
public Address getAddressForLocation(Context context, Location location) throws IOException {
if (location == null) {
return null;
}
double latitude = location.getLatitude();
double longitude = location.getLongitude();
int maxResults = 1;
Geocoder gc = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gc.getFromLocation(latitude, longitude, maxResults);
if (addresses.size() == 1) {
return addresses.get(0);
} else {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我可以做类似的事情.address.getLocality()和address.getPostalCode()
但我想要的是街道名称.就像在"Potterstreet 12"中一样.当我打印AddressLine(0)和AddressLine(1)时,我只获得邮政编码,城市和国家.
如何检索我目前所处位置的街道名称?
在我的Android应用程序中,我找到了使用位置的地址Geocoder.getFromLocation().我可以得到一个Address对象并获取地址的地址行Address.getAddressLine().
我只对第一个地址行感兴趣,我可以轻松搞定Address.getAddressLine(0).
此外,应用程序允许用户使用选择位置PlaceAutocomplete.当用户选择一个位置时,我会收到一个Place对象,我可以通过它获取地址Place.getAddress().
问题是,Place.getAddress()返回一个字符串而不是一个Address对象.这个字符串包含完整的地址,所以我不能轻易获得第一个地址行,就像我使用Addressobject一样.
如何从返回的字符串中找到第一个地址行Place.getAddress()?