Android geocoder 反向位置 - 获得城市的可靠方式?

标签 android google-maps google-geocoder

到目前为止,我已经搜索了很多,但运气不佳。

Android Geocoder 返回 android.location.Address 对象。 据我了解,城市应该在 getLocality() 中返回。 这似乎在美国国内运作良好,但在国外则不然。

我正在编写一个国际应用程序,并努力寻找一种解决方案来找出地理位置所在的城市。

这里是捷克共和国/布拉格的输出:

Address[addressLines=
[0:"Psohlavců 1764/2",
1:"147 00 Prague-Prague 4",
2:"Czech Republic"],
feature=2,
admin=Hlavní město Praha,
sub-admin=Prague,
locality=null,
thoroughfare=Psohlavců,
postalCode=147 00,
countryCode=CZ,
countryName=Czech Republic,
hasLatitude=true,
latitude=50.0276543,
hasLongitude=true,
longitude=14.4183926,
phone=null,
url=null,
extras=null]

locality 为空,城市在 sub-admin 内! 地址本身没问题,所以地理编码器服务器似乎知道这个城市。

这里有一些随机的 EU 示例,但局部性部分有效:

Address[addressLines=[0:"Nad lesem 440/34",1:"147 00 Prague-Prague 4",2:"Czech Republic"],feature=34,admin=Hlavní město Praha,sub-admin=Prague,locality=null,thoroughfare=Nad lesem,postalCode=147 00,countryCode=CZ,countryName=Czech Republic,hasLatitude=true,latitude=50.02424,hasLongitude=true,longitude=14.4117568,phone=null,url=null,extras=null]

Address[addressLines=[0:"Hauner Straße 4",1:"84431 Heldenstein",2:"Germany"],feature=4,admin=null,sub-admin=null,locality=Heldenstein,thoroughfare=Hauner Straße,postalCode=84431,countryCode=DE,countryName=Germany,hasLatitude=true,latitude=48.2540274,hasLongitude=true,longitude=12.3413535,phone=null,url=null,extras=null]

Address[addressLines=[0:"Igler Straße",1:"6020 Innsbruck",2:"Austria"],feature=Igler Straße,admin=Tyrol,sub-admin=Innsbruck,locality=Innsbruck,thoroughfare=Igler Straße,postalCode=6020,countryCode=AT,countryName=Austria,hasLatitude=true,latitude=47.2465698,hasLongitude=true,longitude=11.4054237,phone=null,url=null,extras=null]

Address[addressLines=[0:"Durnberg 24",1:"5724 Stuhlfelden",2:"Austria"],feature=24,admin=Salzburg,sub-admin=Zell am See District,locality=null,thoroughfare=Durnberg,postalCode=5724,countryCode=AT,countryName=Austria,hasLatitude=true,latitude=47.3233373,hasLongitude=true,longitude=12.4960482,phone=null,url=null,extras=null]

Address[addressLines=[0:"U Roháčových kasáren 14",1:"100 00 Prague 10",2:"Czech Republic"],feature=14,admin=Hlavní město Praha,sub-admin=Prague,locality=Prague 10,thoroughfare=U Roháčových kasáren,postalCode=null,countryCode=CZ,countryName=Czech Republic,hasLatitude=true,latitude=50.0704092,hasLongitude=true,longitude=14.4673473,phone=null,url=null,extras=null]

也许是我的错,但对我来说,似乎根据国家和地区的不同,城市会出现在不同的领域。 但是,地址本身似乎足以发送一封邮政信件。

是否有人编写了一个聪明的函数来试图从 Geocoder 结果中获得更多意义?遗憾的是,谷歌存储了信息但没有正确提供。

最佳答案

要结束我的问题,用变通方法解决了它。 使用 dannyroa 的建议

String city="unknown";
if (address.getLocality() != null)  city=address.getLocality();
else
  if (address.getSubAdminArea() != null)  city=address.getSubAdminArea();

这可以通过从第二个地址行中获取城市信息来进一步扩展。 删除邮政编码并取余数,但此信息在城市中不是唯一的,并且可能会根据地区/区域而变化。

关于Android geocoder 反向位置 - 获得城市的可靠方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21921460/

相关文章:

java - 调试 Intent

ios恢复谷歌地图sdk GMSMapsView的状态

android - 无法在 android 中的警报对话框内执行 fragment 事务

ruby-on-rails - 如何捕获/解决 ActiveRecord :RecordInvalid exception caused when I save a built assocation

python-3.x - 对于这种特殊情况,如何使用地理编码获得公司的正确位置? (Python、谷歌 API)

android - 参数计数错误,格式字符串 [name] 需要 0 但格式调用提供 1

android - 使用存储库模式的具有多个数据源的架构

android - 带 Action 的倒数计时器循环

javascript - 将 infoWindows 添加到数组中的多个标记

android - Genymotion - 从经度和纬度获取地址