Microsoft KB Archive/302898

= HOW TO: Automate the MapPoint 2002 Control and Save the Map as HTML In Visual C# .NET =

Article ID: 302898

Article Last Modified on 6/29/2007

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft MapPoint 2002 Standard Edition

-



This article was previously published under Q302898





For a Microsoft Visual Basic .NET version of this article, see 302897.

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

For a Microsoft Visual C++ .NET version of this article, see 302900.

IN THIS TASK
SUMMARY The MapPoint Control Steps to Build the Sample
 * Import the MapPoint Type Library
 * Create the Sample Project
 * Test the Sample

REFERENCES



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

back to the top

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 C# .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

Steps to Build the Sample
The MapPoint 9 Control installs its own type library and a version of the MapPoint 2002 type library, but not all of the latter. For this example, you need the complete MapPoint 2002 type library as well as the partial type library that ships with the control to build an instance of the Application object. The type library for MapPoint 2002 has a namespace attribute that conflicts with the namespace of the version that is supplied (imported) by the control; when you import the MapPoint 2002 type library, you can use a different namespace to avoid this conflict.

back to the top

Import the MapPoint Type Library
 From the Programs list on the Start menu, click Microsoft Visual Studio .NET, click Visual Studio .NET Tools, and then click Visual Studio .NET CommandPrompt.  In the command prompt window, type the following: tlbimp.exe C:\Program Files\Microsoft Mappoint\mpna81.tlb /namespace:MapPointApp /out:C:\MapPointApp.dll NOTE: The default folder for Mpna81.tlb is C:\Program Files\Microsoft Mappoint. 

back to the top

Create the Sample Project
 Start Microsoft Visual Studio .NET. On the File menu, click New and then click Project. Under Project types click Visual C# 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 Project menu, click Add Reference. In the Add Reference dialog box, click Browse. Select C:\MapPointApp.dll and then click Open. Click OK to dismiss the Add Reference dialog box.</li> On the Tools menu, click Customize Toolbox.</li> In the list of COM components, click Microsoft MappointControl 9 and then click OK.</li> Press CTRL+ALT+X to display the Toolbox.</li> Draw a MapPoint control and three Button controls on Form1.

NOTE: The MapPoint control is added to the bottom of the toolbox and is identified by a MapPoint pushpin icon and the text &quot;Control&quot;.</li> Set the Text properties of Button1, Button2, and Button3 to Make Route Map, Save Map As HTML, and Close the Project, respectively.</li> Add event handlers for the form Load event and the Click events of the buttons. To do this, follow these steps:  In Design view for Form1.cs, double-click Form1. The event handler is created and displayed in the Form1.cs code window.</li> On the View menu, click Designer to switch to Design view.</li> Repeat steps a and b for each of the three button controls.</li></ol> </li>  In Form1.cs, replace the following code private void Form1_Load(object sender, System.EventArgs e) { }

private void button1_Click(object sender, System.EventArgs e) { }

private void button2_Click(object sender, System.EventArgs e) { }

private void button3_Click(object sender, System.EventArgs e) { } with: private MapPoint.Map oMap; private MapPointApp.Application oApp; private MapPoint.Route oRoute; private MapPoint.Waypoints oWaypoints; private MapPoint.FindResults oResults; private MapPoint.Waypoint StartPoint; private MapPoint.Waypoint MidPoint1; private MapPoint.Waypoint MidPoint2; private MapPoint.Waypoint EndPoint; private IEnumerator item; //System.Collections.IEnumerator

private void Form1_Load(object sender, System.EventArgs e) { string objTemplate = &quot;C:\\Program Files\\Microsoft MapPoint\\Templates\\&quot; + &quot;New North American Map.ptt&quot;; axMappointControl1.NewMap(objTemplate); }

private void button1_Click(object sender, System.EventArgs e) { 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) {       item = oResults.GetEnumerator; item.Reset; item.MoveNext; StartPoint = oWaypoints.Add(item.Current,&quot;Loc1&quot;); }

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) {       item = oResults.GetEnumerator; item.Reset; item.MoveNext; MidPoint1 = oWaypoints.Add(item.Current,&quot;Loc2&quot;); }   oRoute.Calculate; // This is leg 1. Double dLeg1; dLeg1 = oRoute.DrivingTime;

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) {       item = oResults.GetEnumerator; item.Reset; item.MoveNext; MidPoint2 = oWaypoints.Add(item.Current ,&quot;Loc3&quot;); }              oRoute.Calculate; Double dElapsedTime1; dElapsedTime1 = oRoute.DrivingTime; Double dLeg2; dLeg2 = dElapsedTime1 - dLeg1; // Finished calculating 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) {       item = oResults.GetEnumerator; item.Reset; item.MoveNext; EndPoint = oWaypoints.Add(item.Current ,&quot;Loc4&quot;); }   oRoute.Calculate; Double dElapsedTime; Double dLeg3; dElapsedTime = oRoute.DrivingTime; dLeg3 = dElapsedTime - dElapsedTime1; // End trip back to EndPoint and leg 3.

System.Globalization.DateTimeFormatInfo test = new System.Globalization.DateTimeFormatInfo;

Double StopTime = 0; // Layover or delivery time. StopTime = 0.3 * MapPoint.GeoTimeConstants.geoOneHour; // Between 18 and 19 minutes. DateTime dtStartDate = System.DateTime.Parse(&quot;8:00:00 AM&quot;); StartPoint.PreferredDeparture = dtStartDate; DateTime dtMid1Date = dtStartDate.AddDays(dLeg1+StopTime); MidPoint1.PreferredDeparture = dtMid1Date;

DateTime dtMid2Date = dtMid1Date.AddDays(dLeg2+StopTime); MidPoint2.PreferredDeparture = dtMid2Date; axMappointControl1.SaveMapAs(&quot;C:\\ATestMap.ptm&quot;); axMappointControl1.ActiveMap.Saved = true; }

private void button2_Click(object sender, System.EventArgs e) { oApp = new MapPointApp.Application; if (System.IO.File.Exists(&quot;C:\\ATestMap.ptm&quot;)) {       oMap = (MapPoint.Map)oApp.OpenMap(&quot;C:\\ATestMap.ptm&quot;,false); oApp.ActiveMap.SaveAs(&quot;C:\\ATestMap.htm&quot;,MapPointApp.GeoSaveFormat.geoFormatHTMLMapAndDirections,false); } }

private void button3_Click(object sender, System.EventArgs e) { if (oMap != null) {       oMap.Saved = true; }   if (oApp != null) {       oApp.Quit; }   this.Dispose; } NOTE: If you did not install MapPoint to the default installation folder, modify the value of objTemplate in Form1_Load so that the path to the template matches your installation of MapPoint. </li></ol>

back to the top

Test the Sample

 * 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. On your hard drive, you now see the C:\Atestmap.htm file. Double-click the file to see your route map in your Web browser.
 * 4) Click Close the Project to dispose of Form1 and the map.

back to the top

<div class="references_section">