DeryckOE DeryckOE - 1 year ago 68
jQuery Question

Filter Google Maps autocomplete to Barcelona, Spain

I need to filter results, to Barcelona, Spain not to Spain as is seen in code.

autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('autocomplete')),
{
types: ['geocode'],
componentRestrictions: {
country: 'es',
}
});


I tried with postalCode using 08 (Barcelona prefix) and 08000 inside componentRestrictions and does not work.

There is away I can filter only to Barcelona address in autocomplete?

Answer Source

You would have to set bounds like as below:

var cityBounds = new google.maps.LatLngBounds(
  new google.maps.LatLng(lat, lng),
  new google.maps.LatLng(lat, lng));

var options = {
  bounds: cityBounds,
  types: ['geocode'],
  componentRestrictions: {country: 'es'}
};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download