DRodrigues
DRodrigues

Reputation: 9

Google Places Autocomplete - Types: Geocode, Address, Establishment - Not Working Properly

I am trying to set the types in google places autocomplete and it's getting me crazy!

I want my users to be able to filter there search by typing the address or a establishment they are looking for, but for some reason it's not working properly.

Code I am using:

<script defer src="https://maps.googleapis.com/maps/api/js?libraries=places&key=[my_api_key]"></script>
<script>
    var searchInputStart = 'start_address';
    start_address = new google.maps.places.Autocomplete((document.getElementById(searchInputStart)), {
        types: ['geocode'],
        types: ['address'],
        types: ['establishment']
    });
    google.maps.event.addListener(start_address, 'place_changed', function () {
        var place_start = start_address.getPlace();
        document.getElementById('loc_lat_start').value = place_start.geometry.location.lat();
        document.getElementById('loc_long_start').value = place_start.geometry.location.lng();
        //get_directions to address
        set_route();
    });
</script>

If I take out --> types: [address] --> it shows the establishments correct, but not the addresses.

If I take out --> types: [establishement] --> it shows the addresses correct, but not the establishements.

How can my users filter places correctly from typing an address or establishment?

Can anyone help me please? What am I doing wrong?

Thank you.

Upvotes: 0

Views: 3050

Answers (1)

xomena
xomena

Reputation: 32178

According to Google Maps API documentation you can set only one type in place autocomplete options.

types Array<string>: The types of predictions to be returned. For a list of supported types, see the developer's guide. If nothing is specified, all types are returned. In general only a single type is allowed. The exception is that you can safely mix the 'geocode' and 'establishment' types, but note that this will have the same effect as specifying no types.

source: https://developers.google.com/maps/documentation/javascript/reference/places-widget#AutocompleteOptions

That means that you can use

start_address = new google.maps.places.Autocomplete(
    document.getElementById(searchInputStart), 
    {
        types: ['address']
    }
);

or

start_address = new google.maps.places.Autocomplete(
    document.getElementById(searchInputStart), 
    {
        types: ['geocode', 'establishment']
    }
);

but not all of them in the same options object.

I hope my answer clarifies your doubt.

Upvotes: 2

Related Questions