Google Places API: How to use multiple types?

2019-04-26 14:20发布

问题:

I need a POI API that returns ratings, photos, opening/closing times, etc and I thought Google Places API seemed to do what I want, but I am having some trouble with filtering: I want to use the autocomplete feature with multiple types for filtering.

Here is what I have:

var map;
var selectAttractionAutocomplete;
var selectCityAutocompleteOptions = {
    types: ['(cities)']
};

map = new google.maps.Map(document.getElementById('map-canvas'), {
    center: new google.maps.LatLng(-33.8665433, 151.1956316),
    zoom: 15
});

var inputsearchedCity = document.getElementById('input-searched-city');
selectCityAutocomplete = new google.maps.places.Autocomplete(inputsearchedCity, selectCityAutocompleteOptions);
selectCityAutocomplete.bindTo('bounds', map);

google.maps.event.addListener(selectCityAutocomplete, 'place_changed', function () {
    console.log(selectCityAutocomplete.getPlace());
});

How can I use multiple types?

I have tried pipes, commas, brackets... nothing works:

var selectCityAutocompleteOptions = {
    types: ['cities|point_of_interest']
};

回答1:

According to Google Documentation, point_of_interestis of type 2, which are not supported in the types filter of a place search, or in the types property when adding a place.



回答2:

Encountered this recently. Answer is here Google Places Auto-Complete

types, which can either specify one of two explicit types or one of two type collections.



回答3:

If your are using in a query string, use the | separator. Remember that only 'geocode|establishment' is currently valid as a collection type, which is the same than not specifying any combined type.

See: https://developers.google.com/places/web-service/autocomplete#place_types

You may restrict results from a Place Autocomplete request to be of a certain type by passing a types parameter. The parameter specifies a type or a type collection, as listed in the supported types below. 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.



回答4:

var request = {
    bounds: map.getBounds(),
    types: ['bar','park']
      //keyword: 'best view'
  };