You are here

public static function ProximityTrait::getProximityQueryFragment in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 src/ProximityTrait.php \Drupal\geolocation\ProximityTrait::getProximityQueryFragment()

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.

Return value

string The fragment to enter to actual query.

3 calls to ProximityTrait::getProximityQueryFragment()
ProximityArgument::getFormula in src/Plugin/views/argument/ProximityArgument.php
ProximityField::query in src/Plugin/views/field/ProximityField.php
Called to add the field to a query.
ProximityFilter::query in src/Plugin/views/filter/ProximityFilter.php
Add this filter to the query.

File

src/ProximityTrait.php, line 86

Class

ProximityTrait
Trait ProximityTrait.

Namespace

Drupal\geolocation

Code

public static function getProximityQueryFragment($table_name, $field_id, $filter_lat, $filter_lng) {

  // 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      )) * 6371\n    )";
}