Android reverse geocoding getLocality returns often null

走远了吗. 提交于 2019-12-03 04:56:32

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;
}
Shangab

Now I live in Canada, Ontario, Hamilton (Hamilton is my city, Ontario is the province)

I noticed that getLocality() returns null, and getAdminArea() returns Ontario, and getSubLocality() returns Hamilton. ch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!