【问题标题】:google map place API how to remove country name from suggestion谷歌地图地方API如何从建议中删除国家名称
【发布时间】:2015-12-19 17:26:37
【问题描述】:

当我们输入位置或地址时,自动填充功能会在其建议中显示国家名称。有没有办法从建议中删除或省略国家名称?

【问题讨论】:

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


【解决方案1】:

您可以配置AutocompleteService返回的AutocompletePrediction object。 AutocompletePrediction的相关属性为:

  • description(类型:字符串。这是 Places 服务建议的未格式化查询版本。)
  • terms(类型:数组 有关上述描述中各个术语的信息,从最具体到最不具体。例如,“Taco Bell”、“Willitis”和“CA”。)

因此您可以修改响应更改两个属性之一,例如通过拼接描述prediction.description.split(", ").slice(0,-1)来删除国家/地区

对于下面的示例,我使用 JQuery UI 小部件作为 https://stackoverflow.com/a/13774273/2314737 之后的 AutocompleteService 的包装器

$(function() {
  $("#search").autocomplete({
    source: function(request, response) {
      var service = new google.maps.places.AutocompleteService();
      service.getPlacePredictions({
        input: request.term,
        type: 'geocode',
      }, function(predictions, status) {
        if (status != google.maps.places.PlacesServiceStatus.OK) {
          alert(status);
          return;
        }
        response($.map(predictions, function(prediction, i) {
          return {
            value: prediction.description,
            //label: prediction.terms[0].value
            label: prediction.description.split(", ").slice(0,-1),             
          }
        }));
      });
    },
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=placeshttps://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
<div class="ui-widget">
  <label for="search">Search:</label>
  <input id="search">
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多