Google Maps/Places Javascript API Autocompletion R

2019-07-20 13:08发布

问题:

I'm using the Google Maps/Places javascript API for autocompletion of locations in an input box. I'm trying to restrict the types of places that are being returned by the autocompletion. I'm very new to coffeescript (and coding in general), but since I have the code snippet here I wanted to see if anyone can see anything in this code that might be causing the call to Google to not consider the place "types" filter. Note that "restaurants" was just used as a test (an unsuccessful one).

I've read through several other questions on here, amongst them: How can I restrict the Google Maps Places Library's Autocomplete to suggestions of only one city's places? How to limit google autocomplete results to City and Country only

Unfortunately, it looks like the syntax I'm using is correct, which is why I'm asking this question because no matter which "type" I use the autocompletion results do not change.

Thanks for any help.

# autocomplete callback
autocomplete_options = {
  types: ['restaurants'] # geocode, establishment, (regions), (cities)
}
autocomplete = new google.maps.places.Autocomplete(document.getElementById 'locationSearchField', autocomplete_options)
google.maps.event.addListener autocomplete, 'place_changed', ->
  place = autocomplete.getPlace()

回答1:

This structure:

f(g x, y)

is interpreted like this:

f(g(x, y))

That means that autocomplete_options in here:

autocomplete = new google.maps.places.Autocomplete(document.getElementById 'locationSearchField', autocomplete_options)

is seen as an argument to document.getElementById and google.maps.places.Autocomplete never sees it. If you add parentheses:

autocomplete = new google.maps.places.Autocomplete(document.getElementById('locationSearchField'), autocomplete_options)
// -----------------------------------------------------------------------^---------------------^

then everyone should get the arguments that you're expecting them to.