【问题标题】:Google Places AutocompleteService filtering by countryGoogle Places AutocompleteService 按国家/地区过滤
【发布时间】:2013-02-02 05:40:39
【问题描述】:

我正在设置一个自定义自动完成字段,在该字段中显示来自 Google 地方信息的位置以及来自我的数据库的与搜索查询匹配的事件。出于这个原因,我使用 Google Places Autocomplete Service 来获取查询预测,而不是直接将 Places Autocomplete 插入到我的文本字段中。

问题是我不知道如何使用自动完成服务按国家/地区过滤地点自动完成建议。

我试过了:

var service = new google.maps.places.AutocompleteService(null, {
        types: ['cities'],
        componentRestrictions: {country: "au"}
    });

但它仍然显示来自德国和法国的自动完成选项:(

有什么建议吗?

【问题讨论】:

    标签: google-places-api


    【解决方案1】:

    您需要在调用服务时通过限制,而不是在创建服务时。这里:

    //create service
    service = new google.maps.places.AutocompleteService();
    
    //perform request. limit results to Australia
    var request = {
        input: 'Brisbane',
        componentRestrictions: {country: 'au'},
    };
    service.getPlacePredictions(request, callback);
    

    【讨论】:

    • 你好罗伯特,你是如何以及在哪里定义你的callback的?
    • @chaeschuechli 看看official example。函数displaySuggestions是回调函数。
    • @Apoorv 注意 service.getPlacePredictions 在传递 request.componentRestrictions 时有效,但 service.getQueryPredictions 似乎没有
    • 可以给城市添加限制吗?例如componentRestrictions: { city: 'nyc'}?
    • .AutocompleteService() 没有 strictBounds 参数。我想知道是否有一个参数可以根据严格的界限来限制结果。
    【解决方案2】:

    您可以使用AutocompletionRequest 指定类型和组件限制。这是一个例子-

    var service = new google.maps.places.AutocompleteService();
    
        service.getPlacePredictions({ input: query, types: ['geocode'], 
                                    componentRestrictions:{ country: 'us' } },
                function (predictions, status) {
                    if (status == google.maps.places.PlacesServiceStatus.OK) {
                        var results = document.getElementById('results');
                        for (var i = 0, prediction; prediction = predictions[i]; i++) {
                            results.innerHTML += '<li>' + prediction.description + '</li>';
                        }
                    }
                });
    

    【讨论】:

      【解决方案3】:

      使用此工作代码

      var input = document.getElementById("query_value");
      var autocomplete = new google.maps.places.Autocomplete(input, {
        componentRestrictions: { country: "IN" },
        place_id: "YOUR_PLACE_ID"
      });
      google.maps.event.addListener(autocomplete, "place_changed", function() {
        var place = autocomplete.getPlace();
      });
      
      
      https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places&language=en&key=YOUR_KEY
      

      【讨论】:

        【解决方案4】:

        您应该使用此代码。

        var options = {
            types: ['(cities)'],
            componentRestrictions: {country: ["in"]}
        }
        //Find From location    
        var input= document.getElementById('input_box_city');
        var fromAuto = new google.maps.places.Autocomplete(input, options);
        

        【讨论】:

        • 这是用于 Autocomplete 类,而不是 AutocompleteService 类。 AutocompleteService 类允许您以编程方式处理结果,而 Autocomplete 类为您装饰输入元素。
        【解决方案5】:

        如文档reference 中所述,地点库AutocompleteService 不支持AutocompleteOptions。如果您认为这是一项有价值的功能,请提交Places API - Feature Request

        【讨论】:

        • 您能否更具体地说明文档中概述的位置?据我所知,您可以按国家/地区进行限制。
        • 是的,我也刚刚检查了“componentRestrictions”
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-28
        • 1970-01-01
        • 2015-11-27
        • 2016-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多