You are here

function getlocations_earth_distance_sql in Get Locations 7

Same name and namespace in other branches
  1. 7.2 getlocations.module \getlocations_earth_distance_sql()

Returns the SQL fragment needed to add a column called 'distance' to a query. For use in Views distance/proximity calculations

Parameters

$latitude The measurement point:

$longitude The measurement point:

$tbl_alias If necessary, the alias name. Used by SQL to clearly identify a field.:

6 calls to getlocations_earth_distance_sql()
getlocations_fields_handler_argument_distance::query in modules/getlocations_fields/handlers/getlocations_fields_handler_argument_distance.inc
Set up the query for this argument.
getlocations_fields_handler_field_distance::click_sort in modules/getlocations_fields/handlers/getlocations_fields_handler_field_distance.inc
Called to determine what to tell the clicksorter.
getlocations_fields_handler_field_distance::query in modules/getlocations_fields/handlers/getlocations_fields_handler_field_distance.inc
Called to add the field to a query.
getlocations_fields_handler_filter_distance::query in modules/getlocations_fields/handlers/getlocations_fields_handler_filter_distance.inc
Add this filter to the query.
getlocations_fields_handler_sort_distance::query in modules/getlocations_fields/handlers/getlocations_fields_handler_sort_distance.inc
Called to add the sort to a query.

... See full list

File

./getlocations.module, line 6129
getlocations.module @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

function getlocations_earth_distance_sql($latitude, $longitude, $tbl_alias = '') {

  // Make a SQL expression that estimates the distance to the given location.
  $radius = getlocations_earth_radius($latitude);

  // If the table alias is specified, add on the separator.
  $tbl_alias = empty($tbl_alias) ? '' : $tbl_alias . '.';
  $latfield = $tbl_alias . 'latitude';
  $lonfield = $tbl_alias . 'longitude';

  // all calcs in mysql

  #$sql = "(IFNULL(ACOS(COS(RADIANS($latitude)) * COS(RADIANS($latfield)) * (COS(RADIANS($longitude)) * COS(RADIANS($lonfield)) + SIN(RADIANS($longitude)) * SIN(RADIANS($lonfield))) + SIN(RADIANS($latitude)) * SIN(RADIANS($latfield))), 0.00000) * $radius)";

  // some calcs predone in php
  $long = deg2rad($longitude);
  $lat = deg2rad($latitude);
  $coslong = cos($long);
  $coslat = cos($lat);
  $sinlong = sin($long);
  $sinlat = sin($lat);
  $sql = "(IFNULL(ACOS({$coslat} * COS(RADIANS({$latfield})) * ({$coslong}*COS(RADIANS({$lonfield})) + {$sinlong} * SIN(RADIANS({$lonfield}))) + {$sinlat} * SIN(RADIANS({$latfield}))), 0.00000) * {$radius})";
  return $sql;
}