Android反向地理编码getLocality通常返回null [英] Android reverse geocoding getLocality returns often null

查看:73
本文介绍了Android反向地理编码getLocality通常返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android地理编码通过 Address.getLocality()获取当前城市.它运行良好,直到最近它似乎经常为该位置返回null.

I am using Android Geocoding to get the current city with Address.getLocality(). It has worked fine, until just recently it appears to often return null for the locality.

这里是一个例子:

try {
    Geocoder c = new Geocoder(this, Locale.getDefault());
    double lat = 51.481;
    double lon = 0.0;
    List<Address> l = c.getFromLocation(lat, lon, 5);
    for (Address a: l) {
        Log.i("GeocoderTest", "Locality " + a.getLocality() + " (" + a + ")");
    }
} catch (IOException e) {
    Log.e("GeocoderTest", "", e);
}

这现在将以下消息记录为第一个返回的地址:

This now logs the following message for the first returned address:

位置为空(地址[addressLines = [0:"14-18 Park Vista",1:伦敦伦敦格林威治自治市镇SE10,2:" UK],功能=,管理员=空,子管理员=空,位置=空,交通费=停车Vista,邮政编码=空,国家/地区代码= GB,国家/地区名称=美国王国,hasLatitude = true,纬度= 51.4819069,hasLongitude = true,经度= -6.327E-4,电话=空,url =空,extras =空])

Locality null (Address[addressLines=[0:"14-18 Park Vista",1:"London Borough of Greenwich, London SE10",2:"UK"],feature=,admin=null,sub-admin=null,locality=null,thoroughfare=Park Vista,postalCode=null,countryCode=GB,countryName=United Kingdom,hasLatitude=true,latitude=51.4819069,hasLongitude=true,longitude=-6.327E-4,phone=null,url=null,extras=null])

某些位置确实返回了该位置所在的城市,而旁边的位置却没有.

Some locations do return the city in the locality, while a location next to it does not.

因此它在之前确实可以正常工作,实际上我以前从未见过零位位置.因此,我想Google的地理编码服务肯定有所改变.知道发生了什么,这种情况是永久的吗?如果是,从位置确定城市的最佳方法是什么?

So it did work just fine before, actually I had not seen a null locality before. So I guess something must have changed in Google's geocoding service. Any idea what is going on, and is this situation permanent? If yes, what would be the best way to determine the city from a location?

推荐答案

我注意到,getLocality()通常会返回由Geocoder返回的列表中第一个地址的null.
另一方面,正确的城市名称将保留在下一个地址的位置.
因此,我正在使用此变通方法,它适用于大城市:

I noticed, that very often getLocality() returns null for the first address in the list, returned by Geocoder.
On the other hand correct city name stays at Locality of a next Address.
So I am using this workaround and it works well for big cities:

 private String getCityNameByCoordinates(double lat, double lon) throws IOException {
    List<Address> addresses = mGeocoder.getFromLocation(lat, lon, 10);
    if (addresses != null && addresses.size() > 0) {
        for (Address adr : addresses) {
            if (adr.getLocality() != null && adr.getLocality().length() > 0) {
                return adr.getLocality();
            }
        }
    }
    return null;
}

这篇关于Android反向地理编码getLocality通常返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