joe
joe

Reputation: 2319

geocode component filtering with field "locality" not working as expected

I'm using Google Map's Geocode API to get the lat, lng values for this address in Manhattan: 123 Dr Martin Luther King Jr Blvd, New York, NY 10035, USA

My initial query looks like this: https://maps.google.com/maps/api/geocode/json?address=123%20mlk%20blvd

This query does not return my desired address, so in order to filter down results to only those in Manhattan I modified by request to include the Component Filtering parameter locality:Manhattan.

As defined in the docs here: https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering

So my question is - why does this url: https://maps.google.com/maps/api/geocode/json?address=123%20mlk%20blvd&components=locality:Manhattan not return my desired address?

And yet - using the component filter postal_code:10035 does return my desired address: https://maps.google.com/maps/api/geocode/json?address=123%20mlk%20blvd&components=postal_code:10035

Thanks

Upvotes: 2

Views: 3839

Answers (1)

xomena
xomena

Reputation: 32178

I think the main detail you should be aware of is that Google changed behaviour of components filters recently. This change was made on January 15, 2018. The documentation contains the following text now:

Filters on route, locality, and administrative_area influence the results but do not guarantee containment in the same way as an address query.

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

That means that currently only postal code and country continue to be enforced strictly for components filters. The Component Filtering in the Geocoding API is no longer as strict as it was before. This change explains behaviour of your sample requests. Component filter with locality is not strict, so you get different result, while component filter with postal code is strict and you get expected result.

If you need a locality you should include it in the address parameter of your request to make it less ambiguous.

E.g.

https://maps.googleapis.com/maps/api/geocode/json?address=123%20mlk%20blvd%20Manhattan&key=YOUR_API_KEY

You should follow the best practices of Geocoding API that are available at:

https://developers.google.com/maps/faq#geocoder_queryformat https://developers.google.com/maps/documentation/geocoding/best-practices

I hope my answer explains your doubts!

Upvotes: 3

Related Questions