Google GeoCode Autocomplete limit State
Asked Answered
O

2

5

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 even better if I could limit the results to the state as well. (New South Wales/NSW)

This is my current initialize() function:

function initialize() {
var options = {
types: ['geocode'],
    componentRestrictions: {country:"AU"}
};
  autocomplete = new google.maps.places.Autocomplete(
  (document.getElementById('autocomplete')),options);
  google.maps.event.addListener(autocomplete, 'place_changed', function() {
  fillInAddress();
  });
}

I have read the documentation and found that you can use the types parameter to restrict the results, but I have been unable to work out what the exact code should be.

Simply changing it to:

types: ['administrative_area1':'NSW'], // OR 'New South Wales'

and various combinations similar to the above (= instead of : etc) have not worked..

Could someone point me in the right direction towards what the actual syntax is for what I am trying to achieve?

Thanks.

Orestes answered 19/2, 2014 at 4:40 Comment(9)
When you've read the documentation you also should have read this: Currently, you can use componentRestrictions to filter by countryGrenada
I did. and I have implemented that part and said that it works. What I am asking is can I filter it further to only show results within a particular state (NSW).Orestes
When it would be possible to filter by state they would have written you can use componentRestrictions to filter by country and state.Grenada
from the API page: You may restrict results from a Place Autocomplete request to be of a certain type by passing a types parameter. The parameter specifies a type or a type collection, as listed in the supported types below. The supported types are: ... administrative_area1 For Australia, administrative_area1 is the state. I am just curious how to use the type parameter to restrict the state returned.Orestes
Please read the documentation thoroughly. The supported types are : geocode, establishment, (cities) and (regions), nothing else. The types-property may not be used to specify the value of a type(e.g. a specific region), it's only meant to filter the results to results that contain the specific component(e.g. a region), it doesn't care about the value of this component.Grenada
@Grenada just to be clear, you are saying it's not possible to limit results to a specific state, correct?Site
Jake, if you post some code to JS Bin / JSFiddle I'll take a stab at this.Press
@NextLocal Thanks, but this is now from an old project I no longer work on.Orestes
Hey friend! See my answer here, hope it helps! https://mcmap.net/q/2030831/-restrict-places-autocomplete-results-to-within-country-and-stateDonitadonjon
P
4

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

Press answered 24/11, 2015 at 3:5 Comment(1)
I know this is an old answer, but there still isn't any default state filtering provided. I was wondering, at which point/place do you append the State? Provided the init function call is autocomplete = new google.maps.places.Autocomplete(document.getElementById 'idAcInput'), this seems to read data continuously. Where do you hook the text in?Cheliform
P
6

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.
    });
Portsalut answered 4/4, 2018 at 1:5 Comment(4)
This is the best method for limiting to a particular state. Cheers!Pooh
@Conard Warhol, I tried the same for "Maryland" but it did not work, could you please help. I t did work for ca Hera are the co-ordinates I used for Maryland - var stateBounds={ md: ["39.0457549", "-76.64127119999999"] // lat/long boundaries list for states/provinces. };Juliannejuliano
md: [ "37.8889", "-79.4861", "39.722", "-74.8581"] should work.Portsalut
In my case this config was not enough. In addition I had to append strictBounds: true. So according to the google dock, this option exactly restricts search by the rectangle you provideMaker
P
4

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

Press answered 24/11, 2015 at 3:5 Comment(1)
I know this is an old answer, but there still isn't any default state filtering provided. I was wondering, at which point/place do you append the State? Provided the init function call is autocomplete = new google.maps.places.Autocomplete(document.getElementById 'idAcInput'), this seems to read data continuously. Where do you hook the text in?Cheliform

© 2022 - 2024 — McMap. All rights reserved.