在PHP中完全偏离多个经度和纬度点?

时间:2018-08-19 08:32:04

标签: 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],但是我超出了这个范围。我知道如何获得两点之间的距离,但是我如何超越呢?

1 个答案:

答案 0 :(得分:0)

您可以通过在逗号定界符周围将字符串拆分为长尾对。

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