2017-12-18 243 views
1

請考慮谷歌地圖gecoding響應的JSON響應。 它由具有五個元素的address_components組成。 通常情況下開發人員會使用它的第一個項目(索引爲0的項目)來查找地址元素,例如ac routelocalityformatted address。但這不是最好的選擇。例如,在這種情況下,第二個項目比其他項目更具描述性地址。有時在同一回覆中有不同地點的同一地址。Google Map API:如何在地理編碼結果中管理不同的「address_components」?

我該如何選擇更好的一個(例如Android中的Google Map API在可用項目中選擇一個,但它並不總是索引爲0的項目,但它在替代項中進行了選擇,我想知道它將如何執行並選擇最好的搭配)?

再說,我也想通過不同address_components的所有元素進行迭代來提取和彙集細條信息,但問題是導致地址不一致的同類型一樣locality元素在不同address_components不同的信息當從不同的address_component項目收集到的信息元素。 作爲示例,在下面的示例中,我們有兩個值شهر جدید اندیشهKaraj在不同的locality元素中,這兩個元素是兩個不同的城市標籤(在同一鄰域中)。

https://maps.googleapis.com/maps/api/geocode/json?latlng=35.7163931455472,51.01335000246763&key=xxxxx&language=fa

{ 
    "results" : [ 
     { 
     "address_components" : [ 
      { 
       "long_name" : "Unnamed Road", 
       "short_name" : "Unnamed Road", 
       "types" : [ "route" ] 
      }, 
      { 
       "long_name" : "Karaj", 
       "short_name" : "Karaj", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "Shahriar County", 
       "short_name" : "Shahriar County", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "Tehran Province", 
       "short_name" : "Tehran Province", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "ایران", 
       "short_name" : "IR", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "Tehran Province, Karaj, Unnamed Road, ایران", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 35.7221382, 
        "lng" : 51.0148178 
       }, 
       "southwest" : { 
        "lat" : 35.716435, 
        "lng" : 51.0095103 
       } 
      }, 
      "location" : { 
       "lat" : 35.719286, 
       "lng" : 51.012165 
      }, 
      "location_type" : "GEOMETRIC_CENTER", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 35.7221382, 
        "lng" : 51.0148178 
       }, 
       "southwest" : { 
        "lat" : 35.716435, 
        "lng" : 51.0095103 
       } 
      } 
     }, 
     "place_id" : "ChIJmf7_zEyTjT8RkM8-nK6dTm0", 
     "types" : [ "route" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "فاز ۶ شهر جدید اندیشه", 
       "short_name" : "فاز ۶ شهر جدید اندیشه", 
       "types" : [ "political", "sublocality", "sublocality_level_1" ] 
      }, 
      { 
       "long_name" : "شهر جدید اندیشه", 
       "short_name" : "شهر جدید اندیشه", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "شهرستان شهریار", 
       "short_name" : "شهرستان شهریار", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "استان تهران", 
       "short_name" : "استان تهران", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "ایران", 
       "short_name" : "IR", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "فاز ۶ شهر جدید اندیشه، شهر جدید اندیشه، استان تهران، ایران", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 35.7389537, 
        "lng" : 51.0349971 
       }, 
       "southwest" : { 
        "lat" : 35.7039031, 
        "lng" : 51.0044146 
       } 
      }, 
      "location" : { 
       "lat" : 35.7210753, 
       "lng" : 51.014934 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 35.7389537, 
        "lng" : 51.0349971 
       }, 
       "southwest" : { 
        "lat" : 35.7039031, 
        "lng" : 51.0044146 
       } 
      } 
     }, 
     "place_id" : "ChIJiYlwJLHsjT8RruE39U9NMoQ", 
     "types" : [ "political", "sublocality", "sublocality_level_1" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "شهر جدید اندیشه", 
       "short_name" : "شهر جدید اندیشه", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "شهرستان شهریار", 
       "short_name" : "شهرستان شهریار", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "استان تهران", 
       "short_name" : "استان تهران", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "ایران", 
       "short_name" : "IR", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "شهر جدید اندیشه، استان تهران، ایران", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 35.7388144, 
        "lng" : 51.04460479999999 
       }, 
       "southwest" : { 
        "lat" : 35.6838973, 
        "lng" : 50.9894371 
       } 
      }, 
      "location" : { 
       "lat" : 35.7078282, 
       "lng" : 51.0227587 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 35.7388144, 
        "lng" : 51.04460479999999 
       }, 
       "southwest" : { 
        "lat" : 35.6838973, 
        "lng" : 50.9894371 
       } 
      } 
     }, 
     "place_id" : "ChIJIQwwRcnsjT8RnTJfLJ3QUAg", 
     "types" : [ "locality", "political" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "شهرستان شهریار", 
       "short_name" : "شهرستان شهریار", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "استان تهران", 
       "short_name" : "استان تهران", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "ایران", 
       "short_name" : "IR", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "شهرستان شهریار، استان تهران، ایران", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 35.7389885, 
        "lng" : 51.23044970000001 
       }, 
       "southwest" : { 
        "lat" : 35.5333437, 
        "lng" : 50.8859253 
       } 
      }, 
      "location" : { 
       "lat" : 35.6096201, 
       "lng" : 51.03319330000001 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 35.7389885, 
        "lng" : 51.23044970000001 
       }, 
       "southwest" : { 
        "lat" : 35.5333437, 
        "lng" : 50.8859253 
       } 
      } 
     }, 
     "place_id" : "ChIJQ6KQjY7xjT8RoYD9gJh8_CY", 
     "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "استان تهران", 
       "short_name" : "استان تهران", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "ایران", 
       "short_name" : "IR", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "استان تهران، ایران", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 36.07789210000001, 
        "lng" : 53.216228 
       }, 
       "southwest" : { 
        "lat" : 34.909543, 
        "lng" : 50.3186971 
       } 
      }, 
      "location" : { 
       "lat" : 35.7248416, 
       "lng" : 51.381653 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 36.07789210000001, 
        "lng" : 53.216228 
       }, 
       "southwest" : { 
        "lat" : 34.909543, 
        "lng" : 50.3186971 
       } 
      } 
     }, 
     "place_id" : "ChIJf5Us9YQBjj8R0OohvHQms1U", 
     "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
     "address_components" : [ 
      { 
       "long_name" : "ایران", 
       "short_name" : "IR", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "ایران", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 39.782056, 
        "lng" : 63.3333366 
       }, 
       "southwest" : { 
        "lat" : 24.8066999, 
        "lng" : 44.0326949 
       } 
      }, 
      "location" : { 
       "lat" : 32.427908, 
       "lng" : 53.688046 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 39.782056, 
        "lng" : 63.3333366 
       }, 
       "southwest" : { 
        "lat" : 24.8066999, 
        "lng" : 44.0326949 
       } 
      } 
     }, 
     "place_id" : "ChIJ8R1rwS7s9z4RzvpRntOVsEA", 
     "types" : [ "country", "political" ] 
     } 
    ], 
    "status" : "OK" 
} 

