我将经度和纬度分为两个单独的EditText,我希望当我按下按钮时,街道名称会出现在另一个EditText中。
我尝试了这种Public Address getAddressForLocation
方法,但是还没有开始工作。
代码
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));
}
}
如何从坐标中获取街道名称?
更新(解决方案)
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!");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句