【问题标题】:google placecomplete plugin does not allow to select an address anymoregoogle placecomplete 插件不再允许选择地址
【发布时间】:2020-08-13 05:59:05
【问题描述】:

我多年来一直在使用这个脚本:https://github.com/stephjang/placecomplete,没有任何问题。今晚我收到很多投诉,用户无法从下拉菜单中选择地址。输入地址可以正常工作并且会显示结果,但是在尝试选择地址时,它不会做任何事情。有谁知道谷歌阻止 placecomplete 请求是否发生了变化?

我的代码:

$inputLocationSearch.placecomplete({ 
        placeholderText: "Enter your site location...", 
        requestParams: {
            componentRestrictions: {country: ['ca','us']}
        } 
    });

问题的屏幕截图(鼠标或键盘无法选择找到的地址):

【问题讨论】:

    标签: javascript jquery googleplacesautocomplete


    【解决方案1】:

    在插件jquery.placecomplete.js 代码中发现问题。替换默认的id 参数以使用place_id,因为id 已被google 自动完成功能弃用。

    我在现有代码上方添加:apr["id"] = apr["place_id"];apr["text"] = apr["description"];

    这是整个代码块,加上:

    var select2options = $.extend({}, {
            query: function(query) {
                GooglePlacesAPI.getPredictions(query.term, requestParams)
                    .done(function(aprs) {
                        var results = $.map(aprs, function(apr) {
                            // Select2 needs a "text" and "id" property set
                            // for each autocomplete list item. "id" is
                            // already defined on the apr object
                            apr["id"] = apr["place_id"];
                            apr["text"] = apr["description"];
                            return apr;
                        });
                        query.callback({results: results});
                    })
                    .fail(function(errorMsg) {
                        $el.trigger(pluginName + ":error", errorMsg);
                        query.callback({results: []});
                    });
            },
            initSelection: function(element, callback) {
                // initSelection() was triggered by value being defined directly
                // in the input element HTML
                var initText = $el.val();
    
                // The id doesn't matter here since we're just trying to prefill
                // the input with text for the user to see.
                callback({id: 0, text: initText});
            },
            minimumInputLength: 1,
            selectOnBlur: true,
            allowClear: true,
            multiple: false,
            dropdownCssClass: "jquery-placecomplete-google-attribution",
            placeholder: this.options.placeholderText
        }, this.options);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 2017-10-26
    • 2015-12-08
    • 2015-12-12
    • 2012-12-18
    相关资源
    最近更新 更多