谷歌地图自动完成边界不起作用

bar*_*oma 6 javascript google-maps autocomplete google-maps-api-3

我想在Google地图服务上使用自动填充边界.但不行.

//----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
Run Code Online (Sandbox Code Playgroud)

当我键入文本框时,地址将来自XX国家/地区.但我想限制在一个国家的地区搜索.例如一个城市范围.但其他地址正在走出这个界限.不考虑界限.

Die*_*yol 6

问题是LatLngBounds需要西南角和东北角.也称为左下角和右上角.

代替:

var defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(40.518, 29.215),
    new google.maps.LatLng(41.242, 30.370));
Run Code Online (Sandbox Code Playgroud)

它应该是:

var defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(41.242, 29.215),
    new google.maps.LatLng(40.518, 30.370));
Run Code Online (Sandbox Code Playgroud)


Pre*_*ari 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
Run Code Online (Sandbox Code Playgroud)