3

I'm having trouble adding the componentRestrictions parameter to the Google Maps Geocoder object. I'm using the JavaScript client-side api. Specifically, the administrativeArea property isn't working for me. Docs say type is a string, and I've tried many combinations but still no luck.

Here is my Geocoding function:

function geocodeAddress(geocoder, resultsMap) {
    var address = document.getElementById('address').value;
    geocoder.geocode({'address': address, 'componentRestrictions': {administrativeArea: 'Maricopa County'}}, function(results, status) {
      if (status === 'OK') {
        resultsMap.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: resultsMap,
          position: results[0].geometry.location
        });
        console.log(results)
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    });
  }

I can get other componentRestriction properties to work, like the postalCode and country, but not administrativeArea. Docs say the property matches all the administrative_area levels, so I've tried using different US counties and different US states, but can't get it to work. I figure I'm getting the syntax wrong, any help would be much appreciated!

xomena
  • 27,383
  • 5
  • 77
  • 109
doublea
  • 379
  • 1
  • 2
  • 11

1 Answers1

4

Your code is correct. Unfortunately, there was an important change in components filtering behavior some time ago. It looks like Maps JavaScript API documentation is not updated regarding components restrictions yet.

I would suggest having a look at corresponding web service documentation that is updated and says the following

The components that can be filtered include:

  • postal_code matches postal_code and postal_code_prefix.

  • country matches a country name or a two letter ISO 3166-1 country code. Note: The API follows the ISO standard for defining countries, and the filtering works best when using the corresponding ISO code of the country.

The following components may be used to influence results, but will not be enforced:

  • route matches the long or short name of a route.

  • locality matches against locality and sublocality types.

  • administrative_area matches all the administrative_area levels.

source: https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering

So, as you can see administrative area is not a strict filter anymore. The only available strict filters at the moment are postal code and country.

I hope my answer clarifies you doubt.

Community
  • 1
  • 1
xomena
  • 27,383
  • 5
  • 77
  • 109
  • 1
    Hmm, yes this does help some, but it doesn't even seem to be influencing my results. E.g. There is a Peoria, IL and a Peoria, AZ, when I search "peoria" with no filters it returns Peoria, IL, but adding `administrativeArea: 'Arizona'` still returns Peoria, IL. If I add the short name, `administrativeArea: 'AZ'` I get the `ZERO_RESULTS` error. Maybe I'll try the viewport biasing to see if I get better results. I'll mark your answer as correct, because this doesn't seem to be a syntax issue – doublea Sep 05 '18 at 13:46
  • 1
    If you believe the observed behavior is unexpected, please [file a bug](https://issuetracker.google.com/issues/new?component=188871&template=788905) in Google issue tracker. – xomena Sep 05 '18 at 15:24