1

我嘗試使用谷歌地圖API自動完成,但有它和地理編碼API,例如之間存在一些差異:谷歌地圖API自動完成地理編碼X

在自動完成API,我想搜索地址「Rua Tiradentes,1020,Ibirubá」,當我在列表中選擇時,地址組件「street_number」沒有出現,這是這個地址和其他地址的問題。

讓我們嘗試用地理編碼API:

var geocoder = new google.maps.Geocoder(); 
geocoder.geocode({ 'address': 'Rua Tiradentes, 1020, Ibirubá' }, function (results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     console.log(results); 
    } else { 
     alert("Geocoder failed due to: " + status); 
    } 
}); 

現在,street_number是address_components裏面,有沒有解決辦法或谷歌自動完成是不是可靠?

這裏是放置代碼:

var b = new google.maps.places.Autocomplete(myInput); 

google.maps.event.addListener(b, 'place_changed', function() { 
    console.log(b.getPlace()); 
}) 
+1

文檔不是很清楚:*以下類型**可能會在地點搜索結果中返回**,除了上面表1中的類型。*請參閱https://developers.google.com/places/documentation/supported_types#table2因此,如果*可能會被返回*,我們並不真正知道它何時以及爲何會/將不會被退回。 – MrUpsidown 2014-10-29 16:19:56

+0

基本上,地理編碼文檔也非常模糊:*除上述之外,地址組件**可能包含以下類型*。請參閱:https://developers.google.com/maps/documentation/geocoding/ #Types – MrUpsidown 2014-10-29 16:25:33

+0

我報告過地圖API的錯誤,它應該像Geocoder API一樣。我寫了一個回退,如果street_number爲空,獲取輸入文本並進行地理編碼請求 – Alexandre 2014-10-29 16:54:41

回答

0

可能的解決方法如下。您可以嘗試對地點進行地理編碼以獲取所有地址組件。最近,Google增加了使用placeId作爲請求參數對地址進行地理編碼的可能性。

您可以找到文檔中的更多詳細信息: https://developers.google.com/maps/documentation/javascript/geocoding https://developers.google.com/maps/documentation/javascript/3.exp/reference#GeocoderRequest

所以,你可以得到這樣的無功發生地= autocomplete.getPlace();您可以像place.place_id那樣獲取地點ID並執行如下所示的地址解析請求:

geocoder.geocode({ 'placeId': place.place_id}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
    console.log(results); 
    } else { 
    alert('Geocode was not successful for the following reason: ' + status); 
    } 
}); 
相關問題