Android geocoder反向定位-获取城市的可靠方法?

Android geocoder反向定位-获取城市的可靠方法?,android,google-maps,google-geocoder,Android,Google Maps,Google Geocoder,到目前为止,我已经找了不少,但运气不好 Android地理编码器返回Android.location.Address对象。 据我所知,城市应该在getLocation()中返回。 这在美国国内似乎效果不错,在国外则不然 我正在编写一个国际应用程序,努力找到一个解决方案,找出一个地理位置的城市 捷克共和国/布拉格的产出如下: Address[addressLines= [0:"Psohlavců 1764/2", 1:"147 00 Prague-Prague 4", 2:"Czech Repub

到目前为止,我已经找了不少,但运气不好

Android地理编码器返回
Android.location.Address
对象。 据我所知,城市应该在
getLocation()
中返回。 这在美国国内似乎效果不错,在国外则不然

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

捷克共和国/布拉格的产出如下:

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]
地区
为空,城市在
子管理员
范围内! 地址本身没有问题,因此地理编码服务器似乎知道这个城市

这里有一些随机的例子,但局部性部分起作用:

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]
也许是我的错,但对我来说,这似乎取决于这个城市在不同领域的国家和地区。 然而,这个地址本身似乎很好,足以发送一封邮政信


有人写了一个聪明的函数,试图使地理编码器的结果更有意义吗?很遗憾,谷歌存储了这些信息,但没有正确地提供这些信息。

在结束我的问题时,用一种变通方法解决了这个问题。 使用dannyroa的建议

String city="unknown";
if (address.getLocality() != null)  city=address.getLocality();
else
  if (address.getSubAdminArea() != null)  city=address.getSubAdminArea();
这可以通过从第二个地址行获取城市信息来进一步扩展。
删除邮政编码并获取剩余的邮政编码,但此信息在一个城市中不是唯一的,并且可能会根据地区/区域的不同而变化。

Yup。在一些国家,情况有所不同。首先看位置,如果是空的,检查sub-admin。如果位置是空的,您确定sub-admin作为替代方案是可靠的吗?根据我的经验,它要么在位置中,要么在sub-admin中。没有遇到两个都为空的场景。这是一个bug。甚至我也经历过同样的问题。不幸的是,我还没有找到解决方法。它也不可靠,因为有时getSubAdminArea()也会返回null,但city会保留在mAdminArea中(并以奇怪的格式“city CityName”)。这种反向地理编码似乎不够可靠。您可以提供一个示例位置吗?谷歌在这方面做得非常糟糕,他们可能开始意识到他们的数据在新的国家有多么糟糕。看起来他们一直在用越来越多的数据扩展他们已经不匹配的结构,到处乱扔数据。然而,你可以使它可靠。如果此代码在某个特定国家/地区失败,那么您只需找到该国家/地区的“正确规则”,并在其中添加“如果”案例。到目前为止,它在与我相关的所有国家都有效(到目前为止),根本不可靠。副行政区并不总是城市。对你和狮子座同样重要。没有一个例子,你的评论是无用的。