如何计算GPX文件的距离?

gue*_*rda 22 xml algorithm gps gpx

我有一个带GPS轨道的GPX文件.现在我想计算这条赛道所覆盖的距离.

计算这个的最佳方法是什么?

cdo*_*ner 24

计算两点(GPX文件中每对航点)之间距离的传统方法是使用Haversine公式.

我有一个SQL Server函数来实现该算法.这应该很容易翻译成其他语言:

create function dbo.udf_Haversine(@lat1 float, @long1 float, 
                   @lat2 float, @long2 float) returns float begin
    declare @dlon float, @dlat float, @rlat1 float, 
                 @rlat2 float, @rlong1 float, @rlong2 float, 
                 @a float, @c float, @R float, @d float, @DtoR float

    select @DtoR = 0.017453293
    select @R = 3959      -- Earth radius

    select 
        @rlat1 = @lat1 * @DtoR,
        @rlong1 = @long1 * @DtoR,
        @rlat2 = @lat2 * @DtoR,
        @rlong2 = @long2 * @DtoR

    select 
        @dlon = @rlong1 - @rlong2,
        @dlat = @rlat1 - @rlat2

    select @a = power(sin(@dlat/2), 2) + cos(@rlat1) * 
                     cos(@rlat2) * power(sin(@dlon/2), 2)
    select @c = 2 * atn2(sqrt(@a), sqrt(1-@a))
    select @d = @R * @c

    return @d 
end
Run Code Online (Sandbox Code Playgroud)

以千里为单位返回距离.对于千米,将地球半径替换为等效公里数.

是一个更深入的解释.

编辑:此功能足够快且足够准确,可以使用邮政编码数据库进行半径搜索.多年来它一直在这个网站上做得很好(但现在不再这样了,因为现在链接已被破坏).