我使用谷歌地图的反向地理编码来获取地址,并且第一次使用地址时,完全放置地址,但现在它只是在任何地址显示纬度和经度,没有名称。
有人能帮助我吗?
我的代码:
private class GetAddressTask extends AsyncTask<Location, Void, String> {
@Override
protected String doInBackground(Location... params) {
Geocoder geocoder = new Geocoder(MainActivity.this);
Location loc = params[0];
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
} catch (IOException e) {
return null;
}
if( addresses != null && addresses.size() > 0 ) {
Address addr = addresses.get(0);
String addressText = addr.getAddressLine(0);
updateLastStreet(addressText, MainActivity.this);
return addressText;
}
return null;
}
}
答案 0 :(得分:1)
您是否遗漏了Android.Manifest文件中的权限,然后添加此
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
尝试这种方法:
public List<Address> getAddress(LatLng point) {
try {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this);
if (point.latitude != 0 || point.longitude != 0) {
addresses = geocoder.getFromLocation(point.latitude ,
point.longitude, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
return addresses;
} else {
Toast.makeText(this, "Coordinates are null",
Toast.LENGTH_SHORT).show();
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}