我试图通过设置一个国家/地区componentRestrictions
和半径来将自动完成结果限制为一个城市radius
,如下所述:
https ://developers.google.com/maps/documentation/javascript/places
不幸的是,国家限制有效,而不是半径(在这种情况下,它应该将地址限制在 500 米内)!你能帮助我吗..?
<script src="https://maps.googleapis.com/maps/api/js?&sensor=false&region=it&libraries=places"></script>
<script>
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var myLatlng = new google.maps.LatLng(41.88994,12.51383);
var mapOptions = {
zoom:16,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: myLatlng,
styles: [
{
featureType: "poi.business",
elementType: "labels",
stylers: [
{ visibility: "off" }
]
}
]
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('directions-panel'));
var input = document.getElementById('start');
var options = {
types: ['geocode'],
componentRestrictions: {country: 'it'},
radius: '500',
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
autocomplete.bindTo('bounds', map);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'My Marker'
});
}
function calcRoute() {
var start = document.getElementById('start').value;
if (start.indexOf("Roma") === -1) {
start += ", Roma";
}
var end = "Via Tiburtina 500, Roma";
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.TRANSIT
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<form onsubmit="calcRoute(); return false">
<input type="text" id="start" name="start" placeholder="Indirizzo di partenza" style="margin-left:5px;width:200px">
<input type="submit" value="Vai">
</form>
<div id="map-canvas" style="width: 927px; margin-top:5px;margin-left:5px; height: 300px; position: relative;"></div>
<div id="directions-panel"></div>
</body>
</html>