源函数有两个参数,请求和回调,一旦响应返回,您需要调用回调
$( document ).ready(function() {
$( "#finder_city" ).autocomplete({
source: function(request, callback){
var searchParam = request.term;
init(searchParam, callback)
},
minLength: 2
});
});
function init(query, callback) {
ymaps.geocode(query, { results: 5 }).then(function (res) {
var response = [];
if (res.geoObjects.get(0) == null) {
}
else if (res.geoObjects.get(1) == null){
response = [
res.geoObjects.get(0).properties.get('text')
];
}
else if (res.geoObjects.get(2) == null){
response = [
res.geoObjects.get(0).properties.get('text'),
res.geoObjects.get(1).properties.get('text')
];
}
else if (res.geoObjects.get(3) == null){
response = [
res.geoObjects.get(0).properties.get('text'),
res.geoObjects.get(1).properties.get('text'),
res.geoObjects.get(2).properties.get('text')
];
}
else if (res.geoObjects.get(4) == null){
response = [
res.geoObjects.get(0).properties.get('text'),
res.geoObjects.get(1).properties.get('text'),
res.geoObjects.get(2).properties.get('text'),
res.geoObjects.get(3).properties.get('text')
];
}
else {
response = [
res.geoObjects.get(0).properties.get('text'),
res.geoObjects.get(1).properties.get('text'),
res.geoObjects.get(2).properties.get('text'),
res.geoObjects.get(3).properties.get('text'),
res.geoObjects.get(4).properties.get('text')
];
}
callback(response);
});
}
演示:小提琴