Translate decimal coordinates to degrees, minutes, hemisphere in PHP

map

Here’s how:

/**
 * * Helper function to translate lat or long coords in decimal degrees, to their degrees, minutes, hemisphere counterparts
 * @param $data
 * @param $lat_or_long
 * @return array
 */
function zar_calc_deg($coords, $lat_or_long) {
    $unfloat = explode('.', $coords);
    $deg = $unfloat[0];

    if ($coords < 0) {
        $deg = $deg * -1;
        $hem = ('long' == $lat_or_long) ? 'W' : 'S';
    } else {
        $hem = ('lat' == $lat_or_long) ? 'N' : 'E';
    }

    if (!isset($unfloat[1])) $unfloat[1] = 0;
    $mins = floor(($unfloat[1]/100) * 60);

    return array($lat_or_long.'_deg' => $deg, $lat_or_long.'_mins' => $mins, $lat_or_long.'_hem' => $hem);
}

Leave a Reply

Jouw e-mailadres wordt niet gepubliceerd. Verplichte velden zijn gemarkeerd met *

De volgende HTML tags en attributen zijn toegestaan: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>