【发布时间】:2018-09-04 02:07:54
【问题描述】:
如何仅针对美国(国家/地区)限制自动完成建议。到目前为止,这是我的代码,
<div class="search-result">
<form action="" method="post" class="clearfix">
<input type="text" placeholder="Enter City or Zip Code" class="searchBox" id="search-result" autocomplete="on">
<button type="submit" class="btn btn-brown"></button>
</form>
<div class="marker-list">
<ul>
</ul>
</div>
</div>
下面是 Javascript 代码。请注意,还有更多代码,并且无需过滤美国的结果即可使用,
inputResult = document.getElementById("search-result"),
searchBoxResult = new google.maps.places.SearchBox(inputResult),
searchBoxResult.addListener("places_changed", function () {
// Clear old markers
markers.forEach(function(place) {
place.setMap(null);
});
markers = [];
var
places = searchBoxResult.getPlaces(),
bounds = new google.maps.LatLngBounds(),
searchPlace = new google.maps.LatLng({lat: places[0].geometry.location.lat(), lng: places[0].geometry.location.lng()});
$(".search-result form").submit();
getPlacesList(searchPlace, customPlaces, searchRadius);
var placesFilter = function (element) {
return distance(element.LatLng, searchPlace) < searchRadius;
};
var j=0;
customPlaces.forEach(function (place) {
if(place.distance<=20){
createMarker(place,j++);
}
});
customPlacesFiltered = customPlaces.filter(placesFilter);
customPlacesFiltered.forEach(function(place) {
bounds.extend(place.LatLng);
});
map.fitBounds(bounds);
});
我的 api 请求是,
<script src="https://maps.googleapis.com/maps/api/js?key=xxxx&libraries=places,geometry&callback=initMap&sensor=false&libraries=geometry,places&components=country:us&ext=.js" async defer></script>
如何使用此方法将结果限制为仅适用于美国。请帮忙。
【问题讨论】:
-
请帮忙。我为此花了几个小时
-
@geocodezip 其实我不想用 maps.Autocomplete
-
喜欢使用边界
-
请修正您问题的标题。
标签: javascript google-maps google-places-api