gpt4 book ai didi

Android 反向地理编码 getLocality 通常返回 null

转载 作者:IT老高 更新时间:2023-10-28 23:12:01 28 4
gpt4 key购买 nike

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

这是一个例子:

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

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

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])

有些位置确实会返回本地的城市,而旁边的位置则不会。

所以它之前确实工作得很好,实际上我以前没有见过空位置。所以我想谷歌的地理编码服务一定发生了一些变化。知道发生了什么,这种情况是永久性的吗?如果是,从某个位置确定城市的最佳方法是什么?

最佳答案

我注意到,getLocality() 经常为列表中的第一个地址返回 null,由 Geocoder 返回。
另一方面,正确的城市名称保留在下一个地址的位置。
所以我正在使用这种解决方法,它适用于大城市:

 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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8661857/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com