StackOverflowNewbie
StackOverflowNewbie

Reputation: 40653

Google Maps Autocomplete: how to be more restrictive than country?

Is there a way to make Google Map's autocomplete restrict results to, say, just within a state? It seems that it can only be restricted by country: https://developers.google.com/maps/documentation/javascript/reference#ComponentRestrictions.

If there is no way to do it with Google's autocomplete, is there a jQuery plugin that will do this for me? I need a good autocomplete plugin that will hopefully return the entire map API response (or at least lat/lng vallues).

Upvotes: 1

Views: 2426

Answers (1)

Sean Mickey
Sean Mickey

Reputation: 7716

You have the option of setting bounds rather than using Component Restrictions when you construct the Autocomplete. It is described in the Autocomplete section of the Developer's Guide and coding specifications are defined in the AutocompleteOptions entry in the API Doc. You may also change the bounds after the Autocomplete has been created by calling: autocomplete.setBounds(bounds).

The bounds aren't guaranteed to limit results to results within the bounds, but setting the bounds to the area that covers a state or city will come very close to achieving what you describe.

If you need to go further, you also have the option of switching from the Autocomplete to using the Places Librarydev-guide, which also allows you to define the rankBy optional property. If you use the google.maps.places.RankBy.DISTANCE to define rankBy, you get added control over limiting the results returned to a specific geographic area. These are about the best options currently available on Google Maps.

Upvotes: 2

Related Questions