在PHP中将度,分,秒(DMS)转换为十进制

Azl*_*ara 6 php api maps dms

目前,我正在学习使用Google Maps API.根据我的阅读,API需要十进制度(DD)的纬度和经度.

在我的数据库中,数据存储为DMS.

例如,110°29'01.1"

我想问你们是否有任何DMS到DD的DD.并且,转换器必须从单个字符串接受,如上例所示.

问候

小智 11

您可以尝试这是否适合您.

<?php

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

    // Converting DMS ( Degrees / minutes / seconds ) to decimal format
    return $deg+((($min*60)+($sec))/3600);
}    

function DDtoDMS($dec)
{
    // Converts decimal format to DMS ( Degrees / minutes / seconds ) 
    $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)


bic*_*cle 5

这是您在 DMS 值中传递纬度、经度并返回转换后的 DMS 字符串的地方。简单易行

function DECtoDMS($latitude, $longitude)
{
    $latitudeDirection = $latitude < 0 ? 'S': 'N';
    $longitudeDirection = $longitude < 0 ? 'W': 'E';

    $latitudeNotation = $latitude < 0 ? '-': '';
    $longitudeNotation = $longitude < 0 ? '-': '';

    $latitudeInDegrees = floor(abs($latitude));
    $longitudeInDegrees = floor(abs($longitude));

    $latitudeDecimal = abs($latitude)-$latitudeInDegrees;
    $longitudeDecimal = abs($longitude)-$longitudeInDegrees;

    $_precision = 3;
    $latitudeMinutes = round($latitudeDecimal*60,$_precision);
    $longitudeMinutes = round($longitudeDecimal*60,$_precision);

    return sprintf('%s%s° %s %s %s%s° %s %s',
        $latitudeNotation,
        $latitudeInDegrees,
        $latitudeMinutes,
        $latitudeDirection,
        $longitudeNotation,
        $longitudeInDegrees,
        $longitudeMinutes,
        $longitudeDirection
    );

}
Run Code Online (Sandbox Code Playgroud)


小智 5

我编写了一个 PHP 函数来执行问题所要求的操作:将以度/分/秒为单位的字符串转换为十进制度数。它接受多种不同格式的字符串,并遵循方向 (NSEW)。

\n\n

这是代码:

