function earth_distance_sql in Location 7.3
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.4 earth.inc \earth_distance_sql()
Returns the SQL fragment needed to add a column called 'distance' to a query that includes the location table.
Parameters
float $longitude: The measurement point.
float $latitude: The measurement point.
string $tbl_alias: If necessary, the alias name of the location table to work from. Only required when working with named {location} tables.
Return value
string SQL fragment.
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_search_search_execute in contrib/
location_search/ location_search.module - Implements hook_search_execute().
File
- ./
earth.inc, line 142 - 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 "(COALESCE(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})";
}