MapPoint Programming Challenge
Come up with the best solution and get recognition in the next newsletter as well as receive your choice of a Large MapPoint Mug or a AutoRoute 2004.
Some collections of stops cannot be Optimized. If this is being done programmatically, execution hangs.
A minimal example of a route that will not Optimize can be downloaded here:
The route will calculate fine, but when you try to optimize, it returns an error "Unable to get directions from ... move one or both of the stops, and try again".
Specifically, it is the route up on Hilton street that is preventing it from Optimizing, if you shift it even slightly it seems to be fine.
So working Interactively, this is not really a problem. But programmatically from Excel, I am having trouble finding a way to trap for this condition.
Stops can be tested and those that that are totally unroutable (stuck behind a one-way road or on a mini-road network not connected to the main road network) can be caught as an error is generated from this condition. You can then shift the individual stop's position until it works.
Optimize does not generate a trappable error in the same way, it just hangs.
Here's the same minimal route in Excel, the code is included below.
When you run it, you see MapPoint uses cpu for several minutes, then just gives up.. there is no error generated, it just hangs. (hit ctrl-alt-del and kill mappoint to get back to excel).
How is it possible to detect this hanging or have it trigger and error and continue program execution without user intervention?
Code:Private Sub CommandButton1_Click() 'This will prevent the waiting for ole application warning from popping up Application.DisplayAlerts = False Set oApp = CreateObject("MapPoint.Application.NA.11") oApp.Visible = True Set objMap = oApp.NewMap Set objRoute = objMap.ActiveRoute For i = 2 To 5 objRoute.WayPoints.Add objMap.GetLocation(Worksheets("Sheet1").Cells(i, 2), _ Worksheets("Sheet1").Cells(i, 3)), Worksheets("Sheet1").Cells(i, 1) Next Stop 'The route can be calculated i.e. all the stops are routable objRoute.Calculate Stop 'It is this Optimize that hangs and unfortunately doesn't generate an error trappable 'by Excel. The error only appears when working interactively with MapPoint. objRoute.WayPoints.Optimize 'Set back to true Application.DisplayAlerts = False End Sub