Ben Davidow
Ben Davidow

Reputation: 1215

Bias location returns on Google Places API autocomplete

I am using the Google places API to autocomplete street address searches. I want to bias the results to a 50 mile radius around a specific longitude and latitude (this is a fixed location, it doesn't depend on the user). Currently, it autocompletes addresses, but it doesn't bias them based on location (other than doing within the U.S.). Specifically, I want to bias a 300 mile radius around latitude: 34.159947, longitude: -118.257017.

<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
        ...
<input id="address-search" type="text" value="" name="subject">


$(document).ready(function() {
    var input = document.getElementById('address-input');
    var options = {componentRestrictions: {country: 'us'} };               
    new google.maps.places.Autocomplete(input, options); 
    google.maps.event.addDomListener(window, 'load', initialize);
});

       

Upvotes: 1

Views: 1901

Answers (1)

maged
maged

Reputation: 889

You can't give it a radius, but you can give it rectangular bounds

var defaultBounds = new google.maps.LatLngBounds(
  new google.maps.LatLng(-33.8902, 151.1759),
  new google.maps.LatLng(-33.8474, 151.2631)
);

var options = {bounds: defaultBounds };  

See the Places Autocomplete documentation

Upvotes: 3

Related Questions