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

This article was previously published under Q302898
This article has been archived. It is offered "as is" and will no longer be updated.
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.


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

  1. 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.
  2. 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

  1. 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.
  2. In the New Project dialog box, type MappointControl for the project name and then click OK. Form1 is created by default.
  3. 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.
  4. On the Tools menu, click Customize Toolbox.
  5. In the list of COM components, click Microsoft MappointControl 9 and then click OK.
  6. Press CTRL+ALT+X to display the Toolbox.
  7. 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 "Control".
  8. Set the Text properties of Button1, Button2, and Button3 to Make Route Map, Save Map As HTML, and Close the Project, respectively.
  9. Add event handlers for the form Load event and the Click events of the buttons. To do this, follow these steps:
    1. In Design view for Form1.cs, double-click Form1. The event handler is created and displayed in the Form1.cs code window.
    2. On the View menu, click Designer to switch to Design view.
    3. Repeat steps a and b for each of the three button controls.
  10. 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){		} 					
    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.IEnumeratorprivate void Form1_Load(object sender, System.EventArgs e){	string objTemplate =             "C:\\Program Files\\Microsoft MapPoint\\Templates\\" +             "New North American Map.ptt";	axMappointControl1.NewMap(objTemplate);}private void button1_Click(object sender, System.EventArgs e){	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)	{		item = oResults.GetEnumerator();		item.Reset();		item.MoveNext();		StartPoint = oWaypoints.Add(item.Current,"Loc1"); 	}	oResults = oMap.FindAddressResults("11235 SE 6th Street", "Bellevue", "", "WA", "98004", 244);	if (oResults.Count >= 1)	{		item = oResults.GetEnumerator();		item.Reset();		item.MoveNext();		MidPoint1 = oWaypoints.Add(item.Current,"Loc2"); 	}	oRoute.Calculate(); // This is leg 1.           	Double dLeg1;	dLeg1 = oRoute.DrivingTime;	oResults = oMap.FindAddressResults("22011 SE 51st Street", "Issaquah", "", "WA", "98027", 244);	if (oResults.Count >= 1)	{		item = oResults.GetEnumerator();		item.Reset();		item.MoveNext();		MidPoint2 = oWaypoints.Add(item.Current ,"Loc3"); 	}				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("16011 N.E. 36th Way", "Redmond", "", "WA", "98052", 244);	if (oResults.Count >= 1)	{		item = oResults.GetEnumerator();		item.Reset();		item.MoveNext();		EndPoint = oWaypoints.Add(item.Current ,"Loc4"); 	}	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("8:00:00 AM");				StartPoint.PreferredDeparture = dtStartDate;	DateTime dtMid1Date = dtStartDate.AddDays(dLeg1+StopTime);	MidPoint1.PreferredDeparture = dtMid1Date;	DateTime dtMid2Date = dtMid1Date.AddDays(dLeg2+StopTime);	MidPoint2.PreferredDeparture = dtMid2Date;           	axMappointControl1.SaveMapAs("C:\\ATestMap.ptm");	axMappointControl1.ActiveMap.Saved = true;}private void button2_Click(object sender, System.EventArgs e){	oApp = new MapPointApp.Application();	if (System.IO.File.Exists("C:\\ATestMap.ptm"))	{		oMap = (MapPoint.Map)oApp.OpenMap("C:\\ATestMap.ptm",false);		oApp.ActiveMap.SaveAs("C:\\ATestMap.htm",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.
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
For more information, see the following Microsoft Developer Network (MSDN) Web site:
Microsoft Office Development with Visual Studio
(c) Microsoft Corporation 2001, All Rights Reserved. Contributions by Chris Jensen, Microsoft Corporation.

back to the top

Article ID: 302898 - Last Review: 10/24/2013 07:00:31 - Revision: 5.2

  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft MapPoint 2002 Standard Edition
  • kbnosurvey kbarchive kbhowtomaster KB302898