Microsoft KB Archive/302897

= HOW TO: Automate the MapPoint 2002 Control and Save the Map as HTML in Visual Basic .NET =

Article ID: 302897

Article Last Modified on 6/29/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft MapPoint 2002 Standard Edition

-



This article was previously published under Q302897



For a Microsoft C# version of this article, see 302898.



For a Microsoft Visual Basic 6.0 version of this article, see 302885.



IN THIS TASK
SUMMARY
 * Discussion of the MapPoint Control
 * Create the Project
 * Test the Code

REFERENCES



SUMMARY
This step-by-step article describes how to automate the MapPoint 2002 control and save the map as HTML in Visual Basic .NET.

back to the top

Discussion of the MapPoint Control
The Microsoft MapPoint Control 9.0 is an ActiveX control that is included with Microsoft MapPoint 2002. This control provides a convenient way to implement MapPoint 2002 functionality on a form in a Visual Basic .NET project. By using this control, you can access most, but not all, MapPoint 2002 functionality. For example, you cannot programmatically save a map in the HTML format with the ActiveX control, but you can save the map as a MapPoint .ptm file by using the control, and then programmatically open the .ptm file in MapPoint and save it as HTML. This article contains sample code that demonstrates this solution.

back to the top

Create the Project
 Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Under Project types click Visual Basic Projects, then click Windows Application under Templates. In the New Project dialog box, type MappointControl for the project name and then click OK. Form1 is created by default. On the Visual Basic menu, click View, and then click Toolbox. On the Tools menu, click Customize Toolbox. In the Customize Toolbox dialog box, select Microsoft MapPoint Control 9.0 from the list of COM Components and click OK. The control will be added to the Toolbox towards the end of the list of Windows Form controls; you can identify the control by the MapPoint pushpin icon. Add the following controls to Form1:  Add three Button controls to the right of Form1. Change the Text properties for Button1, Button2 and Button3 to Make Route Map, Save Map as HTML, and Close the Project, respectively.</li> Add a MapPoint control to Form1 and size the control so that it fills most of the form.</li></ul> </li>  Double-click Form1 to display the code for the Load event handler of the form. Replace the following code Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub with: Public oApp As MapPoint.Application Public oMap As MapPoint.Map Public oRoute As MapPoint.Route Public oWaypoints As MapPoint.Waypoints Public oResults As MapPoint.FindResults Public StartPoint As MapPoint.Waypoint Public MidPoint1 As MapPoint.Waypoint Public MidPoint2 As MapPoint.Waypoint Public EndPoint As MapPoint.Waypoint

Public objTemplate As String ' Object to hold the name of the map to load.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load objTemplate = &quot;C:\New North American Map.ptt&quot; AxMappointControl1.NewMap(objTemplate) End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click oMap = AxMappointControl1.ActiveMap oRoute = oMap.ActiveRoute oWaypoints = oRoute.Waypoints

oResults = oMap.FindAddressResults(&quot;16011 N.E. 36th Way&quot;, &quot;Redmond&quot;, &quot;&quot;, &quot;WA&quot;, &quot;98052&quot;, 244) If oResults.Count >= 1 Then StartPoint = oWaypoints.Add(oResults.Item(1), &quot;Loc1&quot;) End If

oResults = oMap.FindAddressResults(&quot;11235 SE 6th Street&quot;, &quot;Bellevue&quot;, &quot;&quot;, &quot;WA&quot;, &quot;98004&quot;, 244) If oResults.Count >= 1 Then MidPoint1 = oWaypoints.Add(oResults.Item(1), &quot;Loc2&quot;) End If

oRoute.Calculate ' This is leg 1 (that is, the route to Midpoint1).

Dim dLeg1 As Double dLeg1 = oRoute.DrivingTime ' End Leg 1.

oResults = oMap.FindAddressResults(&quot;22011 SE 51st Street&quot;, &quot;Issaquah&quot;, &quot;&quot;, &quot;WA&quot;, &quot;98027&quot;, 244) If oResults.Count >= 1 Then MidPoint2 = oWaypoints.Add(oResults.Item(1), &quot;Loc3&quot;) End If

oRoute.Calculate ' Refigure the complete route to Midpoint2.

Dim dElapsedTime1 As Double dElapsedTime1 = oRoute.DrivingTime ' This is the total trip time so far. Dim dLeg2 As Double ' This is the time for this leg only. dLeg2 = dElapsedTime1 - dLeg1 ' This is the end trip time to this waypoint, and leg 2 drive time.

oResults = oMap.FindAddressResults(&quot;16011 N.E. 36th Way&quot;, &quot;Redmond&quot;, &quot;&quot;, &quot;WA&quot;, &quot;98052&quot;, 244) If oResults.Count >= 1 Then EndPoint = oWaypoints.Add(oResults.Item(1), &quot;Loc1&quot;) End If

oRoute.Calculate ' Refigure the complete route to the endpoint.

Dim dElapsedTime2 As Double Dim dLeg3 As Double ' This is the time for third leg only. dElapsedTime2 = oRoute.DrivingTime dLeg3 = dElapsedTime2 - dElapsedTime1 ' This is the end trip to third waypoint and leg 3.

Dim StartDate As Double Dim Mid1Date As Double Dim Mid2Date As Double Dim EndDate As Double

Dim StopTime As Double ' Calculate StopTime as 18 - 19 minutes. StopTime = 0.3 * MapPoint.GeoTimeConstants.geoOneHour

Dim oDateTime As DateTime oDateTime = &quot;8:00:00 AM&quot;

StartDate = oDateTime.ToOADate

StartPoint.PreferredDeparture = oDateTime.FromOADate(StartDate) Mid1Date = StartDate + dLeg1 + StopTime

MidPoint1.PreferredDeparture = oDateTime.FromOADate(Mid1Date) Mid2Date = Mid1Date + dLeg2 + StopTime MidPoint2.PreferredDeparture = oDateTime.FromOADate(Mid2Date)

AxMappointControl1.SaveMapAs(FileName:=&quot;C:\Atestmap.ptm&quot;) AxMappointControl1.ActiveMap.Saved = True End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click oApp = CreateObject(&quot;MapPoint.Application&quot;) 'Legacy approach. If System.IO.File.Exists(&quot;C:\Atestmap.ptm&quot;) Then oMap = oApp.OpenMap(&quot;C:\Atestmap.ptm&quot;) oApp.ActiveMap.SaveAs(&quot;C:\Atestmap.htm&quot;, MapPoint.GeoSaveFormat.geoFormatHTMLMapAndDirections) End If   End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click If Not oMap Is Nothing Then oMap.Saved = True End If       If Not oApp Is Nothing Then oApp.Quit End If       Me.Dispose(True) ' Unload the (Form1) application. End Sub NOTE: Modify the path to the C:\New North American Map.ptt template file in Form1_Load to reflect the location of that file on your system. The default location for this file is C:\Program Files\Microsoft Mappoint\Templates. </li></ol>

back to the top

Test the Code

 * 1) Press F5 to build and run the program. Form1 loads with a map of North America.
 * 2) Click Make Route Map. MapPoint draws a route map from a starting point, through a midpoint, through another midpoint, and back to the endpoint (which is the same as the starting point). The timed itinerary appears in a list box above the map. A copy of the displayed map is saved as C:\Atestmap.ptm.
 * 3) Click Save Map as HTML. The Atestmap.ptm map file is opened in MapPoint and saved as C:\Atestmap.htm.
 * 4) Start your Web browser and browse to C:\Atestmap.htm to view the map.
 * 5) Click Close the Project to close Form1 and end the program.

back to the top

<div class="references_section">