Набросал на скорую руку:
<?php
class Location {
public $lat, $lon, $name;
public function __construct($lat, $lon, $name='') {
$this->lat = $lat;
$this->lon = $lon;
$this->name = $name;
}
public function getDistanceTo(Location &$obj) {
$lat1 = $this->lat * M_PI / 180;
$lon1 = $this->lon * M_PI / 180;
$lat2 = $obj->lat * M_PI / 180;
$lon2 = $obj->lon * M_PI / 180;
$d_lon = $lon1 - $lon2;
$slat1 = sin($lat1);
$slat2 = sin($lat2);
$clat1 = cos($lat1);
$clat2 = cos($lat2);
$sdelt = sin($d_lon);
$cdelt = cos($d_lon);
$y = pow($clat2 * $sdelt, 2) + pow($clat1 * $slat2 - $slat1 * $clat2 * $cdelt, 2);
$x = $slat1 * $slat2 + $clat1 * $clat2 * $cdelt;
return atan2(sqrt($y), $x) * 6372795 / 1000; // в км.
}
};
$objects = [
new Location(55.7518, 37.6176, 'Московский Кремль'),
new Location(55.7702, 37.5952, 'м. Маяковская'),
new Location(55.7621, 37.8555, 'Реутов'),
new Location(55.6592, 37.6626, 'Коломенское'),
new Location(55.8549, 37.4761, 'м. Речной вокзал'),
new Location(55.9088, 37.7332, 'Мытищи'),
];
$current = new Location(55.7800, 37.6138);
$maxDistance = 12; // в километрах
foreach ($objects as $obj) {
$dist = $obj->getDistanceTo($current);
if ($dist < $maxDistance) {
echo sprintf("%s (%0.2f км)<br>", $obj->name, $dist);
}
}