這是我用來getPlace()
它總是返回undefined
對象的函數:爲什麼Google地圖getPlace()總是返回Undefined對象?
var element = document.getElementById('hdnOldAddress');
var AutoCompleteAddress2;
element.value = 'Marketfair Mall 1916 Skibo Road Fayetteville NC 28314';
autocomplete2 = new google.maps.places.Autocomplete(element);
autocomplete2.addListener('place_changed', function() {
var place2 = autocomplete2.getPlace();
if (place2.address_components) {
AutoCompleteAddress2 = [
(place2.address_components[0] && place2.address_components[0].short_name || ''),
(place2.address_components[1] && place2.address_components[1].short_name || ''),
(place2.address_components[2] && place2.address_components[2].short_name || ''),
(place2.address_components[3] && place2.address_components[3].short_name || ''),
(place2.address_components[4] && place2.address_components[4].short_name || ''),
(place2.address_components[5] && place2.address_components[5].short_name || ''),
(place2.address_components[6] && place2.address_components[6].short_name || ''),
(place2.address_components[7] && place2.address_components[7].short_name || ''),
(place2.address_components[8] && place2.address_components[8].short_name || ''),
(place2.address_components[9] && place2.address_components[9].short_name || ''),
(place2.address_components[10] && place2.address_components[10].short_name || ''),
(place2.address_components[11] && place2.address_components[11].short_name || ''),
(place2.address_components[12] && place2.address_components[12].short_name || ''),
(place2.address_components[13] && place2.address_components[13].short_name || ''),
(place2.address_components[14] && place2.address_components[14].short_name || ''),
].join(' ');
}
});
google.maps.event.trigger(autocomplete2, 'place_changed');
編輯
替換this.getPlace()
與autocomplete2.getPlace()
我試過了,用'var place2 = autocomplete2.getPlace();'替換,但不工作。獲取相同的錯誤。 – JMD
我們正在手動設置'元素'(文本框)的值,而不是從谷歌地圖提供的自動建議下拉列表中進行選擇。這是我們沒有得到地方的原因嗎? – JMD
問題是place_changed事件的觸發器。 – geocodezip