我已经为"地点搜索框"实施了谷歌地图哪个工作得很好,但有时这个地方是不确定的......
<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;
}
});
}
我该如何解决?
还有一个问题,当我点击它自动完成的地方时它可以正常工作,但是当我按下输入页面重新加载post-params时,我可以禁用它吗?这两种方式都有效吗?
答案 0 :(得分:1)
我想我找到了你的错误。在您的第二个听众中,您正在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;
}
});