Skip to content

Commit e8b60a6

Browse files
authored
Create Distance.php
1 parent 4749208 commit e8b60a6

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/Utils/Distance.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace ZhenMu\Support\Utils;
4+
5+
class Distance
6+
{
7+
public static function getDistanceSql($sqlLongitude, $sqlLatitude, $longitude, $latitude, $alias = 'distance')
8+
{
9+
$sql = <<<SQL
10+
2 * ASIN(
11+
SQRT(
12+
POW(
13+
SIN(
14+
(
15+
$latitude * PI() / 180 - $sqlLatitude * PI() / 180
16+
) / 2
17+
), 2
18+
) + COS($latitude * PI() / 180) * COS($sqlLatitude * PI() / 180) * POW(
19+
SIN(
20+
(
21+
$longitude * PI() / 180 - $sqlLongitude * PI() / 180
22+
) / 2
23+
), 2
24+
)
25+
)
26+
) * 6378.137
27+
SQL;
28+
return sprintf('(%s) as %s', $sql, $alias);
29+
}
30+
}

0 commit comments

Comments
 (0)