你好我想从我当前的位置获取城市名称,但不幸的是我无法做到。
我能否知道是否有更好的方法呢? 谢谢 这是代码:
//Getting city name
try{
Geocoder gcd = new Geocoder(this, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
String city = addresses.get(0).getLocality();
Log.v(TAG, "City = " + city);
mLocationLabel.setText(city);
//System.out.println(addresses.get(0).getLocality());
}
} catch (IOException e){
Log.e(TAG, "Failed to get city name.", e);
Log.v(TAG, "Failed to get city name.");
}
答案 0 :(得分:4)
我认为Geocoder的数据取决于位置。
我会得到相当多的测试结果。像这样:
try {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 10);
if (!addressList.isEmpty()) {
for (Address address : addressList) {
String result = address.getAdminArea() != null ? address.getAdminArea() : "?";
result += " | ";
result += address.getSubAdminArea() != null ? address.getSubAdminArea() : "?";
result += " | ";
result += address.getLocality() != null ? address.getLocality() : "?";
result += " | ";
result += address.getSubLocality() != null ? address.getSubLocality() : "?";
result += " | ";
result += address.getThoroughfare() != null ? address.getThoroughfare() : "?";
result += " | ";
result += address.getSubThoroughfare() != null ? address.getSubThoroughfare() : "?";
Log.i(TAG, result);
}
}
} catch (IOException e) {
Log.e(TAG, "Failed Geocoding.");
}
我已经验证了Geocoder的数据不太确定。