var search_result = [];
var addr = '';
function autocomp(id)
{
addr = $("#"+id);
var search_query = addr.val();
if(search_query.length > 2)
{
$(document).ready(function()
{
search_result = [];
$.getJSON('https://geocode-maps.yandex.ru/1.x/?format=json&callback=?&geocode='+search_query, function(data)
{
for(var i = 0; i < data.response.GeoObjectCollection.featureMember.length; i++)
{
search_result.push
({
label: data.response.GeoObjectCollection.featureMember[i].GeoObject.description+' - '+data.response.GeoObjectCollection.featureMember[i].GeoObject.name,
value: data.response.GeoObjectCollection.featureMember[i].GeoObject.name.replace('undefined', ''),
longlat:data.response.GeoObjectCollection.featureMember[i].GeoObject.Point.pos
});
}
//подключаем к текстовому полю виджет autocomplete
$("#"+id).autocomplete({
source: search_result,
select: function(event, ui){}
});
});
});
}
$.ui.autocomplete.filter = function (array, term)
{
return $.grep(array, function (value)
{
return value.label || value.value || value;
});
};
}
var search_result = [];
$(document).ready(function()
{
$('body').on('keyup', '[name^=adress]', function()
{
var address = $(this).val();
var search_query = $(this).val();
search_result = [];
$.getJSON('http://geocode-maps.yandex.ru/1.x/?format=json&callback=?&geocode='+search_query, function(data)
{
for(var i = 0; i < data.response.GeoObjectCollection.featureMember.length; i++)
{
search_result.push
({
label: data.response.GeoObjectCollection.featureMember[i].GeoObject.description+' - '+data.response.GeoObjectCollection.featureMember[i].GeoObject.name,
value: data.response.GeoObjectCollection.featureMember[i].GeoObject.name.replace('undefined', ''),
longlat:data.response.GeoObjectCollection.featureMember[i].GeoObject.Point.pos
});
}
//подключаем к текстовому полю виджет autocomplete
adress.autocomplete({
source: search_result,
select: function(event, ui){}
});
});
});
$.ui.autocomplete.filter = function (array, term) {
return $.grep(array, function (value) {
return value.label || value.value || value;
});
};
});