0

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

在包含脚本中指定 region or language, regionorhl没有帮助。我怎样才能使结果偏向法国?

4

1 回答 1

2

您必须使用位置偏差

改变:

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

至:

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

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

在此处获取 sw 和 ne lat/lng

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 () {
  //...
});
于 2013-08-01T13:49:45.773 回答