Javascript google.maps.places有时未定义

Javascript google.maps.places有时未定义,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我已经为“地点搜索框”实现了谷歌地图,它工作得很好,但有时地点是未定义的 <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAE0jVybaY4jzT3irMM9g8THf3qGF6V9xE&callback=initMap&libraries=places" async defer></script> 我怎样才能解决这个问题 还有一个问题,当我点击“自动完成”按钮时,它会工作,

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

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAE0jVybaY4jzT3irMM9g8THf3qGF6V9xE&callback=initMap&libraries=places"
async defer></script>
我怎样才能解决这个问题


还有一个问题,当我点击“自动完成”按钮时,它会工作,但当我按enter键时,页面会用post参数重新加载,我可以禁用它吗?这两种方法都有效吗?

我想我发现了你的错误。在第二个侦听器中,您正在执行
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;
    }
});

对于最后一点,页面将重新加载,因为输入submit表单。只需使用表单外部的文本输入。当他们假设为结果时,位置是未定义的还是仅当搜索框未找到任何内容时?当他们应该为结果时,位置是未定义的。我点击了自动完成的建议,但没有定义这种行为是随机发生的还是一直发生的?随机的,我在我的网络中运行一个Web服务器,大多数情况下,当其他人进入这个Web应用程序时,他不能使用两个“搜索框”中的一个,因为这个错误哦,上帝。谢谢。请就叫我先生吧;)
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;
    }
});