Google Maps Autocomplete strictbounds参数不起作用

时间:2018-11-13 13:52:20

标签: google-maps autocomplete

我尝试使用Google Maps strictbounds参数将自动填充结果限制在特定区域。我当前的代码是:

var autocomplete = new google.maps.places.Autocomplete(spaceAddress,{类型:['geocode'],严格限制:{位置:“ -23.544085,-46.7125434”,半径:“ 12000”},语言:“ pt -BR“});

虽然它不会破坏我的应用程序,但也不会将结果限制在所选区域内。另外,我使用了下面的国家/地区限制,但客户不喜欢它!

这是目前正在使用的完整代码:

document.addEventListener("DOMContentLoaded", function() {
  var spaceAddress = document.getElementById('space_address');
  if (spaceAddress) {
  var autocomplete = new google.maps.places.Autocomplete(spaceAddress, { types: [ 'geocode' ], strictbounds: {location: "-23.544085,-46.7125434", radius: "12000"}, language: "pt-BR" });
  google.maps.event.addDomListener(spaceAddress, 'keydown', function(e) {
    if (e.key === "Enter") {
      e.preventDefault();
      const spaceAddress = document.getElementById('space_address').value;
      if (spaceAddress != '' && spaceAddress != null) {
      codeAddress(spaceAddress);
      }
    }
    });
  }

有人可以启发我吗?!

1 个答案:

答案 0 :(得分:0)

您需要先创建一个google.maps.Circle类,然后使用该对象通过getBounds()获取边界。就像已经提到的,strictbounds是一个布尔值 https://developers.google.com/maps/documentation/javascript/reference/places-widget#Autocomplete

var circle = new google.maps.Circle({ center: new google.maps.LatLng(-23.544085, -46.7125434), radius: 12000 })
var autocomplete = new google.maps.places.Autocomplete(spaceAddress, { types: [ 'geocode' ], bounds: circle.getBounds(), strictbounds: true });