Restrict Google Places Autocomplete to return addresses only
Asked Answered
A

3

35
autocomplete = new google.maps.places.Autocomplete(input, { types: ['geocode'] });

returns streets and cities amongst other larger areas. Is it possible to restrict to streets only?

Astrophysics answered 25/2, 2013 at 16:26 Comment(1)
hmm, can you explain? Google places gives you list of places by defined radius and category. Please give an example what you tried yetAlbite
W
11

This question is old, but I figured I'd add to it in case anyone else is having this issue. restricting types to 'address' unfortunately does not achieve the expected result, as routes are still included. Thus, what I decided to do is loop through the result and implement the following check:

result.predictions[i].types.includes('street_address')

Unfortunately, I was surprised to know that my own address was not being included, as it was returning the following types: { types: ['geocode', 'premise'] }

Thus, I decided to start a counter, and any result that includes 'geocode' or 'route' in its types must include at least one other term to be included (whether that be 'street_address' or 'premise' or whatever. Thus, routes are excluded, and anything with a complete address will be included. It's not foolproof, but it works fairly well.

Loop through the result predictions, and implement the following:

if (result.predictions[i].types.includes('street_address')) {
    // Results that include 'street_address' should be included
    suggestions.push(result.predictions[i])
} else {
    // Results that don't include 'street_address' will go through the check
    var typeCounter = 0;
    if (result.predictions[i].types.includes('geocode')) {
        typeCounter++;
    }
    if (result.predictions[i].types.includes('route')) {
        typeCounter++;
    }
    if (result.predictions[i].types.length > typeCounter) {
        suggestions.push(result.predictions[i])
    }
}
Wedged answered 8/12, 2020 at 19:58 Comment(3)
Is there a hook that we can place these conditions in?Lawful
Yeah this is the only way I was able to handle it as well, makes no sense, especially when you specifically tell Google addressRicardo
Does this logic filter this address Vilanova i la Geltrú, Spain which does not have an address, number, and neither postal code?Vano
S
1

I think what you want is { types: ['address'] }.

You can see this in action with this live sample: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete (use the "Addresses" radio button).

Sunless answered 6/5, 2015 at 23:41 Comment(6)
Using { types: ['address'] } doesn't restrict results to be only those of type street_address. It also includes results of type route, which are roads without numbers.Twum
is there a way to exclude route from the results?Distant
@Twum this behavior is not intuitive. Is this on purpose? I have the same problem but the weirdest thing is that api works ok when server host is located in Europe and it does not work properly when the same code is placed in the machine located in the USA google autocomplete issueForbear
Google autocomplete does not guarantee always having a valid address containing street_number and postal_code. They suggest asking the user to type manually in these cases. issuetracker.google.com/issues/225210134Vano
Even geocode api does not filter full address, probably only when the user search by postalCode or using lat/long. This returns a Spain address unexpectedly https://maps.googleapis.com/maps/api/geocode/json?address=spain&result_type=street_address&location_type=ROOFTOP&key=YOUR_API_KEYVano
it does restrict it somewhatConnect
P
1

It seems we can resolve this reasonably well (at least for US addresses) by restricting types to 'street_address' and 'premise', and by not performing the operation unless the first character of the input is a numeral (0-9).

Paste answered 3/8, 2023 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.