2014-02-20 35 views
7

到目前爲止,我搜索了很多不幸的東西。Android地理編碼器反向位置 - 獲取城市的可靠方法?

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爲null,城市內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結果更有意義嗎?很遺憾看到Google存儲了信息,但沒有正確提供。

+1

是的。它在一些國家有所不同。先看看本地,如果是空的,檢查子管理。 – dannyroa

+0

如果地方是空的,你確定子管理是可靠的嗎? – John

+2

基於我的經驗,它可以在本地或子管理。沒有遇到兩個都是空的場景。 – dannyroa

回答

5

要關閉我的問題,請使用解決方法解決此問題。 從dannyroa

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

此使用建議者可以通過讓城市的信息進行第二地址線的進一步延伸。 刪除郵政編碼並取出其餘部分,但此信息在城市內並非唯一,可能因地區/地區而異。

+1

它也不可靠,因爲有時getSubAdminArea()也返回null,但城市停留在mAdminArea中(並且以奇怪的格式「city CityName」)。似乎這種反向地理編碼不夠可靠 –

+0

也許你可以提供一個示例位置?谷歌已經完成了糟糕的工作,他們可能已經開始並意識到他們的數據適合新的國家有多糟糕。 看起來他們一直在用已經越來越多的數據來擴展他們已經不合適的結構,在這裏和那裏剷起它。 但是:您可以使其可靠。如果此代碼在特定國家/地區失敗,那麼您只需要爲該國家找到「正確的規則」並在其中添加「if」情況即可。 到目前爲止,它在與我有關的所有國家(迄今爲止)都有效 – John

+1

根本不可靠。子管理區並不總是城市。 – 2016-11-28 19:30:53

相關問題