componentRestrictions似乎不会对AutocompleteService.getQueryPredictions

时间:2015-08-07 23:16:10

标签: google-maps google-maps-api-3

(试图制作一个jsbin,但它不会加载Google库)

加载此版本的API库: https://maps.googleapis.com/maps/api/js?v=3.21&libraries=places&callback=onAPILoaded

运行这个JS:

function runQuery(country) {
  var request = {
    input: 'london',
    componentRestrictions: {country: country},
  };

  var callback = function(predictions, status) {
    if (status != google.maps.places.PlacesServiceStatus.OK) {
      alert(status);
      return;
    }
    var resultsString = predictions.reduce(function (prev, current) {
      return prev + "\n" + current.description;
    }, "RESULTS FOR " + country + "\n--------------\n");
    console.log(resultsString);
  };

  var service = new window.google.maps.places.AutocompleteService();
  service.getQueryPredictions(request, callback);
}

// on api loaded...
function onAPILoaded() {
  runQuery('uk');
  runQuery('ca');
}

获得此输出

RESULTS FOR uk
--------------

London, United Kingdom
London, ON, Canada
London Borough of Hillingdon, United Kingdom
London Eye, London, United Kingdom
London Bridge, London, United Kingdom

RESULTS FOR ca
--------------

London, United Kingdom
London, ON, Canada
London Borough of Hillingdon, United Kingdom
London Eye, London, United Kingdom
London Bridge, London, United Kingdom

问题在于,它看起来并不像componentRestrictions做任何事情 - 无论国家代码如何,结果都是一样的。有任何想法吗?提前谢谢。

1 个答案:

答案 0 :(得分:2)

QueryAutocompletionRequest

没有componentRestrictions - 属性

componentRestrictions - 属性适用于AutocompletionRequest(将与getPlacePredictions()一起使用)