【问题标题】:What is the fastest way to calculate driving distance in wp8 c#?在 wp8 c# 中计算行驶距离的最快方法是什么?
【发布时间】:2014-09-11 04:40:07
【问题描述】:

我正在开发一个应用程序,它显示从用户当前位置到某个点的行驶距离。有几千个坐标点,应用程序需要快速计算距离。下面是我正在使用的方法。

public async Task<int> findRouteLength(System.Device.Location.GeoCoordinate currentPosition, System.Device.Location.GeoCoordinate businessPosition)
    {


        List<System.Device.Location.GeoCoordinate> routePositions = new List<System.Device.Location.GeoCoordinate>();
        routePositions.Add(currentPosition);
        routePositions.Add(businessPosition);
        RouteQuery query = new RouteQuery();
        query.TravelMode = TravelMode.Driving;
        query.Waypoints = routePositions;
        Route route = await query.GetRouteAsync();
        return route.LengthInMeters;

    }

但是,这个任务在一秒钟内只能计算不超过 5-6 个距离。在 windows phone 8 c# 中是否有更快的计算行驶距离的方法?

【问题讨论】:

标签: c# xaml windows-phone-8


【解决方案1】:

试试这个,应该会快很多

public double CalculateDistance(System.Device.Location.GeoCoordinate geo, System.Device.Location.GeoCoordinate geo2)
{
    //var R = 6371; // result in km
    var R = 6371000; // result in m
    var dLat = (geo2.Latitude - geo.Latitude).ToRad();
    var dLon = (geo2.Longitude - geo.Longitude).ToRad();
    var lat1 = geo.Latitude.ToRad();
    var lat2 = geo2.Latitude.ToRad();

    var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
            Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2);
    var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
    return R * c;
}

ToRad 扩展

static class Ext
{
    public static double ToRad(this double val)
    {
        return (Math.PI / 180) * val;
    }
}

【讨论】:

  • 即“如乌鸦飞”或直线。在大多数情况下,无法与行驶距离相提并论。
猜你喜欢
  • 1970-01-01
  • 2018-05-18
  • 1970-01-01
  • 1970-01-01
  • 2011-12-26
  • 2019-02-26
  • 2015-05-11
  • 1970-01-01
相关资源
最近更新 更多