You are here

public function ViewsBoundaryArgument::getCoordinates in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/geolocation/Location/ViewsBoundaryArgument.php \Drupal\geolocation\Plugin\geolocation\Location\ViewsBoundaryArgument::getCoordinates()

Get map location.

Parameters

int $location_option_id: Location option ID.

array $location_option_settings: The current feature settings.

mixed $context: Context like field formatter, field widget or view.

Return value

array With content 'lat' => latitude 'lng' => longitude

Overrides LocationBase::getCoordinates

File

src/Plugin/geolocation/Location/ViewsBoundaryArgument.php, line 43

Class

ViewsBoundaryArgument
Derive center from proximity argument.

Namespace

Drupal\geolocation\Plugin\geolocation\Location

Code

public function getCoordinates($location_option_id, array $location_option_settings, $context = NULL) {
  if ($displayHandler = self::getViewsDisplayHandler($context)) {

    /** @var \Drupal\geolocation\Plugin\views\argument\BoundaryArgument $argument */
    $argument = $displayHandler
      ->getHandler('argument', $location_option_id);
    if (empty($argument)) {
      return FALSE;
    }
    $values = $argument
      ->getParsedBoundary();

    // See documentation at
    // http://tubalmartin.github.io/spherical-geometry-php/#LatLngBounds
    $latitude = ($values['lat_south_west'] + $values['lat_north_east']) / 2;
    $longitude = ($values['lng_south_west'] + $values['lng_north_east']) / 2;
    if ($values['lng_south_west'] > $values['lng_north_east']) {
      $longitude = $longitude == 0 ? 180 : fmod(fmod($longitude + 180 - -180, 360) + 360, 360) + -180;
    }
    return [
      'lat' => $latitude,
      'lng' => $longitude,
    ];
  }
  return parent::getCoordinates($location_option_id, $location_option_settings, $context);
}