Android地理编码器反向位置 - 获得城市的可靠方式?

时间:2014-02-20 22:53:08

标签: 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之内! 地址本身没问题,所以地理编码服务器似乎知道这个城市。

这里有一些随机的欧盟示例,但地点部分起作用:

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结果中更有意义吗?遗憾的是,谷歌已经存储了这些信息,但没有正确提供。

1 个答案:

答案 0 :(得分:6)

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

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

这可以通过从第二个地址线获取城市信息来进一步扩展。 删除邮政编码并取出其余部分,但这些信息在一个城市内并不是唯一的,可能会根据地区/区域而改变。