简体   繁体   中英

How to restrict Google Places autocomplete API by a radius within a certain longitude and latitude?

I am using this code mentioned in docs https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform

How should I strictly restrict results by a certain radius about a coordinate?

Google's code example is biasing Autocomplete results to the user's geographical location using setBounds() :

function geolocate() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var geolocation = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };
      var circle = new google.maps.Circle({center: geolocation, radius: position.coords.accuracy});
      autocomplete.setBounds(circle.getBounds());
    });
  }
}

To actually restrict Autocomplete results to the given bounds, use strictBounds :

autocomplete.setOptions({strictBounds: true})

Hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM