This repository was archived by the owner on Nov 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathqueryFilterByDistanceFrom.php
More file actions
38 lines (35 loc) · 1.5 KB
/
queryFilterByDistanceFrom.php
File metadata and controls
38 lines (35 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Filters objects by distance from a given origin.
*
* @param double $latitude The latitude of the origin point.
* @param double $longitude The longitude of the origin point.
* @param double $distance The distance between the origin and the objects to find.
* @param double $unit The unit measure.
* @param Criteria $comparison Comparison sign (default is: `<`).
*
* @return <?php echo $queryClassName ?> The current query, for fluid interface
*/
public function filterByDistanceFrom($latitude, $longitude, $distance, $unit = <?php echo $defaultUnit ?>, $comparison = Criteria::LESS_THAN)
{
if (<?php echo $peerClassName ?>::MILES_UNIT === $unit) {
$earthRadius = 3959;
} elseif (<?php echo $peerClassName ?>::NAUTICAL_MILES_UNIT === $unit) {
$earthRadius = 3440;
} else {
$earthRadius = 6371;
}
$sql = 'ABS(%s * ACOS( ROUND (%s * COS(RADIANS(%s)) * COS(RADIANS(%s) - %s) + %s * SIN(RADIANS(%s)),14)))';
$preparedSql = sprintf($sql,
$earthRadius,
cos(deg2rad($latitude)),
$this->getAliasedColName(<?php echo $latitudeColumnConstant ?>),
$this->getAliasedColName(<?php echo $longitudeColumnConstant ?>),
deg2rad($longitude),
sin(deg2rad($latitude)),
$this->getAliasedColName(<?php echo $latitudeColumnConstant ?>)
);
return $this
->withColumn($preparedSql, 'Distance')
->where(sprintf('%s %s ?', $preparedSql, $comparison), $distance, PDO::PARAM_STR)
;
}