当我从初始化函数启动一个函数时,我收到消息
“无法获取未定义或空引用的属性'address_components'”
我首先添加一个监听器
google.maps.event.addDomListener(window, 'load', initialize);
这是我的初始化功能
function initialize() {
document.getElementById('autocomplete').value = city()+','+countryName();
autocomplete = new google.maps.places.Autocomplete(
/** @type {HTMLInputElement} */(document.getElementById('autocomplete')),
{types: ['establishment','geocode']});
fillInAddress(); --> this call does not work
google.maps.event.addListener(autocomplete, 'place_changed', function() {
fillInAddress(); --> this call work when I change manually the value in the field
});
}
当由上面的侦听器(place_changed)触发时,以下调用的函数起作用
function fillinAddress()
var place = autocomplete.getPlace();
console.log(place); --> this gives undefined when call from initialize function
for (var i = 0; i < place.address_components.length; i++) { .. --> this gives error
//Unable to get property 'address_components' of undefined or null reference
有什么帮助吗?
最佳答案
您需要将自动完成功能传递给类似
function initialize() {fillinAddress(autocomplete)}
function fillinAddress(autocomplete){console.log(autocomplete);}
关于javascript - 无法获取未定义或null引用javascript的属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27429579/