【问题标题】:convert decimal degrees to degrees minutes and then decimal seconds将十进制度转换为度分,然后转换为小数秒
【发布时间】:2019-10-12 17:52:59
【问题描述】:

我成功地使用下面的 PHP 代码(这里有很好的资源!)将十进制度转换为度分秒。我正在使用的导航应用程序将坐标导出为十进制,因此 50.360503 并显示 50°21.638N。在航海导航中,我们在使用纸质海图时使用度分,然后是十分之一,而不是真正的秒。

谁能给我一些关于如何更改小数而不是下面的代码的指示?

function convertDecimalToDMSLongitude($dec)
{

// Converts decimal longitude / latitude to DMS
// ( Degrees / minutes / seconds )
// This is the piece of code which may appear to
// be inefficient, but to avoid issues with floating
// point math we extract the integer part and the float
// part by using a string function.

$vars = explode(".",$dec);
$deg = $vars[0];
$tempma = "0.".$vars[1];

$tempma = $tempma * 3600;
$min = floor($tempma / 60);
$sec = $tempma - ($min*60);
$sec = number_format($sec, 0);


if ($deg <= 0) {
  $hemisphere = 'W';
} else {
  $hemisphere = 'E';
}

$degrees = abs($deg);
if ($degrees <= 9) {
  $deg = '00'.$degrees;
} elseif ($degrees >= 10) {
  $deg = '0'.$degrees;
} elseif ($degrees >= 100) {
  $deg = $degrees;
}

return $deg.'.'.$min.'.'.$sec.''.$hemisphere;
}

到目前为止,我有这个(下)很接近;当输入 50.360503 作为小数时,它返回 50°21630180.N。我只需要从剩余的数字中分出 21 分钟。 GPS 应用程序(Navionics 或 iSailor)导出为十进制并在屏幕上显示 50°21.638N。

$vars = explode(".",$dec);
$deg = $vars[0];
$tempma = "0.".$vars[1];

//$tempma = $tempma * 3600;
//$min = floor($tempma / 60);
//$sec = $tempma - ($min*60);
//$sec = number_format($sec, 0);

$min = $vars[1]*60;

$sec = '';

if ($deg >= 0) {
  $hemisphere = 'N';
} else {
  $hemisphere = 'S';
}

return abs($deg).'.'.$min.'.'.$sec.' '.$hemisphere;

【问题讨论】:

    标签: math gps coordinates


    【解决方案1】:

    知道了!

    $vars = explode(".",$dec);
    $deg = $vars[0];
    $min = "0.".$vars[1];
    
    $min = $min * 3600;
    $min = $min / 60;
    $min = number_format((float)$min, 3, '.', '');
    
    
    if ($deg <= 0) {
      $hemisphere = 'W';
    } else {
      $hemisphere = 'E';
    }
    
    //add 00 if single degrees
    $degrees = abs($deg);
    if ($degrees <= 9) {
      $deg = '00'.$degrees;
    } elseif ($degrees >= 10) {
      $deg = '0'.$degrees;
    } elseif ($degrees >= 100) {
      $deg = $degrees;
    }
    
    //add a 0 if a single number
    if ($min <= 9) {
      $min = '0'.$min;
    }
    
    return $deg.'° '.$min.' '.$hemisphere;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 2013-10-27
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多