public function GeofieldProximitySourceBase::getProximity in Geofield 8
Get the calculated proximity.
Parameters
float $lat: The current point latitude.
float $lon: The current point longitude.
Return value
float The calculated proximity.
Throws
\Drupal\geofield\Exception\InvalidPointException; If the proximity cannot be created, due to incorrect point coordinates definition.
\Drupal\geofield\Exception\ProximityUnavailableException; If any other case the proximity value cannot be created correctly.
Overrides GeofieldProximitySourceInterface::getProximity
File
- src/
Plugin/ GeofieldProximitySourceBase.php, line 128
Class
- GeofieldProximitySourceBase
- Base class for Geofield Proximity Source plugins.
Namespace
Drupal\geofield\PluginCode
public function getProximity($lat, $lon) {
if (!$this
->isValidLocation($lat, $lon)) {
throw new InvalidPointException($this
->t('@proximity_handler reports Invalid Point coordinates', [
'@proximity_handler' => get_class($this),
]));
}
// Fetch the value of the units that have been set for this class. The
// constants are defined in the module file.
$radius = constant($this->units);
$origin = $this
->getOrigin();
if (!isset($origin['lat']) || !isset($origin['lon']) || $this
->isEmptyLocation($origin['lat'], $origin['lon'])) {
return NULL;
}
// Convert degrees to radians.
$origin_latitude = deg2rad($origin['lat']);
$origin_longitude = deg2rad($origin['lon']);
$destination_latitude = deg2rad($lat);
$destination_longitude = deg2rad($lon);
// Calculate proximity.
$proximity = $radius * acos(cos($origin_latitude) * cos($destination_latitude) * cos($destination_longitude - $origin_longitude) + sin($origin_latitude) * sin($destination_latitude));
if (!is_numeric($proximity)) {
throw new ProximityUnavailableException($this
->t('@proximity_handler not able to calculate valid Proximity value', [
'@proximity_handler' => get_class($this),
]));
}
return $proximity;
}