\n\n
<?php\nfunction convertDMSToDecimal($latlng) {\n    $valid = false;\n    $decimal_degrees = 0;\n    $degrees = 0; $minutes = 0; $seconds = 0; $direction = 1;\n    // Determine if there are extra periods in the input string\n    $num_periods = substr_count($latlng, \'.\');\n    if ($num_periods > 1) {\n        $temp = preg_replace(\'/\\./\', \' \', $latlng, $num_periods - 1); // replace all but last period with delimiter\n        $temp = trim(preg_replace(\'/[a-zA-Z]/\',\'\',$temp)); // when counting chunks we only want numbers\n        $chunk_count = count(explode(" ",$temp));\n        if ($chunk_count > 2) {\n            $latlng = $temp; // remove last period\n        } else {\n            $latlng = str_replace("."," ",$latlng); // remove all periods, not enough chunks left by keeping last one\n        }\n    }\n\n    // Remove unneeded characters\n    $latlng = trim($latlng);\n    $latlng = str_replace("\xc2\xba","",$latlng);\n    $latlng = str_replace("\'","",$latlng);\n    $latlng = str_replace("\\"","",$latlng);\n    $latlng = substr($latlng,0,1) . str_replace(\'-\', \' \', substr($latlng,1)); // remove all but first dash\n\n    if ($latlng != "") {\n        // DMS with the direction at the start of the string\n        if (preg_match("/^([nsewNSEW]?)\\s*(\\d{1,3})\\s+(\\d{1,3})\\s+(\\d+\\.?\\d*)$/",$latlng,$matches)) {\n            $valid = true;\n            $degrees = intval($matches[2]);\n            $minutes = intval($matches[3]);\n            $seconds = floatval($matches[4]);\n            if (strtoupper($matches[1]) == "S" || strtoupper($matches[1]) == "W")\n                $direction = -1;\n        }\n        // DMS with the direction at the end of the string\n        if (preg_match("/^(-?\\d{1,3})\\s+(\\d{1,3})\\s+(\\d+(?:\\.\\d+)?)\\s*([nsewNSEW]?)$/",$latlng,$matches)) {\n            $valid = true;\n            $degrees = intval($matches[1]);\n            $minutes = intval($matches[2]);\n            $seconds = floatval($matches[3]);\n            if (strtoupper($matches[4]) == "S" || strtoupper($matches[4]) == "W" || $degrees < 0) {\n                $direction = -1;\n                $degrees = abs($degrees);\n            }\n        }\n        if ($valid) {\n            // A match was found, do the calculation\n            $decimal_degrees = ($degrees + ($minutes / 60) + ($seconds / 3600)) * $direction;\n        } else {\n            // Decimal degrees with a direction at the start of the string\n            if (preg_match("/^(-?\\d+(?:\\.\\d+)?)\\s*([nsewNSEW]?)$/",$latlng,$matches)) {\n                $valid = true;\n                if (strtoupper($matches[2]) == "S" || strtoupper($matches[2]) == "W" || $degrees < 0) {\n                    $direction = -1;\n                    $degrees = abs($degrees);\n                }\n                $decimal_degrees = $matches[1] * $direction;\n            }\n            // Decimal degrees with a direction at the end of the string\n            if (preg_match("/^([nsewNSEW]?)\\s*(\\d+(?:\\.\\d+)?)$/",$latlng,$matches)) {\n                $valid = true;\n                if (strtoupper($matches[1]) == "S" || strtoupper($matches[1]) == "W")\n                    $direction = -1;\n                $decimal_degrees = $matches[2] * $direction;\n            }\n        }\n    }\n    if ($valid) {\n        return $decimal_degrees;\n    } else {\n        return false;\n    }\n}\n?>\n
Run Code Online (Sandbox Code Playgroud)\n\n

这里是 Github 上的测试用例:https://github.com/prairiewest/PHPconvertDMSToDecimal

\n


Azl*_*ara 2

解决了。

\n\n
 <?php\n\n function DMStoDD($input)\n{\n    $deg = " " ;\n    $min = " " ;\n    $sec = " " ;  \n    $inputM = " " ;        \n\n\n    print "<br> Input is ".$input." <br>";\n\n    for ($i=0; $i < strlen($input); $i++) \n    {                     \n        $tempD = $input[$i];\n         //print "<br> TempD [$i] is : $tempD"; \n\n        if ($tempD == iconv("UTF-8", "ISO-8859-1//TRANSLIT", \'\xc2\xb0\') ) \n        { \n            $newI = $i + 1 ;\n            //print "<br> newI is : $newI"; \n            $inputM =  substr($input, $newI, -1) ;\n            break; \n        }//close if degree\n\n        $deg .= $tempD ;                    \n    }//close for degree\n\n     //print "InputM is ".$inputM." <br>";\n\n    for ($j=0; $j < strlen($inputM); $j++) \n    { \n        $tempM = $inputM[$j];\n         //print "<br> TempM [$j] is : $tempM"; \n\n        if ($tempM == "\'")  \n         {                     \n            $newI = $j + 1 ;\n             //print "<br> newI is : $newI"; \n            $sec =  substr($inputM, $newI, -1) ;\n            break; \n         }//close if minute\n         $min .= $tempM ;                    \n    }//close for min\n\n        $result =  $deg+( (( $min*60)+($sec) ) /3600 );\n\n\n        print "<br> Degree is ". $deg*1 ;\n        print "<br> Minutes is ". $min ;\n        print "<br> Seconds is ". $sec ;\n        print "<br> Result is ". $result ;\n\n\nreturn $deg + ($min / 60) + ($sec / 3600);\n\n   }\n?>\n
Run Code Online (Sandbox Code Playgroud)\n