function earth_distance_sql in Location 7.4
Same name and namespace in other branches
- 5.3 earth.inc \earth_distance_sql()
- 5 earth.inc \earth_distance_sql()
- 6.3 earth.inc \earth_distance_sql()
- 7.5 earth.inc \earth_distance_sql()
- 7.3 earth.inc \earth_distance_sql()
6 calls to earth_distance_sql()
- location_handler_argument_location_proximity::query in handlers/
location_handler_argument_location_proximity.inc - Set up the query for this argument.
- location_handler_field_location_distance::click_sort in handlers/
location_handler_field_location_distance.inc - Called to determine what to tell the clicksorter.
- location_handler_field_location_distance::query in handlers/
location_handler_field_location_distance.inc - Called to add the field to a query.
- location_handler_sort_location_distance::query in handlers/
location_handler_sort_location_distance.inc - Called to add the sort to a query.
- location_views_handler_filter_proximity::query in handlers/
location_views_handler_filter_proximity.inc - Add this filter to the query.
File
- ./
earth.inc, line 109 - Trigonometry for calculating geographical distances. All function arguments and return values measure distances in metres and angles in degrees. The ellipsoid model is from the WGS-84 datum. Ka-Ping Yee, 2003-08-11
Code
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({$tbl_alias}latitude))*({$coslong}*COS(RADIANS({$tbl_alias}longitude)) + {$sinlong}*SIN(RADIANS({$tbl_alias}longitude))) + {$sinlat}*SIN(RADIANS({$tbl_alias}latitude))), 0.00000)*{$radius})";
}