public function GeolocationCore::getProximityQueryFragment in Geolocation Field 8
Gets the query fragment for adding a proximity field to a query.
Parameters
string $table_name: The proximity table name.
string $field_id: The proximity field ID.
string $filter_lat: The latitude to filter for.
string $filter_lng: The longitude to filter for.
int $earth_radius: Filter radius.
Return value
string The fragment to enter to actual query.
File
- src/
GeolocationCore.php, line 264
Class
- GeolocationCore
- Class GeolocationCore.
Namespace
Drupal\geolocationCode
public function getProximityQueryFragment($table_name, $field_id, $filter_lat, $filter_lng, $earth_radius = self::EARTH_RADIUS_KM) {
// Define the field names.
$field_latsin = "{$table_name}.{$field_id}_lat_sin";
$field_latcos = "{$table_name}.{$field_id}_lat_cos";
$field_lng = "{$table_name}.{$field_id}_lng_rad";
// deg2rad() is sensitive to empty strings. Replace with integer zero.
$filter_lat = empty($filter_lat) ? 0 : $filter_lat;
$filter_lng = empty($filter_lng) ? 0 : $filter_lng;
// Pre-calculate filter values.
$filter_latcos = cos(deg2rad($filter_lat));
$filter_latsin = sin(deg2rad($filter_lat));
$filter_lng = deg2rad($filter_lng);
return "(\n ACOS(LEAST(1,\n {$filter_latcos}\n * {$field_latcos}\n * COS( {$filter_lng} - {$field_lng} )\n +\n {$filter_latsin}\n * {$field_latsin}\n )) * {$earth_radius}\n )";
}