phpgoogle-mapsdegrees

Converting Degree, Minutes, Seconds (DMS) to decimal in PHP


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

In my database, the data is stored as DMS.

Example, 110° 29' 01.1"

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


Solution

  • Solved.

     <?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);
    
       }
    ?>