You are here

public static function GeolocationItem::decimalToSexagesimal in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldType/GeolocationItem.php \Drupal\geolocation\Plugin\Field\FieldType\GeolocationItem::decimalToSexagesimal()

Transform decimal notation to sexagesimal.

Sexagesimal means a string like - X° Y' Z"

Parameters

float|string $decimal: Either float or float-castable location.

Return value

string|false The sexagesimal notation or FALSE on error.

3 calls to GeolocationItem::decimalToSexagesimal()
GeolocationField::addSelfTokens in src/Plugin/views/field/GeolocationField.php
Add any special tokens this field might use for itself.
GeolocationFieldProvider::geolocationItemTokens in src/Plugin/geolocation/DataProvider/GeolocationFieldProvider.php
GeolocationLatlngWidget::formElement in src/Plugin/Field/FieldWidget/GeolocationLatlngWidget.php
Returns the form for a single field widget.

File

src/Plugin/Field/FieldType/GeolocationItem.php, line 241

Class

GeolocationItem
Plugin implementation of the 'geolocation' field type.

Namespace

Drupal\geolocation\Plugin\Field\FieldType

Code

public static function decimalToSexagesimal($decimal = '') {
  $negative = FALSE;
  $decimal = (double) $decimal;
  if ($decimal < 0) {
    $negative = TRUE;
    $decimal = abs($decimal);
  }
  $degrees = floor($decimal);
  $rest = $decimal - $degrees;
  $minutes = floor($rest * 60);
  $rest = $rest * 60 - $minutes;
  $seconds = round($rest * 60, 4);
  $value = $degrees . '°';
  if (!empty($minutes)) {
    $value .= ' ' . $minutes . '\'';
  }
  if (!empty($seconds)) {
    $value .= ' ' . $seconds . '"';
  }
  if ($negative) {
    $value = '-' . $value;
  }
  return $value;
}