You are here

function geofield_haversine in Geofield 8

Same name and namespace in other branches
  1. 7.2 geofield.module \geofield_haversine()

Build SQL query snippet for the haversine formula.

Parameters

array $options: The haversine options.

  • origin_latitude - the origin latitude (in degrees).
  • origin_longitude - the origin longitude (in degrees).
  • earth_radius - the earth radius (see the constants at the top).
  • destination_latitude - the db field with the latitude.
  • destination_longitude - the db field with the longitude.

Return value

string The generated SQL query snippet for haversine formula.

4 calls to geofield_haversine()
GeofieldProximityArgument::operator in src/Plugin/views/argument/GeofieldProximityArgument.php
GeofieldProximityFilter::opBetween in src/Plugin/views/filter/GeofieldProximityFilter.php
Filters by operator between.
GeofieldProximityFilter::opSimple in src/Plugin/views/filter/GeofieldProximityFilter.php
GeofieldProximityHandlerTrait::addQueryOrderBy in src/Plugin/views/GeofieldProximityHandlerTrait.php
Add an Order By declaration to the View Query.

File

./geofield.module, line 140
Contains the geofield.module.

Code

function geofield_haversine(array $options = []) {
  $formula = '( :earth_radius * ACOS( LEAST(1, COS( RADIANS(:origin_latitude)) * COS( RADIANS(:destination_latitude) ) * COS( RADIANS(:destination_longitude) - RADIANS(:origin_longitude) ) + SIN( RADIANS(:origin_latitude) ) * SIN( RADIANS(:destination_latitude) ) ) ) )';
  foreach ($options as $key => $field) {
    if (is_numeric($field)) {
      $formula = str_replace(':' . $key, $field, $formula);
    }
    else {
      $formula = str_replace(':' . $key, Database::getConnection()
        ->escapeField($field), $formula);
    }
  }
  return $formula;
}