I am trying to add an Google Geocode Autocomplete field to my form.
I have been able to get it to restrict the results returned to the country (AU) but it would be e
Keep a list of state lat/long boundaries in your app and restrict autocomplete with them.
var stateBounds={
ca: ["32.812736", "-119.216815", "34.608128", "-117.039301"]
// lat/long boundaries list for states/provinces.
};
function getStateBounds(state) {
return new google.maps.LatLngBounds(
new google.maps.LatLng(stateBounds[state][0],
stateBounds[state][1]),
new google.maps.LatLng(stateBounds[state][2],
stateBounds[state][3])
);
}
new google.maps.places.Autocomplete(document.getElementById('search'), {
types: ['address'],
componentRestrictions: {country: 'us'},
bounds: getStateBounds('ca') // get LatLngBounds for ca.
});
I use autocomplete without any sort of filter on it but automatically append the state I'm interested in to all of the user input before sending the query.
For someone who wants California results, for example, if they were to search for Sacramento
it would automatically correct the input to Sacramento, California
and this has worked somewhat well for me. You could be even more specific with the data you send, for example Sacramento, California, USA