I would like to have an input field on my website with autocomplete for places within a certain country. For example in the code below I want to get only places within the country ‘The Netherlands’ as suggestions in the autocomplete.
In the code below I’m getting suggestions for places in all countries, not specified for a certain country.
<!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>
You can set a bias (which you already have) but without actually checking each autocomplete result with additional code you cannot limit the autocomplete list to a certain country or area, to my knowledge.
Here is some more information on Google’s Autocomplete: http://code.google.com/apis/maps/documentation/places/autocomplete.html#place_autocomplete_requests
Ah, if you want to use the geo-autocomplete plugin, then take a look at this example:
view-source:http://geo-autocomplete.googlecode.com/svn/trunk/demo/ui.demo.html
This is from their example: