Google Maps/Places Javascript API Autocompletion Restrict Types

给你一囗甜甜゛ 提交于 2019-12-11 03:08:30

问题


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.



来源:https://stackoverflow.com/questions/19186639/google-maps-places-javascript-api-autocompletion-restrict-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!