回答

1

它看起來像有一兩個不同的問題,所以範圍有點廣。我會盡力解決你的疑惑。

首先,您正在使用反向地理編碼請求將座標35.7163931455472,51.01335000246763解析爲街道地址。請注意,reverse geocoding旨在將給定座標解析爲最近的可用地址。除了街道地址之外,服務還會提出不同類型的結果:路線,次級地址,地點,行政區域,國家。 有時候,如果沒有街道地址靠近給定點(大約距離該點50米內),則類型街道地址的結果不會出現在響應中。在您的示例中,類型街道地址的結果不可用,因此響應包含從路由級別開始的不同類型的結果。看看我的屏幕截圖,它代表您的回覆中的前三項。

enter image description here

正如你所看到的第一個項目都有一個類型路線,第二個項目都有一個類型sublocality,第三個有一個模式標本產地。

很難說什麼是選擇地址的「最佳」標準。你可以看到路徑項是最接近的,但不幸的是,這條路是未命名的,所以你認爲它不夠好。從技術上講,這是最接近的項目,所以服務假設這是最好的匹配。我建議檢查結果的類型,如果項目0有一個類型street_address應該足夠好去,如果項目0有一個類型路線,檢查道路是否有名稱,如果這是未命名道路檢查項目1可能更詳細。

但無論如何,未命名道路是數據的問題,您可以按照幫助中心報告給谷歌的數據團隊:

https://support.google.com/maps/answer/3094088

其次,如果我理解正確的話,你注意到了0項和1項儘管它們(我截圖中的標記1和標記2)似乎屬於安迪舍新城,但它們具有不同的局部地址組件值。路線(地點ID ChIJmf7_zEyTjT8RkM8-nK6dTm0)被報告爲Karaj地點的一部分。我可以看到卡拉季的谷歌地圖的界限,如下面的截圖

enter image description here

https://www.google.com/maps/place/Karaj,+Alborz+Province,+Iran/@35.7700272,50.95899,12.41z/data=!4m5!3m4!1s0x3f8dbf95ef45f011:0x722a04e54eba9bcd!8m2!3d35.8400188!4d50.9390906

而且我可以看到Andisheh新城的邊界作爲顯示在下面的屏幕截圖

enter image description here

https://www.google.com/maps/place/Andisheh+New+Town,+Tehran+Province,+Iran/@35.6998795,51.0216598,14.08z/data=!4m5!3m4!1s0x3f8decc945300c21:0x850d09d2c5f329d!8m2!3d35.7078282!4d51.0227587

請注意,您的回覆路線顯然位於Andisheh New Town多邊形內,因此此時看起來我們也面臨數據問題。地點ID ChIJmf7_zEyTjT8RkM8-nK6dTm0應該有Andisheh New Town的地點組件,而不是Karaj。

隨意使用這個頁面發送反饋給谷歌:

https://www.google.com/maps/place/Tehran+Province,+Karaj,+Unnamed+Road,+Iran/@35.719286,51.0077876,17z/data=!3m1!4b1!4m5!3m4!1s0x3f8d934cccfffe99:0x6d4e9dae9c3ecf90!8m2!3d35.719286!4d51.012165!10m2!1e3!2e3

如果你想和反向地理編碼玩,請使用地理編碼工具:

https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3D35.716393%252C51.01335

希望我的答案澄清你的疑惑!

相關問題