I have a chunk of code that I wrote to find the driving distance between two addresses. Excuse the name "DoWork()" as it is just my naming for when I am trying things out.
Question 1 : In general am I going about it the correct way? Meaning adding waypoints to a route, calculating it, then looking at the distance.
Question 2 : I am using the data type of Object to eventually get to a Location. See this snippet.
Code:
Dim objLoc1 As MapPoint.Location
Dim objPin1OrLoc1 As Object
....
objPin1OrLoc1 = objMap.FindResults("1 Church St, Belfast ME 04915").Item(1)
....
objLoc1 = DirectCast(objPin1OrLoc1, MapPoint.Location)
Is this the preferred way to get a result from FindResult and feed it to a waypoint?
Here is the complete sub. It should be good to just copy and paste into a project if you already have a reference to Mappoint.
Code:
Private Sub DoWork1()
Dim objMapPointApp As New MapPoint.Application
Dim objMap As MapPoint.Map
Dim objLoc1 As MapPoint.Location
Dim objLoc2 As MapPoint.Location
Dim objPin1OrLoc1 As Object
Dim objPin2OrLoc2 As Object
Dim objRoute As MapPoint.Route
' Prepare Map and Route with defaults
objMap = objMapPointApp.ActiveMap
objRoute = objMap.ActiveRoute
' Get the first result from potential addresses
objPin1OrLoc1 = objMap.FindResults("1 Church St, Belfast ME 04915").Item(1)
objPin2OrLoc2 = objMap.FindResults("100 W Main St, Troy, OH 45373").Item(1)
' If both addresses survived the process
If (Not objPin1OrLoc1 Is Nothing) And (Not objPin2OrLoc2 Is Nothing) Then
' Cast objects to Mappoint.Location
objLoc1 = DirectCast(objPin1OrLoc1, MapPoint.Location)
objLoc2 = DirectCast(objPin2OrLoc2, MapPoint.Location)
' Add locations to waypoint
With objRoute
.Waypoints.Add(objLoc1)
.Waypoints.Add(objLoc2)
.Calculate()
End With
MessageBox.Show(objRoute.Distance.ToString)
End If
' **********************
' Clean up afterwards
' **********************
' Avoid dialog box asking to save
objMapPointApp.ActiveMap.Saved = True
objMapPointApp.Quit()
objMapPointApp = Nothing
End Sub
[/code]