//Сначала подключаем GeoIp для определения текущего местоположения пользователя
use \Bitrix\Main\Service\GeoIp;
// получаем ip пользователя
$userIpAddress = GeoIp\Manager::getRealIp();
$lt = GeoIp\Manager::getGeoPositionLatitude($userIpAddress, "ru"); // возвращает широту
$ln = GeoIp\Manager::getGeoPositionLongitude($userIpAddress, "ru"); // возвращает долготу
// класс рассчета ближайшего расстояния
class Point
{
public $x, $y;
function __construct($x, $y)
{
$this->x = $x;
$this->y = $y;
}
function distanceTo(Point $point)
{
$distanceX = $this->x - $point->x;
$distanceY = $this->y - $point->y;
$distance = sqrt($distanceX * $distanceX + $distanceY * $distanceY);
return $distance;
}
function __toString()
{
return 'x: ' . $this->x . ', y: ' . $this->y;
}
}
// в примере мы рассматриваем расстояние до случайных точек
$points = array(new Point(0, 0.02), new Point(0.5, 0.3), new Point(1, 1), new Point(-1, -2));
$curNearestPoint = $points[0];
$curNearestDistance = $a->distanceTo($curNearestPoint);
//проходим по всем точкам и находим наименьшее расстояние
foreach ($shops_info as $point) {
$distance = $a->distanceTo($point);
if ($distance < $curNearestDistance) {
$curNearestDistance = $distance;
$curNearestPoint = $point;
}
}
// $curNearestPoint — ближайшая точка к текущим координатам пользователя