5

https://google-developers.appspot.com/maps/documentation/javascript/examples/places-autocomplete

我有一个类似于上面的谷歌地方自动完成演示网址的页面,如果我输入白金汉宫。它将返回结果

  1. 白金汉宫路,伦敦,英国
  2. 白金汉宫商店,白金汉宫路,维多利亚,伦敦,英国

等等。如何从结果中删除英国伦敦?

4

3 回答 3

2

谷歌似乎没有兴趣很快将其整理出来。我不得不诉诸以下,可能也足以满足其他人的需求:

document.addEventListener('DOMNodeInserted', function(event) {
    var target = $(event.target);
    if (target.hasClass('pac-item')) {
        target.html(target.html().replace(/, Australia<\/span>$/, "</span>"));
    }
});

请注意,这pac-item是每个建议中使用的类。有关使用的其他类,请参阅Google 参考。带有类的容器pac-container在未显示时似乎会删除项目,并在显示时添加新项目,因此如果将这些pac-items项目添加到 DOM,则意味着建议正在显示的路上并且pac-container即将变得可见。

刚刚解决了这个问题,所以对改进持开放态度。

这也不是一个完整的解决方案。选择删除国家/地区的建议时,自动完成功能仍会将国家/地区添加到地理编码中!place_changed改变它的阶段太晚了,所以请把上面的解决方案作为答案的一部分。一旦我弄清楚其余部分,我会再次更新。

- - 更新

就个人而言,我最终根本没有使用谷歌自动完成功能,因为一旦选择了修改后的建议,我无法解决自动完成仍然显示国家/地区的问题。一种更有用的方法是使用 twitter typeahead 并使用 google API 仅获取建议。这让我可以更好地控制建议,但显然需要更多的手动工作来弥补失去的功能

于 2014-08-20T00:55:06.860 回答
1

这是我如何使它与 jQuery 自动完成一起工作。希望它可以帮助某人。

   $("#Search").autocomplete({
       source: function (request, response) {
           var options = {
               input: request.term,
               types: ['(cities)'],
               region: 'US',
               componentRestrictions: { country: "us" }
           };
           function callback(predictions, status) {
               for (var i = 0, prediction; prediction = predictions[i]; i++) {
                   results.push(prediction.description.replace(/, United States$/, ""));
               }
               response(results);
           }
           var service = new google.maps.places.AutocompleteService();
           service.getPlacePredictions(options, callback);
           var results = [];
       }
   });
于 2015-06-19T16:20:37.723 回答
0

This is not possible without manually processing the results. If you think that it would be a useful feature, please file a Places API - Feature Request.

于 2012-11-09T04:51:44.640 回答