ADD поиск с саджестом
This commit is contained in:
parent
afe5bcbe1e
commit
2793f74286
|
|
@ -26,6 +26,7 @@ class IndexController extends Controller
|
|||
self::addScript('/js/route.js?ver=' . App::getConfig('version'));
|
||||
self::addScript('/js/leaflet.markercluster.js');
|
||||
self::addScript('/js/fotorama.js');
|
||||
self::addScript('/js/suggest.js');
|
||||
|
||||
self::addStyle('/css/style.css?ver=' . App::getConfig('version'));
|
||||
self::addStyle('http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css');
|
||||
|
|
|
|||
|
|
@ -325,8 +325,61 @@ class JsonController extends Controller
|
|||
{
|
||||
App::error404();
|
||||
}
|
||||
|
||||
self::renderPartial('json.php', Helper::boolval(Favorite::addRemove($id)));
|
||||
}
|
||||
|
||||
static function actionSearch()
|
||||
{
|
||||
$search = isset($_GET['query']) ? $_GET['query'] : '';
|
||||
|
||||
$results = array();
|
||||
if ($search)
|
||||
{
|
||||
// ====== POINTS ============
|
||||
$points = Point::model()->searchByString($search);
|
||||
if (!empty($points))
|
||||
{
|
||||
foreach ($points as $point)
|
||||
{
|
||||
$results[] = array('value' => $point->name, 'data' => '/?point='.$point->id);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($results) < 10)
|
||||
{
|
||||
// ====== YANDEX ============
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, 'http://geocode-maps.yandex.ru/1.x/?geocode=' . urlencode($search));
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
$out = null;
|
||||
$out = curl_exec($ch); // выполняем запрос curl - обращаемся к сервера php.su
|
||||
|
||||
if (!empty($out))
|
||||
{
|
||||
$obj = simplexml_load_string($out);
|
||||
|
||||
if (isset($obj->GeoObjectCollection->metaDataProperty->GeocoderResponseMetaData->found))
|
||||
{
|
||||
$count = (int) $obj->GeoObjectCollection->metaDataProperty->GeocoderResponseMetaData->found;
|
||||
if ($count > 0)
|
||||
{
|
||||
for ($index = 0; $index < $count && $index < 5 && count($results) < 25; $index++)
|
||||
{
|
||||
$name = (string) $obj->GeoObjectCollection->featureMember[$index]->GeoObject->name;
|
||||
$coords = explode(' ', (string) $obj->GeoObjectCollection->featureMember[$index]->GeoObject->Point->pos);
|
||||
$lat = $coords[1];
|
||||
$lng = $coords[0];
|
||||
$results[] = array('value' => $name, 'data' => "/?lat=$lat&lng=$lng&zoom=13");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
curl_close($ch);
|
||||
}
|
||||
}
|
||||
|
||||
self::renderPartial('json.php', array('query' => $search, 'suggestions' => $results));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,5 +83,10 @@ class Point extends Model
|
|||
$res = App::DB()->query('SELECT count(`id`) AS `cnt` FROM ' . $this->_tableName_ . ' WHERE `author` = ' . $userId . ' ;')->fetch(PDO::FETCH_ASSOC);
|
||||
return $res['cnt'];
|
||||
}
|
||||
|
||||
public function searchByString($search)
|
||||
{
|
||||
return $this->getAll(array('where' => "`name` LIKE '%$search%' OR `description` LIKE '%$search%'", 'limit' => 20));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@
|
|||
<div id="boxFilter" style="height: 100%; overflow-y: auto;">
|
||||
<h3>Фильтры и поиск</h3>
|
||||
<span class="input-group">
|
||||
<input type="text" id="addressField" class="form-control" placeholder="Поиск на карте..." onkeydown="if(event.keyCode == 13) {searchAdderess()}" />
|
||||
<input type="text" id="addressField" class="form-control" placeholder="Поиск на карте..." />
|
||||
<span class="input-group-btn">
|
||||
<button class="btn btn-default" type="button" onclick="searchAdderess()"><span class="glyphicon glyphicon-search"></span></button>
|
||||
</span>
|
||||
|
|
|
|||
|
|
@ -73,3 +73,6 @@ div#box {background-image: url('/img/back.png'); background-repeat: no-repeat; b
|
|||
|
||||
/*Leaflet FIX*/
|
||||
.leaflet-popup-content-wrapper .leaflet-popup-scrolled {border-bottom: none;border-top: none;}
|
||||
|
||||
#suggestContainer {position: absolute;top: 40px;background-color: #fff;padding-bottom: 8px; border-bottom: 1px solid #333; width: 219px; display: none;max-height: 345px;overflow-y: scroll;}
|
||||
#suggestContainer > div:hover {background-color: #eee;}
|
||||
|
|
@ -33,7 +33,8 @@ ymaps.ready(function () {
|
|||
|
||||
points.im = L.marker([lat, lng], {icon: categories.im, title: 'Вы здесь'});
|
||||
points.im.addTo(myMap);
|
||||
points.im.bindPopup('<b>Ваше местоположение</b><br/><img src="/ico/man.png" /> <button type="button" class="btn btn-default btn-xs" onclick="addToRoute(\'im\')"><span class="glyphicon glyphicon-plus"></span> Добавить в маршрут</button>')
|
||||
points.im.bindPopup('<b>Ваше местоположение</b><br/><img src="/ico/man.png" /> <button type="button" class="btn btn-default btn-xs" onclick="addToRoute(\'im\')"><span class="glyphicon glyphicon-plus"></span> Добавить в маршрут</button>');
|
||||
suggestInit('#addressField');
|
||||
});
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,52 @@
|
|||
var suggestSelector = '';
|
||||
|
||||
function suggestInit(selector)
|
||||
{
|
||||
suggestSelector = selector;
|
||||
$(selector).after('<div id="suggestContainer"></div>');
|
||||
$(selector).bind('input', function () {
|
||||
var query = $(selector).val();
|
||||
$('#suggestContainer').html('');
|
||||
$.ajax({
|
||||
url: "/json/search?query=" + query,
|
||||
context: document.body
|
||||
}).done(function (data) {
|
||||
res = data.suggestions;
|
||||
newHtml = '';
|
||||
$('#suggestContainer').html('').show();
|
||||
for (var i = 0; i < res.length; i++) {
|
||||
newHtml += '<div><a href="' + res[i].data + '">' + res[i].value + '</a></div>';
|
||||
}
|
||||
$('#suggestContainer').html(newHtml);
|
||||
}).fail(function (jqXHR, textStatus) {
|
||||
alert("Request failed: " + textStatus);
|
||||
});
|
||||
}).bind('keydown', function (event) {
|
||||
suggestKeyPress(event.which);
|
||||
}).bind('focus', function(){if($(selector).val())$('#suggestContainer').show();});
|
||||
$(selector).parent().bind('blur', function(){$('#suggestContainer').hide();});
|
||||
$('#suggestContainer').hide();
|
||||
}
|
||||
|
||||
function suggestKeyPress(key)
|
||||
{
|
||||
console.log(key);
|
||||
switch (key) {
|
||||
case 27:
|
||||
//ESCAPE
|
||||
$('#suggestContainer').hide();
|
||||
break
|
||||
case 13:
|
||||
//ENTER
|
||||
break
|
||||
case 38:
|
||||
//UP
|
||||
break
|
||||
case 40:
|
||||
//DOWN
|
||||
break
|
||||
default :
|
||||
$('#suggestContainer').show();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue