Android reverse geocoding getLocality returns often null

前端 未结 3 1272
情歌与酒
情歌与酒 2021-02-04 01:22

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 loc

相关标签:
3条回答
  • 2021-02-04 01:36

    Using getSubAdminArea() worked fine for me.

    0 讨论(0)
  • 2021-02-04 01:45

    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;
    }
    
    0 讨论(0)
  • 2021-02-04 01:51

    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

    0 讨论(0)
提交回复
热议问题