【发布时间】:2015-12-19 17:26:37
【问题描述】:
【问题讨论】:
-
这可能就是你要找的东西:stackoverflow.com/questions/10626727/…
标签: javascript google-maps google-maps-api-3
【问题讨论】:
标签: javascript google-maps google-maps-api-3
您可以配置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>
【讨论】: