1

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.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
Wowa
  • 1,791
  • 1
  • 14
  • 24
  • I'm going from memory here (it has been a couple of years since I used the MapPoint API), but I *believe* you can suppress the directions in the API, and that doing so makes it significantly faster. You should also ensure you are re-using the mappoint application instance and not spinning it up each time - that is slow. – Marc Gravell Feb 01 '11 at 12:17
  • `@Marc Gravell:` The application is only starting one time, and my stopwatch only mesureas the Calculte time. Do you mean the Directionspane? – Wowa Feb 01 '11 at 12:45

1 Answers1

1

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)

winwaed
  • 7,645
  • 6
  • 36
  • 81
  • 1
    sorry... it only took to 2sec because i forget to change shortest way to fastest way, now it again takes 16-18sec. Now i even remove/hide all placecategories, shapes und mapfeatures. – Wowa Feb 02 '11 at 09:49
  • No it doesn't have traffic management, real time or otherwise. I hide the entire app for speed. Then it doesn't bother rendering the route line on the map (which also includes a map view change as it zooms to the new route). – winwaed Feb 02 '11 at 13:35