PHP Code Sample : Convert GPS / NMEA Lat Long to Decimal Degrees

Print Friendly, PDF & Email

This piece of code converts NMEA/GPS Lat -Long to Decimal Degrees. Code comments are self explanatory. The code is not graceful, but it works.

function degree2decimal($deg_coord){
//reference http://www.directionsmag.com/site/latlong-converter
//GPS/NMEA fixes are in Degree Minutes.m format
//for Google maps we need to convert them to decimal degrees
//sample format of GPS 4533.35 is 45 degrees and 33.35 minutes
//formula is as follows//Degrees=Degrees 
//.d = M.m/60//Decimal Degrees=Degrees+.d
$degree=(int)($deg_coord/100); //simple way
$minutes= $deg_coord-($degree*100);
$dotdegree=$minutes/60;
$decimal=$degree+$dotdegree;
$direction=substr($deg_coord,-1);
//South latitudes and West longitudes need to return a negative result
if (($direction=="S") or ($direction=="W"))       
 { 
$decimal=$decimal*(-1);}
$decimal=number_format($decimal,4,'.',''); //truncate decimal to 4 places
return $decimal;
}
Ishan Karve

About Ishan Karve

Ishan Karve is just an every day normal guy next door who happens to be an Electronics Engineer by profession and dabbles with PHP, Javascript, C++ and python. His interests vary as seasons change.. they change from astronomy to soul searching. This site is just a reflection of what he does to keep his mind engaged when he is not occupied by work and family. He is an extremely objective guy and is always ready for some good arguments.. of course over a glass of 40% proof alcohol.
This entry was posted in PHP, Programming and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *