无法使用 Geocoder 获取城市名称

Unable to get city name using Geocoder

您好,我想从我当前的位置获取城市名称,但不幸的是我无法获取。

请问有没有更好的方法? 谢谢 这是代码:

//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.");
    }

我认为 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 的数据不是那么确定。