我想在我的网站上输入一个字段,该字段可以自动填写特定国家/地区内的地方。例如,在下面的代码中,我想仅获取“荷兰”国家/地区内的位置作为自动填充建议。
在下面的代码中,我正在为所有国家/地区的地方提供建议,而不是为某个国家/地区指定。
<!DOCTYPE html>
<html>
<head>
<title>Google Maps JavaScript API v3 Example: Places Autocomplete</title>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"
type="text/javascript"></script>
<style type="text/css">
body {
font-family: sans-serif;
font-size: 14px;
}
#map_canvas {
height: 400px;
width: 600px;
margin-top: 0.6em;
}
</style>
<script type="text/javascript">
function initialize() {
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input, {country: 'NL'});
autocomplete.bindTo('bounds', map);
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
map: map
});
google.maps.event.addListener(autocomplete, 'place_changed', function() {
infowindow.close();
var place = autocomplete.getPlace();
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17); // Why 17? Because it looks good.
}
});
setupClickListener('changetype-all', []);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div>
<input id="searchTextField" type="text" size="50">
<label for="changetype-all"></label>
</div>
</body>
</html>
最佳答案
您可以设置一个偏差(已有的偏差),但据我所知,如果不使用附加代码实际检查每个自动完成结果,就无法将自动完成列表限制为某个国家或地区。
以下是有关Google自动完成功能的更多信息:http://code.google.com/apis/maps/documentation/places/autocomplete.html#place_autocomplete_requests
嗯,如果您想使用geo-autocomplete插件,请看以下示例:
查看源:http://geo-autocomplete.googlecode.com/svn/trunk/demo/ui.demo.html
这是从他们的例子:
$('#demo3_location').geo_autocomplete({
geocoder_region: 'Africa',
geocoder_types: 'country',
mapheight: 100, mapwidth: 200, maptype: 'hybrid'
});