gpt4 book ai didi

javascript - google.maps.places 有时未定义

转载 作者:行者123 更新时间:2023-12-03 05:07:39 25 4
gpt4 key购买 nike

我已经为“地点搜索框”实现了谷歌地图,它工作得很好,但有时该地点是未定义的......

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAE0jVybaY4jzT3irMM9g8THf3qGF6V9xE&callback=initMap&libraries=places"
async defer></script>

搜索框:

function initMap() {
var defaultBounds = new google.maps.LatLngBounds(new google.maps.LatLng(48.210033, 16.363449), new google.maps.LatLng(48.210033, 16.363449));

var options = {
bounds: defaultBounds,
types: ['geocode']
};

var autocomplete = new google.maps.places.Autocomplete(document.getElementById('searchInput'), options);

google.maps.event.addListener(autocomplete, 'place_changed', function () {
var place = autocomplete.getPlace();
if (typeof place == "undefined") {
window.alert("Not found!");
}
else{
console.log(place);
globalPlace = place;
}
});

var autocomplete2 = new google.maps.places.Autocomplete(document.getElementById('searchInputAdd'), options);

google.maps.event.addListener(autocomplete2, 'place_changed', function () {
var place = autocomplete.getPlace();
if (typeof place == "undefined") {
window.alert("Not found!");
}
else{
console.log(place);
globalPlaceAdd = place;
}
});
}

我该如何解决这个问题?

还有一个问题,当我单击自动完成位置时,它可以工作,但是当我按 Enter 键时,页面会使用后参数重新加载,我可以禁用它吗?这两种方法都有效吗?

最佳答案

我想我已经发现你的错误了。在第二个监听器中,您正在执行 autocomplete.getPlace() 而不是 autocomplete2.getPlace()

试试这个:

var autocomplete2 = new google.maps.places.Autocomplete(document.getElementById('searchInputAdd'), options);

google.maps.event.addListener(autocomplete2, 'place_changed', function () {
var place = autocomplete2.getPlace();
if (typeof place == "undefined") {
window.alert("Not found!");
}
else{
console.log(place);
globalPlaceAdd = place;
}
});

关于javascript - google.maps.places 有时未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41958925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com