Вывод точек поблизости

This commit is contained in:
Krivchikov Dmitry 2014-10-11 15:50:12 +04:00
parent c377fccb65
commit 0e178f19b2
3 changed files with 43 additions and 3 deletions

View File

@ -70,11 +70,13 @@ class PageController extends Controller
$randomPoint = Point::model()->getRandom();
$similarPoints = Point::model()->getAll(array('where' => 'categoryId = '.$point->categoryId.' AND `id` <> '.$point->id.' ORDER BY RAND() LIMIT 6;'));
$closestPoints = Point::model()->getClosestPoints($id);
self::render('point.php', array(
'point' => $point,
'randomPoint' => $randomPoint,
'similarPoints' => $similarPoints,
'closestPoints' => $closestPoints,
'categories' => $categories,
'photos' => $point->photos,
));

View File

@ -44,6 +44,40 @@ class Point extends Model
return $this->getByPK($pointId);
}
public function getClosestPoints($pointId)
{
$point = Point::model()->getByPK($pointId);
$lat = floatval($point->lat);
$lng = floatval($point->lng);
$query = '
SELECT *, (
SQRT(POW(`lat`-:lat,2)+POW(`lng`-:lng,2))
) AS `dist` FROM `' . $this->_tableName_ . '`
WHERE
`lat` BETWEEN :minLat AND :maxLat
AND
`lng` BETWEEN :minLng AND :maxLng
AND
`lat` <> :lat
AND
`lng` <> :lng
ORDER BY `dist`
LIMIT 6;';
$res = App::DB()->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$res->execute(array(':lat' => $lat, ':lng' => $lng, ':minLat' => $lat-1, ':minLng' => $lng-1, ':maxLat' => $lat+1, ':maxLng' => $lng+1));
$ready = array();
$calledClass = get_called_class();
while ($res && $row = $res->fetch(PDO::FETCH_ASSOC))
{
$ready[] = new $calledClass($row);
}
return $ready;
}
public function getCountByUser($userId)
{
$res = App::DB()->query('SELECT count(`id`) AS `cnt` FROM ' . $this->_tableName_ . ' WHERE `author` = ' . $userId . ' ;')->fetch(PDO::FETCH_ASSOC);

View File

@ -98,9 +98,13 @@
</div>
<div class="col-md-2" style="width: 250px!important;">
<h2>Поблизости:</h2>
<p><img src="/photos/small/_03bad.jpg" style="width: 105px;"/> <img src="/photos/small/1_0a464.jpg" style="width: 105px;"/></p>
<p><img src="/photos/small/15_165ea.jpg" style="width: 105px;"/> <img src="/photos/small/271_6506d.jpg" style="width: 105px;"/></p>
<p><img src="/photos/small/305_337c1.jpg" style="width: 105px;"/> <img src="/photos/small/300_81352.jpg" style="width: 105px;"/></p>
<?php if(empty($closestPoints)):?>
Известных нам точек поблизости нет.
<?php else: ?>
<?php foreach ($closestPoints as $cPoint): ?>
<a href="/page/point/id/<?= $cPoint->id ?>"><img src="<?= $cPoint->getImg()?>" title="<?= $cPoint->name ?>" style="width: 105px;"/></a>
<?php endforeach;?>
<?php endif; ?>
<hr/>
<h2>Похожие:</h2>
<?php foreach ($similarPoints as $sPoint): ?>