【问题标题】:Unable to get city name using Geocoder无法使用地理编码器获取城市名称
【发布时间】:2015-09-18 20:55:00
【问题描述】:

您好,我正在尝试从当前位置获取城市名称,但不幸的是我无法做到。

我能知道是否有更好的方法吗? 谢谢 这是代码:

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

【问题讨论】:

  • 您尝试过其他地点吗? Address 不保证它会拥有所有数据。 See the docs

标签: android google-geocoder


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多