如何正确计算gpx文件的距离 - php

时间:2016-06-01 16:57:48

标签: php xml distance gpx

我做错了什么?我试图计算距离gpx(lat,lon)文件的距离,但收到了错误的结果。

function distance($lat1, $lon1, $lat2, $lon2) {
   $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);
   $kilometers = $dist * 60 * 1.1515 ;
   return $kilometers;
}

$gpx_file_source = simplexml_load_file($gpx_patch);
$j=0;
foreach($gpx_file_source->trk->trkseg->trkpt as $point){
        if($j%2==0){
             $lat1=$point->attributes()[0];
             $lon1=$point->attributes()[1];
        }else{
             $lat2=$point->attributes()[0];
             $lon2=$point->attributes()[1];
             $totaldistance=distance(''.$lat1.'', ''.$lon1.'', ''.$lat2.'', ''.$lon2.'').;
             $totaldistance+=$totaldistance;
        }   
  $j++;
}
echo $totaldistance; //0.00011797444243905

我的结果是: 0.00011797444243905 km

正确的结果: 17.35 km

GPX文件: http://itros.us/distance.gpx

1 个答案:

答案 0 :(得分:0)

我改变了计数功能并且正在工作!

3958*3.1415926*sqrt(($lat2-$lat1)*($lat2-$lat1) + cos($lat2/57.29578)*cos($lat1/57.29578)*($lon2-$lon1)*($lon2-$lon1))/180