Windows Phone 8.1 -计算覆盖距离的问题

本文关键字:覆盖 距离 问题 计算 Phone Windows | 更新日期: 2023-09-27 17:49:42

我正在写一个实时计算覆盖距离的应用程序。我将我的实际位置与之前的位置进行比较,计算它们之间的距离(使用哈弗斯公式),并将所有内容相加。这给了我一些结果,距离在增长,一切似乎都在工作。但问题在于准确性。我在一条大约15公里长的路线上多次测试了这款应用,覆盖的距离总是比我车上的计数器显示的要大。差别总是不同的——从500米到甚至2公里。

这是我的地理定位器对象:

Geolocator gl = new Geolocator() {
    DesiredAccuracy = PositionAccuracy.High, MovementThreshold = 20, ReportInterval = 50
};

在构造函数中,我声明当位置改变时,"OnPositionChanged"方法应该被触发,并且该方法还可以找到我的实际位置:

gl.PositionChanged += OnPositionChanged;
setMyLocation();
这是"OnPositionChanged()"方法:
async private void OnPositionChanged(Geolocator sender, PositionChangedEventArgs e)
{
    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {       
        setMyLocation();
    });
}

这是setMyLocation()方法:

private async void setMyLocation()
{
    try
    {
        p = new Position();
        location = await gl.GetGeopositionAsync(TimeSpan.FromMinutes(5), TimeSpan.FromSeconds(5));
        p.Latitude = location.Coordinate.Point.Position.Latitude;
        p.Longitude = location.Coordinate.Point.Position.Longitude;
        obla = location.Coordinate.Point.Position.Latitude;
        oblo = location.Coordinate.Point.Position.Longitude;
        if (prev_location.Latitude == 0)
        {
            prev_location = p;    
        }
        positions.Add(new BasicGeoposition() {
            Latitude = location.Coordinate.Latitude, 
            Longitude = location.Coordinate.Longitude
        });
        myMap.Children.Remove(myCircle);
        myCircle = new Ellipse();
        myCircle.Fill = new SolidColorBrush(Colors.Green);
        myCircle.Height = 17;
        myCircle.Width = 17;
        myCircle.Opacity = 50;
        myMap.Children.Add(myCircle);
        MapControl.SetLocation(myCircle, location.Coordinate.Point);
        MapControl.SetNormalizedAnchorPoint(myCircle, new Point(0, 0));
        routeKM += (Distance(p, prev_location));
        Distance_txt.Text = routeKM.ToString("f2") + " km";
        prev_location = p;                    
    }
    catch
    {        
    }   
}

这是我用Haversine公式计算的double (routeKM):

public double Distance(Position pos1, Position pos2)
{
    var R = 6371d; // Radius of the earth in km
    var dLat = Deg2Rad(pos2.Latitude - pos1.Latitude);  // deg2rad below
    var dLon = Deg2Rad(pos2.Longitude - pos1.Longitude);
    var a = Math.Sin(dLat / 2d) * Math.Sin(dLat / 2d) +
            Math.Cos(Deg2Rad(pos1.Latitude)) * Math.Cos(Deg2Rad(pos2.Latitude)) *
            Math.Sin(dLon / 2d) * Math.Sin(dLon / 2d);
    var c = 2d * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1d - a));
    var d = R * c; // Distance in km
    return d;
}
double Deg2Rad(double deg)
{
    return deg * (Math.PI / 180d);
}

所以我的问题是:如何提高准确性?我不高兴我的车的计数器显示甚至比我的应用程序少2公里。

Windows Phone 8.1 -计算覆盖距离的问题

通过做DesiredAccuracy = PositionAccuracy.High,你已经建议Windows Phone OS寻找最高的准确性。

我认为你应该改变你的距离查找逻辑,看看它是否有效。

按照以下答案的建议使用GeoCoordinate.GetDistanceTo

https://stackoverflow.com/a/6366657/744616