【问题标题】:How to specify the autocomplete results in a certain country?如何指定某个国家的自动完成结果?
【发布时间】:2013-08-02 11:41:27
【问题描述】:

我有与文档中相同的自动完成脚本:https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete

我的包含脚本:

<script type="text/javascript" src="//maps.google.com/maps/api/js?v=3.8&sensor=false&libraries=geometry,places&language=&hl=&region=FR"></script>

javascript:

function initAutocomplete(callback, input_s, lat_s, lng_s, label_s) {

    var input = $(input_s)[0];
    var autocomplete = new google.maps.places.Autocomplete(input);
    //autocomplete.bindTo('', search_map.map);
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
        var place = autocomplete.getPlace();
        if (place.geometry) {
            $(lat_s).val(place.geometry.location.lat());
            $(lng_s).val(place.geometry.location.lng());
            $(label_s).val(place.formatted_address);
            if (callback) {
                callback();
            }

        }
    });
}

initAutocomplete(somefunction, '#zipcode_search_input', "#search_lat", "#search_lng", "#search_address_label");

我希望结果偏向于法国城市。例如:如果我输入“laval”,我希望首先出现Laval, France,然后是Laval, Canada

在包含脚本中指定区域或languageregionhl 没有帮助。我如何才能将结果偏向法国?

【问题讨论】:

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


    【解决方案1】:

    你必须使用location biasing

    更改:

    var autocomplete = new google.maps.places.Autocomplete(input);
    

    收件人:

    var autocomplete = new google.maps.places.Autocomplete(input, 
       {componentRestrictions: {country: 'fr'}});
    

    编辑: componentsRestrictions 实际上限制了特定国家/地区的结果。改为偏向结果(仍然显示来自其他国家/地区的结果,但优先级较低),请执行以下操作:

    Grab sw and ne lat/lng here

    var southWest = new google.maps.LatLng(42.97250, -3.82324);
    var northEast = new google.maps.LatLng(51.64529, 5.49316);
    var bounds = new google.maps.LatLngBounds(southWest,northEast);
    var autocomplete = new google.maps.places.Autocomplete(input, {bounds: bounds});
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
      //...
    });
    

    【讨论】:

      猜你喜欢
      • 2012-01-07
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 2021-08-04
      • 2017-03-20
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多