2
votes

I have tryied to used Google Maps strictbounds parameter to restrict autocomplete results to an specific area. My current code is:

var autocomplete = new google.maps.places.Autocomplete(spaceAddress, { types: [ 'geocode' ], strictbounds: {location: "-23.544085,-46.7125434", radius: "12000"}, language: "pt-BR" });

Althoug it doesn't break my application, it also doesn't restrict the results to the selected area. Alternatively, I used the country restriction below, but the customer doesn't like it!

Here's the complete code in use by now:

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);
      }
    }
    });
  }

Could someone please enlighten me?!

1
The strictbounds parameter accepts boolean values. Have a look at the example in stackoverflow.com/a/46989785/5140781. - xomena

1 Answers

2
votes

You need to create a google.maps.Circle class first and then use that object to get the bounds via getBounds(). Also as mentioned already strictbounds is a boolean 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 });