Can I restrict google geocoder results by cities and towns?
Asked Answered
S

1

6

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)']
});
Secondly answered 21/7, 2019 at 7:3 Comment(5)
locality filter is for cities and towns, but note that this filter is not strict, it just a bias to search address within locality. The actual result might be outside. Also, geocoding typically returns only one result (best-match), it is not like autocomplete service. If your intention is getting list of cities from geocoding service, it won't work.Elenaelenchus
My intent is to fetch just one result, but limit it to a country, like I'm doing and by a city and/or town. I don't want the user, who enters text into a input, to be able to fetch a result as a State or Country. This is for a dating site and I'm trying to get the closest approximate location (city/town) without having the user enter in an exact address!!Secondly
If I can use locality, can you please provide an example?Secondly
Not sure if it works in all situations, but something like https://maps.googleapis.com/maps/api/geocode/json?components=locality%3AHouston%7Ccountry%3AUS&key=YOUR_API_KEY seems to return city 'Houston, TX, USA'.Elenaelenchus
this is the url, I need an example using the API programmaticlySecondly
B
3

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!

Bagging answered 30/7, 2019 at 2:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.