HOW TO: Automate the MapPoint 2002 Control and Save the Map as HTML in Visual Basic .NET (302897)
The information in this article applies to:
- Microsoft Visual Basic .NET (2002)
- Microsoft MapPoint 2002
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. 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.
- Add a MapPoint control to Form1 and size the control so
that it fills most of the form.
- 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 = "C:\New North American Map.ptt"
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("16011 N.E. 36th Way", "Redmond", "", "WA", "98052", 244)
If oResults.Count >= 1 Then
StartPoint = oWaypoints.Add(oResults.Item(1), "Loc1")
End If
oResults = oMap.FindAddressResults("11235 SE 6th Street", "Bellevue", "", "WA", "98004", 244)
If oResults.Count >= 1 Then
MidPoint1 = oWaypoints.Add(oResults.Item(1), "Loc2")
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("22011 SE 51st Street", "Issaquah", "", "WA", "98027", 244)
If oResults.Count >= 1 Then
MidPoint2 = oWaypoints.Add(oResults.Item(1), "Loc3")
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("16011 N.E. 36th Way", "Redmond", "", "WA", "98052", 244)
If oResults.Count >= 1 Then
EndPoint = oWaypoints.Add(oResults.Item(1), "Loc1")
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 = "8:00:00 AM"
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:="C:\Atestmap.ptm")
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("MapPoint.Application") 'Legacy approach.
If System.IO.File.Exists("C:\Atestmap.ptm") Then
oMap = oApp.OpenMap("C:\Atestmap.ptm")
oApp.ActiveMap.SaveAs("C:\Atestmap.htm", 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.
back to the top
Test the Code- Press F5 to build and run the program. Form1 loads with a
map of North America.
- 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.
- Click Save Map as HTML. The Atestmap.ptm map file is opened in MapPoint and saved as
C:\Atestmap.htm.
- Start your Web browser and browse to C:\Atestmap.htm to
view the map.
- Click Close the Project to close Form1 and end the program.
back to the top
REFERENCES For more information, see the following Microsoft Developer
Network (MSDN) Web site: For
additional information, click the article number below to view the article in
the Microsoft Knowledge Base: 302885 HOWTO: Use the MapPoint 2002 Control and Automation With Visual Basic to Save a Map as HTML
back to the top
(c) Microsoft Corporation 2001, All Rights
Reserved. Contributions by Chris Jensen, Microsoft Corporation.
Modification Type: | Major | Last Reviewed: | 1/19/2006 |
---|
Keywords: | kbAutomation kbHOWTOmaster KB302897 kbAudDeveloper |
---|
|