尝试从 .gpx 文件计算每个 gps 点之间的距离。我尝试了 2 种不同的公式。这个应该更准确:dist = 6378.388 * acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon2 - lon1))
这也适用于短距离:距离 = sqrt(dx * dx + dy * dy)
每个点之间的距离最大。100米。我用我的程序计算了距离,得到了 26.8 公里。然后我将 .gpx 文件导入到 goole earth 中,距离为 21 公里。我不知道为什么这些值差异如此之大。也许你可以帮助我。
这里有代码(只是相关的片段)来计算与 gpx 文件的距离:
...
$xml = file_get_contents($file);
$doc = new DOMDocument();
$doc->loadXML($xml);
$track = array();
/* get the elevation for each gps point */
...
/* get the time for each gps point */
...
/* get the position for each gps point*/
$trkpts = $doc->getElementsByTagName('trkpt');
$i=0;
foreach ($trkpts as $trkpt) {
$track[$i]['position'] = array();
$track[$i]['position']['lon'] = $trkpt->getAttribute('lat');
$track[$i]['position']['lat'] = $trkpt->getAttribute('lon');
$i++;
}
/* calculate the distance for each gps point */
ini_set('precision', '50');
for($i2=0;$i2<($i);$i2++){
if($i2==0){
$track[$i2]['distance'] = 0;
/* get first point */
$lastlat = deg2rad($track[$i2]['position']['lat']);
$lastlon = deg2rad($track[$i2]['position']['lon']);
$lastele = $track[$i2]['ele'];
}
else{
$aktlat = deg2rad($track[$i2]['position']['lat']);
$aktlon = deg2rad($track[$i2]['position']['lon']);
$aktele = $track[$i2]['ele'];
/* if coordinates differ calculate distance */
if(($aktlat-$lastlat)!=0 || ($aktlon-$lastlon)!=0){
$track[$i2]['distance'] = acos(sin($lastlat)*sin($aktlat)+cos($lastlat)*cos($aktlat)*cos($aktlon-$lastlon));
$track[$i2]['distance'] = $track[$i2]['distance'] * 6378137;
/* calculate the elevation difference */
$track[$i2]['distance'] = sqrt(pow($track[$i2]['distance'],2)+pow(($aktele-$lastele),2));
}
else{
$track[$i2]['distance'] = 0;
}
/* save act point as last point */
$lastlat = deg2rad($aktlat);
$lastlon = deg2rad($aktlon);
$lastele = $aktele;
}
}
这是每个想要检查的人的 gpx 文件:http: //mein-sporttagebuch.de/userfiles/tempfiles/runtastic_20130825_1054_Radfahren.gpx
感谢您的任何建议。
编辑 这里是获取海拔的代码(从海拔我计算一些更多的措施,如最高点和最深点):
$eles = $doc->getElementsByTagName('ele');
$i=0;
$minheight = 0;
$maxheight = 0;
$asc = 0;
$desc = 0;
$lastheight = 0;
foreach ($eles as $ele) {
if($i>0){
if($lastheight<$ele->nodeValue){
$asc += $ele->nodeValue-$lastheight;
}
else{
$desc += $lastheight-$ele->nodeValue;
}
}
$track[$i] = array();
$track[$i]['ele'] = $ele->nodeValue;
if($minheight>$ele->nodeValue || $i==0){
$minheight = $ele->nodeValue;
}
if($maxheight<$ele->nodeValue){
$maxheight = $ele->nodeValue;
}
$lastheight = $ele->nodeValue;
$i++;
}