本文介绍了在 PHP 中将度、分、秒 (DMS) 转换为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Currently, I'm learning to use Google Maps API. From what I read, the API require the latitude and longitude in Decimal Degree (DD).

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

In my database, the data is stored as DMS.

示例,110° 29' 01.1"

Example, 110° 29' 01.1"

我想问你们是否有任何 DMS 到 php 中的 DD.而且,转换器必须像上面的例子一样接受来自单个字符串.

I would to ask if you guys have any DMS to DD in php. And, the converter must accept from a single string like the example above.

问候

推荐答案

已解决.

 <?php

 function DMStoDD($input)
{
    $deg = " " ;
    $min = " " ;
    $sec = " " ;
    $inputM = " " ;


    print "<br> Input is ".$input." <br>";

    for ($i=0; $i < strlen($input); $i++)
    {
        $tempD = $input[$i];
         //print "<br> TempD [$i] is : $tempD";

        if ($tempD == iconv("UTF-8", "ISO-8859-1//TRANSLIT", '°') )
        {
            $newI = $i + 1 ;
            //print "<br> newI is : $newI";
            $inputM =  substr($input, $newI, -1) ;
            break;
        }//close if degree

        $deg .= $tempD ;
    }//close for degree

     //print "InputM is ".$inputM." <br>";

    for ($j=0; $j < strlen($inputM); $j++)
    {
        $tempM = $inputM[$j];
         //print "<br> TempM [$j] is : $tempM";

        if ($tempM == "'")
         {
            $newI = $j + 1 ;
             //print "<br> newI is : $newI";
            $sec =  substr($inputM, $newI, -1) ;
            break;
         }//close if minute
         $min .= $tempM ;
    }//close for min

        $result =  $deg+( (( $min*60)+($sec) ) /3600 );


        print "<br> Degree is ". $deg*1 ;
        print "<br> Minutes is ". $min ;
        print "<br> Seconds is ". $sec ;
        print "<br> Result is ". $result ;


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

   }
?>

这篇关于在 PHP 中将度、分、秒 (DMS) 转换为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-13 18:48