Google Places API: How to use multiple types?

十年热恋 提交于 2019-12-22 03:25: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:


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.




回答3:


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.




回答4:


This question is partly answered in this thread.

First, the place type of "cities" it not supported. You can find a list of supported place types here.

There is no way to use multiple types at once. However, you can call the API twice in order to get similar results. For example:

var selectCityAutocompleteOptions1 = {
  types: ['zoo']
};
var selectCityAutocompleteOptions2 = {
  types: ['museum']
};

Based off of your description, though, it sounds like you want all points of interest results, without filtering by type. In that case you might want to use a Find Place Requests Place Search instead.




回答5:


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


来源:https://stackoverflow.com/questions/31625112/google-places-api-how-to-use-multiple-types

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