Bias位置返回Google Places API自动填充功能

时间:2013-08-21 18:25:26

标签: javascript jquery google-places-api

我正在使用Google地方信息API来自动填写街道地址搜索。我想将结果偏向特定经度和纬度周围50英里的半径(这是一个固定的位置,它不依赖于用户)。目前,它会自动填充地址,但不会根据位置偏向它们(除了在美国境内)。具体来说,我想偏向北纬300英里半径:34.159947,经度:-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);
});

1 个答案:

答案 0 :(得分:2)

你不能给它一个半径,但你可以给它一个矩形边界

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

请参阅Places Autocomplete documentation