简体   繁体   中英

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

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

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