PHP:使用度,小步和秒来格式化纬度和经度

rag*_*lka 6 php latitude-longitude coordinates

我怎么能转换这个:

26.72773551940918
Run Code Online (Sandbox Code Playgroud)

进入这样的事情:

22°12'42"N
Run Code Online (Sandbox Code Playgroud)

这里的诀窍是坐标实际上是Latitude和Longitude,我只需要正确格式化它们.

SER*_*PRO 14

您可以在此处找到执行此操作的功能

<?php

function DMStoDEC($deg,$min,$sec)
{

// Converts DMS ( Degrees / minutes / seconds ) 
// to decimal format longitude / latitude

    return $deg+((($min*60)+($sec))/3600);
}    

function DECtoDMS($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);

    return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
}    

?>
Run Code Online (Sandbox Code Playgroud)


Leo*_*sov 7

lat/lon coords用(粗略地说)写入基数为60的数字系统.这是你如何转换它们:

function fraction_to_min_sec($coord)
{
  $isnorth = $coord>=0;
  $coord = abs($coord);
  $deg = floor($coord);
  $coord = ($coord-$deg)*60;
  $min = floor($coord);
  $sec = floor(($coord-$min)*60);
  return array($deg, $min, $sec, $isnorth ? 'N' : 'S');
  // or if you want the string representation
  return sprintf("%d&deg;%d'%d\"%s", $deg, $min, $sec, $isnorth ? 'N' : 'S');
}
Run Code Online (Sandbox Code Playgroud)

我说我的函数比@ SeRPRo的函数具有更好的数值稳定性.