You are here

function getlocations_get_exact_distance_sql in Get Locations 7.2

Same name and namespace in other branches
  1. 7 getlocations.module \getlocations_get_exact_distance_sql()

File

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

Code

function getlocations_get_exact_distance_sql($latitude, $longitude, $tbl_alias = '') {
  $radius = getlocations_earth_radius($latitude);
  $tbl_alias = empty($tbl_alias) ? $tbl_alias : $tbl_alias . '.';
  $latfield = $tbl_alias . 'latitude';
  $lonfield = $tbl_alias . 'longitude';

  // all calcs in mysql

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

  // some calcs predone in php
  $lat = deg2rad($latitude);
  $long = deg2rad($longitude);

  #$coslong = cos($long);
  $coslat = cos($lat);

  #$sinlong = sin($long);
  $sinlat = sin($lat);
  $sql = "(IFNULL(ACOS(({$sinlat} * SIN(RADIANS({$latfield}))) + ({$coslat} * COS(RADIANS({$latfield})) * COS(RADIANS({$longitude} - {$lonfield})))), 0.00000) * {$radius})";
  return $sql;
}