7

我想在谷歌地图服务上使用自动完成边界。但不工作。

//----autocomplete start
var input = (document.getElementById('searchPlace'));
var defaultBounds = new google.maps.LatLngBounds(
            new google.maps.LatLng(40.518, 29.215),
            new google.maps.LatLng(41.242, 30.370));

var options = {
            bounds:defaultBounds,
            componentRestrictions: { country: 'XX'},};

var autocomplete = new google.maps.places.Autocomplete(input, options);
//----autocomplete end

当我输入文本框时,地址来自 XX 国家。但我想限制在某个国家/地区的搜索。例如城市边界。但是其他地址正在超出此范围。不考虑界限。

4

5 回答 5

8

问题是LatLngBounds预计会出现西南角和东北角。也称为左下角和右上角。

代替:

var defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(40.518, 29.215),
    new google.maps.LatLng(41.242, 30.370));

它应该是:

var defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(41.242, 29.215),
    new google.maps.LatLng(40.518, 30.370));
于 2016-11-13T04:07:45.503 回答
2

更新于 10/2018:此答案已过时;请看其他答案

它不会如你所愿。这就是文档所说的:

搜索地点的区域。结果偏向但不限于包含在这些范围内的地方。

https://developers.google.com/maps/documentation/javascript/places-autocomplete#address_forms

于 2014-01-29T13:11:30.023 回答
0

Bounds 仅适用于 maps API,不适用于 Places API。

对于Places API 中的位置偏差,您需要在选项中使用位置 latlng 对象和半径(以米为单位)。

        var myLatlng = new google.maps.LatLng(35.9907,-84.0497);

        var acOptions = {
            location: myLatlng,
            radius: 150000,  // (in meters; this is 150Km)
            types: ['address'],
            componentRestrictions: {country: 'us'}
        };

请注意,这只会使自动完成结果偏向您输入的半径;它不会将结果限制在该半径范围内。

于 2015-01-21T18:32:46.720 回答
0

也许当您使用边界时,您不能使用 componentRestrictions ...

尝试这个..

    //----autocomplete start
    var input = (document.getElementById('searchPlace'));
    var defaultBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(40.518, 29.215),
        new google.maps.LatLng(41.242, 30.370));

    var options = {
        bounds:defaultBounds
    };
    var autocomplete = new google.maps.places.Autocomplete(input, options);
    //----autocomplete end
于 2013-09-21T09:35:55.240 回答
0
var defaultBounds = new google.maps.LatLngBounds(
                new google.maps.LatLng(17.2916377 ,78.2387067),
                new google.maps.LatLng(17.5608321, 78.6223912)
);

var input = document.getElementById('destination');
var options = {
  bounds: defaultBounds,
  types: ['establishment'],
  strictBounds: true
};

var destination_autocomplete = new google.maps.places.Autocomplete(input, options)
于 2019-07-30T07:14:55.500 回答