我正在开发一个应用程序,它显示从用户当前位置到某个点的行驶距离。有几千个坐标点,应用程序需要非常快速地计算距离。以下是我正在使用的方法。
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 :(得分: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;
}
}