为什么MapPoint.Route.Calculate()比在应用程序本身中计算慢得多?
我刚尝试使用mappoint api通过代码进行多次路由。它工作得很好,但是一些路线需要极长才能计算(18.5秒)。如果我通过MapPoint本身计算相同的路线,则只需不到2秒。
在上面的例子中,我计算了德国乌尔姆和曼海姆之间的路线。
class CalculateTime : IDisposable
{
MapPoint.Application app;
MapPoint.Map map;
MapPoint.Route route;
public CalculateTime()
{
app = (MapPoint.Application)Activator.CreateInstance(Type.GetTypeFromProgID("mappoint.application"));
map = app.ActiveMap;
route = map.ActiveRoute;
}
public class Place
{
public string City { get; set; }
public string Street { get; set; }
public string Postal { get; set; }
public Place(string City = "", string Street = "", string Postal = "")
{
this.City = City;
this.Street = Street;
this.Postal = Postal;
}
}
public class Place
{
public string City { get; set; }
public string Street { get; set; }
public string Postal { get; set; }
public Place(string City = "", string Street = "", string Postal = "")
{
this.City = City;
this.Street = Street;
this.Postal = Postal;
}
}
public TimeSpan Calculate(Place From, Place To)
{
Stopwatch sw = new Stopwatch();
MapPoint.FindResults frFromCollection = map.FindAddressResults(City: From.City, Street: From.Street, PostalCode: From.Postal);
MapPoint.FindResults frToCollection = map.FindAddressResults(City: To.City, Street: To.Street, PostalCode: To.Postal);
object frFrom = frFromCollection[1];
object frTo = frToCollection[1];
route.Waypoints.Add(frFrom);
route.Waypoints.Add(frTo);
sw.Start();
route.Calculate();
TimeSpan time = new TimeSpan(0, (int)(route.DrivingTime * 24 * 60), 0);
route.Clear();
sw.Stop();
Marshal.ReleaseComObject(frFromCollection);
Marshal.ReleaseComObject(frToCollection);
Marshal.ReleaseComObject(frFrom);
Marshal.ReleaseComObject(frTo);
MessageBox.Show(sw.Elapsed.Seconds + "." + sw.ElapsedMilliseconds);
return time;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~CalculateTime()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
map.Saved = true;
Marshal.ReleaseComObject(route);
Marshal.ReleaseComObject(map);
app.Quit();
Marshal.ReleaseComObject(app);
}
}
}
我记得还有另一种方法来计算显示弹出窗口的路线(不是MapPoint.Route.Calculate()
,但我忘了它。
有人知道如何加快计算速度吗?
电贺
WOWA
编辑:
我刚试过这个:
通过代码添加航点,但通过MapPoint本身计算ROute。这也需要极长的时间。不知怎的,似乎Waypoint是问题,而不是Calculate
方法
编辑:
看起来MapPoint已经在后台计算了路线。
如果我在调用计算完成毫秒内完成之前等待几个秒。
没有找到相关结果
已邀请:
1 个回复
棠媳鳖