Skip to content

Commit 0f7c3bf

Browse files
committed
Merge pull request #1 from schmeiserd/master
fix filterByDistanceFrom(..)
2 parents 09e2039 + f122607 commit 0f7c3bf

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/templates/queryFilterByDistanceFrom.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,27 @@
1212
*/
1313
public function filterByDistanceFrom($latitude, $longitude, $distance, $unit = <?php echo $defaultUnit ?>, $comparison = Criteria::LESS_THAN)
1414
{
15+
if (<?php echo $peerClassName ?>::MILES_UNIT === $unit) {
16+
$earthRadius = 3959;
17+
} elseif (<?php echo $peerClassName ?>::NAUTICAL_MILES_UNIT === $unit) {
18+
$earthRadius = 3440;
19+
} else {
20+
$earthRadius = 6371;
21+
}
22+
23+
$sql = 'ABS(%s * ACOS(%s * COS(RADIANS(%s)) * COS(RADIANS(%s) - %s) + %s * SIN(RADIANS(%s))))';
24+
$preparedSql = sprintf($sql,
25+
$earthRadius,
26+
cos(deg2rad($latitude)),
27+
$this->getAliasedColName(<?php echo $latitudeColumnConstant ?>),
28+
$this->getAliasedColName(<?php echo $longitudeColumnConstant ?>),
29+
deg2rad($longitude),
30+
sin(deg2rad($latitude)),
31+
$this->getAliasedColName(<?php echo $latitudeColumnConstant ?>)
32+
);
33+
1534
return $this
16-
->withDistance($latitude, $longitude, $unit)
17-
->where(sprintf('Distance %s ?', $comparison), $distance, PDO::PARAM_STR)
35+
->withColumn($preparedSql, 'Distance')
36+
->where(sprintf('%s %s ?', $preparedSql, $comparison), $distance, PDO::PARAM_STR)
1837
;
1938
}

0 commit comments

Comments
 (0)