如何从坐标中获取街道名称?
问题描述:
我将经度和纬度分为两个单独的EditText,我希望当我按下按钮时,街道名称出现在另一个EditText中.
I have the longitude and latitude into two separate EditText I want that when I press a button the street name appears in another EditText.
我尝试了Public Address getAddressForLocation
方法,但是我没有使它起作用.
I tried with the Public Address getAddressForLocation
method, but I have not gotten it to work..
代码
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);
for (int i = 0; i < addresses.getMaxAddressLineIndex(); i++) {
Log.d("=Adress=",addresses.getAddressLine(i));
}
}
如何从坐标中获取街道名称?
How to get Street name from coordinates?
更新(解决方案)
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder();
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("");
}
et_lugar.setText(strReturnedAddress.toString());
}
else {
et_lugar.setText("No Address returned!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
et_lugar.setText("Canont get Address!");
}
谢谢
答
解决方案
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder();
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("");
}
et_lugar.setText(strReturnedAddress.toString());
}
else {
et_lugar.setText("No Address returned!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
et_lugar.setText("Canont get Address!");
}