需要帮助转换和计算距离


need help converting and calculating distance

我被卡住了,需要一些帮助。我有一个数据库,有3000多个纬度和经度,我正试图将它们转换为十进制的纬度和经度。这样我就可以显示两个坐标之间的距离。这两个坐标在页面的url中传递。第一个(lat1&lon1)已转换,但第二个(lat2&lon2)尚未转换。我的数据库中的纬度和经度存储如下:26°56.34308'-094°41.32328',所以我认为可能应该删除逗号。我从一些来源得到了一些代码,但我不知道如何将它们正确地组合在一起。

///// 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";

数据库中有纬度和经度吗?什么样的数据库?一些数据库,如MySQL和PostgresQL,可以在一个简单的SQL查询中轻松计算2点之间的距离。为了做到这一点,您必须确保数据位于类似point列的空间列中。(如果point列中没有数据,则可以轻松创建新的point列,并使用非点列中的数据更新新列)

然后,要计算2个点之间的距离,只需执行SQL查询即可。

<?php
$dbh = mysql_connect($host, $username, $password);
mysql_select_db($dbname);
// Get distance between 2 points manually
$query = "select GLength(GeomFromText('LineString({$lat1} {$lon1},{$lat2} {$lon2})'))";
$dbr = mysql_query($query);
list($length) = mysql_fetch_array($dbr);
// Now, $length has the distance between lat1, lon1 and lat2, lon2
// To put points into a table use this query
// Assuming that your table has 2 columns point1 and point2
$query = "insert into mytable (point1, point2) values( geomfromtext('point({$lat1} {$lon1})'), geomfromtext('point({$lat2} {$lon2})') )";
mysql_query($query);
// Get distance between 2 points that are stored in point columns
// Assuming the table has 2 point columns point1 and point2
$query = "select GLength(GeomFromText(concat('LineString(', x(point1), ' ', y(point1), ',', x(point2), ' ', y(point2), ')'))) from mytable";
$dbr = mysql_query($query);
list($length) = mysql_fetch_array($dbr);
// Now $length has the distance between point1 and point2
?>

然后,您可以从那里进行任何距离转换。(此外,在查询中使用变量时要小心,不要像上面对$lat1$lon1等进行检查……如果您从用户输入中获取值,请确保并检查它们以确保它们只包含数字)

检查这些链接

MySql或PHP函数,用于将经度和纬度转换为十进制度数,

找到两个Lat/Long点之间距离的最快方法和

计算给定2点的距离,纬度和经度

也许你会在这里得到你的解决方案。