从两个纬度和方向

时间:2016-04-06 12:55:38

标签: php

我正在开发一个应用程序,我需要介于两个lat long之间的lat长度。

所以我拥有的是

1)拉长起点

2)Lat long of end point

3)方向,如南(180度)或北(0度)

4)两点之间的距离

现在我要做的是将整个路径分成若干块,就像我将整个路径划分10公里

  

因此,假设总距离 100KM,那么我将获得10个块。

     

如果 200KM则会有20个块。

所以现在我想从两个纬度很长的时间找出这些点的长度。

因此所有20个点应该在一条线上并且距离相等。

如何实现?

2 个答案:

答案 0 :(得分:3)

您可以尝试取两个Lat Lon点之间的差异,将它们除以块的数量。这将给出“增量”。然后可以在循环中根据方向添加或取消,以创建两者之间的点。

考虑以下我做的测试:

//Position 1
$lat1 = 53.2226754;
$lon1 = 0.124584;

//Position 2
$lat2 = 52.212445;
$lon2 = -0.12458;

//Differences in Lat / Lon
$latDif = round($lat1 - $lat2, 7);
$lonDif = round($lon1 - $lon2, 7);

//Calculate Step / Increment
//I used 10 as an example for the number of steps
$chunks = 10;
$latStep = round($latDif / $chunks, 7);
$lonStep = round($lonDif / $chunks, 7);

//New Lat Lon starts at start point
$newLat = $lat1;
$newLon = $lon1;


echo $lat1 . ", " . $lon1 . "<br>"; //Start Point

for($i = 1; $i < $chunks; $i++){

    //Direction could be substituted here

    if($lat1 < $lat2){
        $newLat = $newLat + $latStep; //Going North
    } else {
        $newLat = $newLat - $latStep; //Going South
    }

    if($lon1 < $lon2){
        $newLon = $newLon + $lonStep; //Going East
    } else {
        $newLon = $newLon - $lonStep; //Going West
    }

    echo $newLat . ", " . $newLon . "<br>"; //New Point
}

echo $lat2 . ", " . $lon2 . "<br>"; //End Point

这导致以下Lat Lon输出:

53.2226754,0.124584;
53.1216524,0.0996676;
53.0206294,0.0747512;
52.9196064,0.0498348;
52.8185834,0.0249184;
52.7175604,0.0000002;
52.6165374,-0.0249144;
52.5155144,-0.0498308;
52.4144914,-0.0747472;
52.3134684,-0.0996636;
52.212445,-0.12458;

我在https://jsfiddle.net/sve52r7n/上绘制了这些内容并获得了以下内容:

online geoplanner

Geoplanner output of Lat Lon

答案 1 :(得分:2)

根据@Piskvor's comment,由于墨卡托投影地图,我以前的方法不会长距离工作。

经过一些互联网挖掘,一些令人头疼和严肃的思维难以置信,我想我已经创建了一个PHP方法来计算两个Lat Lon点之间的Great Circle路径。

对于以下示例,我使用起点作为伦敦希思罗机场和终点作为JFK国际机场。

首先,我们需要使用Haversine Formula计算连接它们的Great Circle上A点和B点之间的距离:

//Must convert to Radians for use with Trig functions
$lat1 = deg2rad(51.4700223); $lon1 = deg2rad(-0.4542955); // LHR
$lat2 = deg2rad(40.6413111); $lon2 = deg2rad(-73.7781391); // JFK

$eRadius = 6367000; // Earth Radius in metres

//Difference between lat and lon
$difLat = $lat2 - $lat1;
$difLon = $lon2 - $lon1;

//Some mathematical magic
$a = sin($difLat / 2) * sin($difLat / 2) + 
     cos($lat1) * cos($lat2) * 
     sin($difLon / 2) * sin($difLon / 2);

$c = 2 * atan2(sqrt($a), sqrt(1-$a));

//Distance from pA to pB along the great circle in metres
$t_distance = $eRadius * $c;

现在我们可以计算路径点的总距离:

//Step distance in metres
$sDist = 800000; //800km

//Lat Lon Storage;
$lat = []; //Short hand for array(); PHP > 5.4
$lon = [];

//Number of steps - rounded down!
$steps = floor($t_distance / $sDist);

//Percentage of distance for 1 step
$_f = 100 / $steps;

//CALCULATE POINTS
for($i = 0; $i <= $steps; $i++){
    $f = ($_f * $i) / 100; // value between 0-1 corresponding with percentage of step with the step number
    $_d = $t_distance / $eRadius; //Angular Distance

    $a = sin((1- $f) * $_d) / sin($_d);
    $b = sin($f * $_d) / sin($_d);

    $x = ($a * cos($lat1) * cos($lon1)) + ($b * cos($lat2) * cos($lon2));
    $y = ($a * cos($lat1) * sin($lon1)) + ($b * cos($lat2) * sin($lon2));
    $z = ($a * sin($lat1)) + ($b * sin($lat2));

    //New Lat Lon point
    $nLat = round(atan2($z, sqrt(pow($x, 2) + pow($y, 2))),7);
    $nLon = round(atan2($y, $x),7);

    //Push to Lat Lon arrays, converting Radians back to Degrees
    array_push($lat, rad2deg($nLat));
    array_push($lon, rad2deg($nLon));
}

使用简单的foreach循环,Lat Long点类似于:

51.470024866283,-0.45429823575923
53.302510052872,-13.752895032598
53.561704700234,-27.704938735627
52.21669773532,-41.302699715615
49.42997998883,-53.701145438835
45.476181591127,-64.517161945995
40.6413111,-73.7781391

online geoplanner上绘制这些点会得到以下结果:

enter image description here

我还想指出,在创建此方法时,这两个网站确实帮助了我: