【问题标题】:Return multiple types from google places autocompleteService.getPlacePredictions从谷歌地方返回多种类型 autocompleteService.getPlacePredictions
【发布时间】:2023-03-15 14:09:01
【问题描述】:

我正在尝试为 google 地方自动完成实现自定义 UI,因为预建的 UI 不允许我手动选择结果。在 getPlacePredictions 函数选项中不使用多种类型时,一切正常,但是当我使用 ['(regions)', '(cities)'] 时,状态返回 'invalid request'

是我做错了什么还是不能返回多种类型?

var _this = this;

this.input = $('#zipcode_autocomplete');

this.service = new google.maps.places.AutocompleteService();

this.input.on('input', function() {
  return _this.service.getPlacePredictions({
    input: _this.input.val(),
    types: ['(regions)', '(cities)'],
    componentRestrictions: {
      country: 'us'
    }
  }, _this.callback);
});

this.callback = function(predictions, status) {
  var i, prediction, _results;
  console.log(predictions);
  if (status !== google.maps.places.PlacesServiceStatus.OK) {
    alert(status);
    return;
  }
  i = 0;
  prediction = void 0;
  this.results = $('ul.location_results').removeClass('hidden').html('');
  _results = [];
  while (prediction = predictions[i]) {
    this.results.append("<li><span class='location-address'>" + prediction.terms[0].value + "</span><span class='location-state'>" + prediction.terms[prediction.terms.length - 2].value + "</span></li>");
    _results.push(i++);
  }
  return _results;
};

【问题讨论】:

  • 好的,这很令人沮丧,感谢您将我指向该页面,我无法在任何地方找到它。 @Andy 如果您想将其发布为答案,我会接受它
  • 完成。在那里也添加了更多信息。

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


【解决方案1】:

根据 API 'In general only a single type is allowed'

如果您要尝试单独获取这两种类型,您可以使用deferred object 来管理流程,如下所示:

// set a new promises array, set the types array
var promises = [], types = ['(regions)', '(cities)'];

// loop over the types and push the output of getPredications() for each one
// into the promises array
for (var i = 0, l = types.length; i < l; i++) {
  promises.push(getPredictions(types[i]));
}

// when all promises have completed then do something
// This uses jQuery's when method which can take an array of
// promises when used with apply
$.when.apply(null, promises).then(runThisFunction);

function getPredictions(type) {

  // Set up a new deferred object
  var deferred = new $.Deferred();

  // Call the asynchronous function to get the place predictions
  this.service.getPlacePredictions({
    input: _this.input.val(),
    types: type,
    componentRestrictions: {
      country: 'us'
    }
  }, function (data) {

    // Once the data has been returned perhaps do something with data
    // but definitely resolve the deferred object.       
    deferred.resolve();
  });

  // return the promise
  return deferred.promise();
}

【讨论】:

  • 我正在尝试做类似的事情(从我自己的来源和谷歌中提取数据),而你的解决方案只是谷歌上针对这种情况的一个解决方案。不幸的是,我很难理解这是如何工作的。你愿意解释一下吗?
  • @MichaelSzyndel,我添加了一些注释。查看 jQuery 的 deferred objects API 的文档以获取更多信息。
  • @MikeSzyndel 我正在尝试根据“postal_code”和“address”获取结果。我很难理解你的代码。请您帮我将您的逻辑集成到我的代码中 - jsfiddle.net/s30wo6zy
  • @AnUser 你真的不应该在 2020 年使用 jQuery。更好的方法是使用 Promise,特别是 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
  • 以下是如何将 google API 响应放入 Promise stackoverflow.com/questions/45796155/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-09
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 1970-01-01
  • 2021-04-13
相关资源
最近更新 更多