【问题标题】:google maps autocomplete bounds not working谷歌地图自动完成边界不起作用
【发布时间】:2013-09-25 11:30:51
【问题描述】:

我想在 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

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

【问题讨论】:

  • From the docs :结果偏向于,但不限于,包含在这些范围内的地点。

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


【解决方案1】:

问题是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));

【讨论】:

  • 这在 2018 年仍然正确。东南 x 西北不会削减它。
  • 还将 strictBounds:true 添加到选项中
【解决方案2】:

2018 年 10 月更新:此答案已过时;请查看其他答案

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

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

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

【讨论】:

  • 上面的链接好像坏了。备用网址为developers.google.com/maps/documentation/javascript/…
  • 现在他们添加了一个名为“strictBounds”的新布尔参数。如果设置为 true,则 API 不会返回此区域之外的结果,即使它们与用户输入匹配。
【解决方案3】:

也许当您使用边界时,您不能使用 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

【讨论】:

    【解决方案4】:

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

    对于 Places API 中的 location biasing,您需要在选项中使用位置 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'}
            };
    

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

    【讨论】:

      【解决方案5】:
      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-29
        • 1970-01-01
        • 1970-01-01
        • 2017-03-08
        • 1970-01-01
        • 1970-01-01
        • 2018-03-03
        • 1970-01-01
        相关资源
        最近更新 更多