【问题标题】:Autocomplete in google maps places throws error TypeError: undefined is not a function (evaluating 'a[zG]("placeholder")')谷歌地图中的自动完成会引发错误 TypeError: undefined is not a function (evalating 'a[zG]("placeholder")')
【发布时间】:2025-12-12 03:00:01
【问题描述】:

我正在为此苦苦挣扎。

我正在创建一个简单的搜索地图功能,但自动完成向我抛出了不那么描述性的错误:TypeError: undefined is not a function (evalating 'azG')

我正在 modal.s.shown 事件的引导模式中初始化我的地图。

这里是 Javascript:

$('#map-modal').on('shown.bs.modal', function (e) {
    //picking up data attributes from the image that initialised the modal
    var image = $(e.relatedTarget);
    map_lat = $(image).data('lat');
    map_lng = $(image).data('lng');

    //passing the data to a initialise map function
    initialiseModalMap(map_lat,map_lng);

    //add autocomplete
    var input = $('#start-point');
    var options = {
        types: ['geocode']
    };
    var autocomplete = new google.maps.places.Autocomplete(input, options);
    autocomplete.bindTo('bounds', map);
    google.maps.event.addListener(autocomplete, 'place_changed', function() {
       console.log('changed');
    });
});

但是一旦事件显示.bs.modal 被触发,我在控制台中得到以下错误:

TypeError: undefined is not a function (evaluating 'a[zG]("placeholder")')

并且永远不会执行自动完成,在自动完成代码块之前,一切都很好。

我错过了什么?

如果您需要更多信息,请尽管询问。

提前致谢!

【问题讨论】:

    标签: javascript google-maps google-maps-api-3 autocomplete google-places-api


    【解决方案1】:

    当我切换到 vanilla javascript 来获取输入元素时,不会抛出错误

    所以

    var input = $('#start-point');
    

    我放了

    var input = document.getElementById('start-point');
    

    但自动完成的下拉菜单没有呈现,但我想这是一个完全不同的问题

    【讨论】:

    • 好答案。 API 需要一个对 DOM 元素的引用,而 jQuery 返回一个 jQuery 对象。 $("#start-point").get(0) 允许您使用 jQuery 访问原始 DOM 对象。
    最近更新 更多