I'm having an issue with the Google Maps Javascript API's Geocoder class and the results it returns when using componentRestrictions
.
In a nutshell, I'm trying to allow a user to enter an address and find that address within a specific locality.
I pass a String as an address to the geocode object literal.
I then restrict the search to only "New York" city using the locality
property within componentRestrictions
.
So my code looks like this:
geocoder.geocode(
{address: address,
componentRestrictions: {
locality: 'New York'
}
}, function(results, status) {
console.log(results);
}
);
When passing a specific address, such as 'E 64th St & 5th Ave'
, it returns the correct result of the location of the Central Park Zoo.
However, if I use something more obscure for the address, such as 'East Village'
, it returns a result of the location of New York city hall.
If I remove the componentRestrictions
property, and pass the same address as 'East Village'
, it returns the correct location for East Village.
What makes this stranger is I'm using code provided in a course through Udacity, taught by Google developers. Their code can be found here on GitHub.
When I run their code exactly as they provide it, I run into the same issue as my own code.
But in their course video, it works correctly.
It seems that some of the API might have changed since they created this course, so it is possible I am using the componentRestrictions incorrectly.
Has anyone experienced an issue like this and found a solution?
Or am I using the code incorrectly?
Thanks for your help!
Google has released the new version of geocoder in November 2016. The new version doesn't have full support for address components.
Take a look at this document that explains how the component filtering works in new version:
https://developers.google.com/maps/documentation/geocoding/faq#trbl_component_filtering
In the new geocoder, component filtering is only fully supported for address-level results. There is partial support for country code (for example, components=country:GB) and postal code restrictions for locality and higher-level results.