return deg2rad($angle) * self::earthRadius($latitude);
}
- /**
- * Estimate the earth-surface distance between two locations.
- */
- static function earthDistance($longitudeSrc, $latitudeSrc,
- $longitudeDst, $latitudeDst
- ) {
-
- $longSrc = deg2rad($longitudeSrc);
- $latSrc = deg2rad($latitudeSrc);
- $longDst = deg2rad($longitudeDst);
- $latDst = deg2rad($latitudeDst);
-
- $radius = self::earthRadius(($latitudeSrc + $latitudeDst) / 2);
-
- $cosAngle = cos($latSrc) * cos($latDst) * (cos($longSrc) * cos($longDst) + sin($longSrc) * sin($longDst)) + sin($latSrc) * sin($latDst);
- return acos($cosAngle) * $radius;
- }
-
/**
* Estimate the min and max longitudes within $distance of a given location.
*/
);
}
- /*
- * Returns the SQL fragment needed to add a column called 'distance'
- * to a query that includes the location table
- *
- * @param $longitude
- * @param $latitude
- */
-
- static function earthDistanceSQL($longitude, $latitude) {
- $long = deg2rad($longitude);
- $lat = deg2rad($latitude);
- $radius = self::earthRadius($latitude);
-
- $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
-";
- }
-
static function where($latitude, $longitude, $distance, $tablePrefix = 'civicrm_address') {
self::initialize();
$params = array();
$clause = array();
- list($minLongitude, $maxLongitude) = self::earthLongitudeRange($longitude,
- $latitude,
- $distance
- );
- list($minLatitude, $maxLatitude) = self::earthLatitudeRange($longitude,
- $latitude,
- $distance
- );
-
- $earthDistanceSQL = self::earthDistanceSQL($longitude, $latitude);
+ list($minLongitude, $maxLongitude) =
+ self::earthLongitudeRange($longitude,
+ $latitude,
+ $distance
+ );
+ list($minLatitude, $maxLatitude) =
+ self::earthLatitudeRange(
+ $longitude,
+ $latitude,
+ $distance
+ );
$where = "
{$tablePrefix}.geo_code_1 >= $minLatitude AND
{$tablePrefix}.geo_code_1 <= $maxLatitude AND
{$tablePrefix}.geo_code_2 >= $minLongitude AND
{$tablePrefix}.geo_code_2 <= $maxLongitude AND
-$earthDistanceSQL <= $distance
+ACOS(
+ COS(RADIANS({$tablePrefix}.geo_code_1)) *
+ COS(RADIANS($latitude)) *
+ COS(RADIANS({$tablePrefix}.geo_code_2) - RADIANS($longitude)) +
+ SIN(RADIANS({$tablePrefix}.geo_code_1)) *
+ SIN(RADIANS($latitude))
+ ) * 6378137 <= $distance
";
return $where;