2014-09-10 104 views
1

我正在開發一個應用程序,顯示從用戶的當前位置到某個點的行駛距離。有幾千個座標點,應用程序需要快速計算距離。以下是我正在使用的方法。wp8 c#中計算駕駛距離的最快方法是什麼?

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#駕駛距離?

+0

http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm你最好減少*數千*用自己寫的智能代碼來獲得更好的性能。 – 2014-09-10 22:38:50

回答

0

試試這個,應該是更快

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; 
    } 
} 
+1

這就是「如烏鴉飛」或直線。大部分時間與駕駛距離無法比擬。 – CodeCaster 2014-09-10 21:00:33

相關問題