i just tried multiple routing via code with the mappoint api. It works all fine, but a few routes takes extrem long to calculate (18.5 sec). If i calculate the same route via MapPoint itself it only takes less than 2 sec.
In the example above i calculated the route between Ulm and Mannheim in Germany.
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);
}
}
}
I remember there was another methode to calculate the route which shows a popup (not MapPoint.Route.Calculate()
, but I forgot it.
Does anybody have an idea how to speedup the calculation?
Greets Wowa
EDIT:
I just tried this:
Adding the Waypoints via Code, but Calculate the ROute via MapPoint itself. this also takes extremly long. Somehow it seems the Waypoint are the problem, not the Calculate
methode
EDIT:
It looks like MapPoint already calculates the route in the background. If i wait a few secondes before calling Calculate its finished within millisecondes.
As well as spooling up the application each time, remember that the COM interface adds some overhead. Not much, but it is something to be aware of.
Also I've noticed that MapPoint 2010 takes a second or two to load - longer than earlier versions.
As Marc indicates, hiding much of the map display can result in roughly 30% speedups because MapPoint does not have to display the route.
Finally, route calculations vary a lot according to the waypoint locations, and road density/complexity. One route might take 0.5s to compute but a similar length route elsewhere takes 2-3 secs.
Finally, Finally :-) , many aspects of MapPoint are optimized for human users rather than API use. for example the garbage collection is definitely human-oriented, rather than batch API oriented in it's optimizations. (MapPoint will slow down with lots of batch API use as the garbage collector is not called often enough for this type of work)