summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnkur Rishi2007-03-16 23:51:30 (GMT)
committer Ankur Rishi2007-03-16 23:51:30 (GMT)
commit55d80ef3816d5a729b632851bf8df37e0d37f45b (patch)
treeee2a7bb9d226310eb3ffe126f02aba5a21348b22
parent3439c11cf5de9878ca62f3a58a5d9823fbf3af9a (diff)
Applying patch to make earth_distance_sql() fn in earth.inc more re-usable.
Patch courtesy of drupal.org user scott.mclewin in http://drupal.org/node/98831
-rw-r--r--earth.inc15
1 files changed, 13 insertions, 2 deletions
diff --git a/earth.inc b/earth.inc
index 004fdf3..a5c9b45 100644
--- a/earth.inc
+++ b/earth.inc
@@ -88,17 +88,28 @@ function earth_distance($longitude1, $latitude1, $longitude2, $latitude2) {
return acos($cosangle) * $radius;
}
-function earth_distance_sql($longitude, $latitude) {
+/*
+ * Returns the SQL fragment needed to add a column called 'distance'
+ * to a query that includes the location table
+ *
+ * @param $longitude The measurement point
+ * @param $latibude The measurement point
+ * @param $tbl_alias If necessary, the alias name of the location table to work from. Only required when working with named {location} tables
+ */
+function earth_distance_sql($longitude, $latitude, $tbl_alias = '') {
// Make a SQL expression that estimates the distance to the given location.
$long = deg2rad($longitude);
$lat = deg2rad($latitude);
$radius = earth_radius($latitude);
+ // If the table alias is specified, add on the separator.
+ $tbl_alias = empty($tbl_alias) ? $tbl_alias : ($tbl_alias . '.');
+
$coslong = cos($long);
$coslat = cos($lat);
$sinlong = sin($long);
$sinlat = sin($lat);
- return "(IFNULL(ACOS($coslat*COS(RADIANS(latitude))*($coslong*COS(RADIANS(longitude)) + $sinlong*SIN(RADIANS(longitude))) + $sinlat*SIN(RADIANS(latitude))), 0.00000)*$radius)";
+ return "(IFNULL(ACOS($coslat*COS(RADIANS({$tbl_alias}latitude))*($coslong*COS(RADIANS({$tbl_alias}longitude)) + $sinlong*SIN(RADIANS({$tbl_alias}longitude))) + $sinlat*SIN(RADIANS({$tbl_alias}latitude))), 0.00000)*$radius)";
}
function earth_longitude_range($longitude, $latitude, $distance) {