首页 > 解决方案 > php中的总距离多个纬度和经度点?

问题描述

我试图打印出php中多个点之间的纬度总距离并打印出结果,以便我可以将其转储到mysql数据库中。例如我有

 MULTILINESTRING ((166.282008076887 -50.4981757000558,166.282014047837 -50.4981149924515,166.282009048641 -50.4981449926728,166.282021047737 -50.498071992073,166.281791047443 -50.4979599921101,166.281661047662 -50.4978739926141,166.281637048376 -50.4978479925945))

我无法过去在 爆炸,然后又在一个空间爆炸,所以我有 $points[0] 和 $point[1] 但我被困在这之外。我知道如何获得两点之间的距离,但我该如何超越呢?

标签: php

解决方案


您可以通过在逗号分隔符周围将字符串拆分为 Lat-Long 对。

    $str="166.282008076887 -50.4981757000558,166.282014047837 -50.4981149924515,166.282009048641 -50.4981449926728,166.282021047737 -50.498071992073,166.281791047443 -50.4979599921101,166.281661047662 -50.4978739926141,166.281637048376 -50.4978479925945";



$latLongArray=explode(",",$str);
echo "<pre>";
print_r($latLongArray);
echo "</pre>";

上面将字符串拆分为空格分隔的纬度/经度对,如下所示 -

Array
(
    [0] => 166.282008076887 -50.4981757000558
    [1] => 166.282014047837 -50.4981149924515
    [2] => 166.282009048641 -50.4981449926728
    [3] => 166.282021047737 -50.498071992073
    [4] => 166.281791047443 -50.4979599921101
    [5] => 166.281661047662 -50.4978739926141
    [6] => 166.281637048376 -50.4978479925945
)

接下来,将它们沿空间拆分,并将每个纬度/经度数组对保存在一个新数组中。您可以使用关联数组或 json,选择权在您手中。

$finalArray=array();
        for($i=0;$i<count($latLongArray);$i++){
            $tempStr=str_replace(" ",",",$latLongArray[$i]);
            $tempArr=explode(",",$tempStr);
            array_push($finalArray,$tempArr);
        }


echo "<pre>";
        print_r($finalArray);
        echo "</pre>";

这将打印保存在最终数组中的纬度/经度对 -

Array
(
    [0] => Array
        (
            [0] => 166.282008076887
            [1] => -50.4981757000558
        )

    [1] => Array
        (
            [0] => 166.282014047837
            [1] => -50.4981149924515
        )

    [2] => Array
        (
            [0] => 166.282009048641
            [1] => -50.4981449926728
        )

    [3] => Array
        (
            [0] => 166.282021047737
            [1] => -50.498071992073
        )

    [4] => Array
        (
            [0] => 166.281791047443
            [1] => -50.4979599921101
        )

    [5] => Array
        (
            [0] => 166.281661047662
            [1] => -50.4978739926141
        )

    [6] => Array
        (
            [0] => 166.281637048376
            [1] => -50.4978479925945
        )

)

希望这可以帮助。

稍后添加——

$totalDistance=0;
for($i=0;$i<count($finalArray);$i++){
$totalDistance+= calculateDistance($finalArray[$i],$finalArray[$i+1]);
}
echo $totalDistance;

function calculateDistance($pointA,$pointB){
// calculate distance between the two points
// ie, $pointA->Lat/Long, $pointB->Lat/Long
}

推荐阅读