【问题标题】:Different result between google maps Autocomplete and AutocompleteService谷歌地图自动完成和自动完成服务之间的不同结果
【发布时间】:2013-03-09 01:47:27
【问题描述】:

我正在使用谷歌地图 APIv3 AutocompleteService 将数据提取到引导程序的预输入。但是与使用google内置的自动完成返回的结果相比,结果有点不准确。

我的 HTML 代码:

<label>Built-in</label><input type="text" id="address1" />
<label>Customize</label><input type="text" id="address2" />

我的脚本:

<script>
    //this input use google built-in autocomplete
    var input1 = document.getElementById('address1');
    autocomplete = new google.maps.places.Autocomplete(input1);

    //this input use google AutocompleteService
    $('#address2').typeahead({
        source: process_keyword
    });

    var service = new google.maps.places.AutocompleteService();

    function process_keyword(query, process) {
        var place_results = [];
        var request = {
            input: query,
            types: ['geocode']
        };
        service.getPlacePredictions(request, function (predictions, status) {
            process($.map(predictions, function (prediction) {
                return prediction.description;
            }));
        });
    }
</script>

我在这里发布了完整的代码:http://jsbin.com/ididas/1/edit

例如当我输入时

9 丁天晃

到第一个框,它将显示 5 个结果。但是当我在第二个框中输入相同的查询时,它只会显示 2 个结果。

我的问题是他们为什么会有这种不同,我该如何修复,以便我的自定义预输入可以完美地作为内置的自动完成功能

【问题讨论】:

    标签: javascript google-maps-api-3


    【解决方案1】:

    对我来说,service.getPlacePredictions() 也返回 5 个结果。问题出现在$.typeahead,因为当您搜索9 dinh tien hoang 并且服务返回9 Đinh Tiên Hoàng 时,查询字符串与结果不匹配(例如,搜索d 将不匹配Đ,它是一个完全不同的角色)。

    由于您根本不需要对 $.typeahead() 进行任何过滤(因为 autocompleteService 已经返回过滤结果),您可以将其添加到 $.typeahead-options 中:

    matcher:function(){return true;}
    

    【讨论】:

    • matcher: 我在 AutocompleteService 文档中找不到这个参数。
    • @StevenAguilar:这是一个$.typeahead-issue
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 2014-11-24
    • 2016-11-17
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多