简体   繁体   English

在wp8 c#中计算行驶距离最快的方法是什么?

[英]What is the fastest way to calculate driving distance in wp8 c#?

I am developing an application which shows driving distances to a certain points from current position of user. 我正在开发一个显示从用户当前位置到特定点的行驶距离的应用程序。 There are couple of thousands of coordinate points and the application needs to calculate the distance really fast. 有成千上万个坐标点,应用程序需要真正快速地计算距离。 Below is the method I'm using. 以下是我使用的方法。

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;

    }

However, this task can only calculate no more than 5-6 distances in one second. 但是,此任务在一秒钟内只能计算不超过5-6个距离。 Is there any faster way of calculating driving distances in windows phone 8 c# ? Windows Phone 8 C#中有没有更快的方法来计算行车距离?

Try this, it should be much faster 试试这个,它应该快得多

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 extension ToRad扩展

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM