Reputation: 14540
There are component restrictions for the google geocoding api.
admininstrativeArea, country, locality, postalCode, route
can I use any of these to filter by city and town? So that I don't get back a country or state?
Something like
this.geocoder = new google.maps.Geocoder();
this.geocoder.geocode({
address: this.registerForm.get('city').value,
componentRestrictions: {
country: 'US',
locality // not sure here
}
},
(results, status) => {
if (status === google.maps.GeocoderStatus.OK) {}
}
I can do it with google maps autocomplate!
this.autocomplete = new google.maps.places.Autocomplete(this.element, {
types: ['(cities)']
});
Upvotes: 6
Views: 3534
Reputation: 717
Geocoding API's component filtering actually behaves differently from the Places API -- Place Types.
Component filtering lets you return addresses restricted to a specific area. Take for example, you search for "High St Hasting" with component restriction to country: 'GB'
, the request will return the closest address match within the country "United Kingdom". The returned address may be of a type street address, neigborhood, country or other types.
Places APIs place types on the other hand, lets you return addresses that matches the given type. For example, you want to search for addresses of type "establishment" only.
I understand this feature is what matches your use-case in which you wanted to restict results to be of type cities or towns only. This feature is not currently supported by the Geocoding API.
As a workaround, you can use Places Autocomplete and bias the autocomplete results to favor an approximate location or area as stated in this documentation. Your user can then select the correct location from the autocomplete suggestions.
var options = {
types: ['(cities)'],
componentRestrictions: {country: 'us'}
};
Place Autocomplete is also recommended if you search for ambiguous (incomplete) addresses, like when responding to user input. More details here.
Hope this helps!
Upvotes: 3