【问题标题】:componentRestrictions doesn't seem to have an effect with AutocompleteService.getQueryPredictionscomponentRestrictions 似乎对 AutocompleteService.getQueryPredictions 没有影响
【发布时间】:2015-10-31 12:00:56
【问题描述】:

(尝试创建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 正在做任何事情 - 无论国家/地区代码如何,结果都是相同的。有任何想法吗?提前致谢。

【问题讨论】:

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


    【解决方案1】:

    QueryAutocompletionRequest 没有 componentRestrictions-property

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

    【讨论】:

    • 是的,就是这样,只需将 service.getPlacePredictions 更改为服务即可。 getQueryPredictions 完成这项工作
    猜你喜欢
    • 2019-02-16
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 2012-10-22
    • 2021-11-01
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多