有没有办法使用PHP转换经度和纬度?

时间:2012-03-07 23:58:34

标签: php

我有一个包含超过3000个地点的纬度和经度的数据库。在我的表中,纬度和经度看起来像这样:

纬度:26°56.34308'
经度:-094°41.32328'

我需要将这些数字转换为小数分钟而不使用 - 和数字。原因是计算到另一个地方的距离。

有没有办法用PHP做到这一点?

这是我到目前为止所需要的一些帮助。

    ///// Get the two locations from the url

$lat1 = $_GET[lat1];
$lon1 = $_GET[lon1];

////// lat2 & Lon2 are the ones that need to be converted

$lat2 = $_GET[lat2];
$lon2 = $_GET[lon2];

///// Convert lat2 & lon2 into decimal format

$pos1 = strrpos($mystring, "°");
$pos2 = strrpos($mystring, ".");
$pos3 = strrpos($mystring, "'");
// Get subsring from a string: substr(source, start, length)
$deg = substr($mystring, 0, $pos1);
$min = substr($mystring, $pos1, $pos2 - $pos1);
$sec = substr($mystring, $pos2, $pos3 - $pos2);

function DMStoDEC($deg,$min,$sec) {
// Converts DMS ( Degrees / minutes / seconds )
// to decimal format longitude / latitude
    return $deg+((($min*60)+($sec))/3600);
}

//////calculate the distance

function distance($lat1, $lon1, $lat2, $lon2, $unit) {
    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +
        cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $unit = strtoupper($unit);

    if ($unit == "K") {
        return ($miles * 1.609344);
    } else if ($unit == "N") {
        return ($miles * 0.8684);
    } else {
        return $miles;
    }
}

// Miles
echo distance($lat1, $lon1, $lat2, $lon2, "m") . " miles<br><br>";

//Kilometers
echo distance($lat1, $lon1, $lat2, $lon2, "k") . " kilometers<br><br>";

//Nautical miles
echo distance($lat1, $lon1, $lat2, $lon2, "N") . " Nautical miles";

1 个答案:

答案 0 :(得分:1)

我发现了这个:

<?php

function DMStoDEC($deg,$min,$sec) {
// Converts DMS ( Degrees / minutes / seconds ) 
// to decimal format longitude / latitude
    return $deg+((($min*60)+($sec))/3600);
}    

function DECtoDMS($dec) {
// Converts decimal longitude / latitude to DMS
// ( Degrees / minutes / seconds ) 

// This is the piece of code which may appear to 
// be inefficient, but to avoid issues with floating
// point math we extract the integer part and the float
// part by using a string function.

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

?> 

http://www.web-max.ca/PHP/misc_6.php

修改
将你的字符串解析为deg,min,sec:

$pos1 = strrpos($mystring, "°");
$pos2 = strrpos($mystring, ".");
$pos3 = strrpos($mystring, "'");
// Get subsring from a string: substr(source, start, length)
$deg = substr($mystring, 0, $pos1); 
$min = substr($mystring, $pos1, $pos2 - $pos1);
$sec = substr($mystring, $pos2, $pos3 - $pos2);