如何在某个国家/地区指定自动填充结果?

时间:2013-08-01 13:49:46

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

我有与文档中相同的自动完成脚本: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

在include脚本中指定区域或languageregionhl无效。我怎样才能将结果偏向法国?

1 个答案:

答案 0 :(得分:2)

您必须使用location biasing

更改

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

to:

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 () {
  //...
});