Microsoft KB Archive/305200

= HOW TO: Automate MapPoint by Using Visual Basic. NET =

Article ID: 305200

Article Last Modified on 8/9/2004

-

APPLIES TO


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

-



This article was previously published under Q305200





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

IN THIS TASK
SUMMARY Sample MapPoint Automation Client
 * Download the Sample
 * Run the Sample

Create the Sample Supporting Files and Customization REFERENCES



SUMMARY
This step-by-step article demonstrates how to use MapPoint 2002 functionality from an Automation client that is developed with Visual Basic .NET. This article describes each step that is involved in building the Automation client. You may also download this sample.

back to the top

Sample MapPoint Automation Client
The sample code demonstrates how to add custom Pushpins and user-designed features that are mapped from a custom database. The sample includes custom Pushpins and an Excel workbook that contains geographic locations that define a custom power-line grid. The Pushpins and the workbook are used to illustrate mapping custom geographic features.

back to the top

Download the Sample
The following file is available for download from the Microsoft Download Center:

Mappoint2002demo.exe

http://download.microsoft.com/download/MapPoint2002/sample/1.0/WIN98MeXP /EN-US/Mappoint2002demo.exe

Release Date: January 23, 2002

For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file. The Mappoint2002demo.exe (or Mappoint2002demo.hqx) file contains the following files: Folder             File name                            File Size -- ---  - ..\MapPointDemo AssemblyInfo.vb                         5KB Form1.resx                              8KB Form1.vb                               22KB MapPointDemo.sln                        1KB MapPointDemo.suo                        7KB MapPointDemo.vbproj                     5KB MapPointDemo.vbproj.user                2KB Eula.txt

..\MapPointDemo\obj Interop.MapPoint.dll                  100KB

..\MapPointDemo\obj\Debug MapPointDemo.exe                       36KB MapPointDemo.MapPointDemoFrm.Resources  3KB MapPointDemo.pdb                       30KB

..\MapPointDemo\Samples Grid.xls                               15KB SampData.xls                           52KB SubStation.bmp                          4KB TransmissionTower.bmp                   4KB

..\MapPointDemo\Templates New North American Map.ptt              5KB back to the top

Run the Sample

 * 1) Extract the contents of Mappoint2002demo.exe to the C:\MapPointDemo folder.
 * 2) Open Mappointdemo.sln in Microsoft Visual Studio .NET.
 * 3) Press F5 to build and run the sample.

back to the top

Create the Sample
 Start 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. For the Project Name type C:\MapPointDemo, and for Location type C:\. Click OK. Form1 is created by default. Starting at the top of Form1 and working down, add 10 Button controls, 1 Label control, and a ListBox control (in that order). Resize the form as needed to accommodate the controls. Select the 10 buttons on the form and set the Size property to 200,24 . Add a reference to the MapPoint 9.0 object library. To do this, follow these steps:  On the Project menu, click Add Reference. On the COM tab, locate Microsoft MapPoint 9.0 Object Library and click Select.</li> Click OK in the Add References dialog box to accept your selections. If you receive a prompt to generate wrappers for the libraries you selected, click Yes.</li></ol> </li>  Double-click Form1 to view the code behind the form, and then replace the following code Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub with: Dim oApp As MapPoint.Application Dim WithEvents oMap As MapPoint.Map ' With events. Dim oDataMap As MapPoint.DataMap Dim oWaypoint1 As MapPoint.Waypoint Dim oWaypoint2 As MapPoint.Waypoint Dim oRoute As MapPoint.Route Dim oFindResults As MapPoint.FindResults Dim oLocation1 As MapPoint.Location Dim oLocation2 As MapPoint.Location Dim oLocation3 As MapPoint.Location Dim oPushPin1 As MapPoint.Pushpin Dim oPushPin2 As MapPoint.Pushpin Dim oPushPin3 As MapPoint.Pushpin Dim MapPath As String Dim sMePath As String Dim oShape1 As MapPoint.Shape Dim oShape2 As MapPoint.Shape Dim oSymbols As MapPoint.Symbols Dim oSymbol1 As MapPoint.Symbol Dim oSymbol2 As MapPoint.Symbol Dim dsGrid As MapPoint.DataSet Dim dsRs As MapPoint.Recordset Dim aGrid As Object ' Visual Basic .NET requires you to declare dimension count - therefore, 2 dimensions.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Button1.Text = &quot;First Location&quot; Button2.Text = &quot;Target Restaurants&quot; Button3.Text = &quot;Next Location&quot; Button4.Text = &quot;Wire Location 1 to Location 2&quot; Button5.Text = &quot;Extend Line to Latitude-Longitude&quot; Button6.Text = &quot;Get the Route&quot; Button7.Text = &quot;Hide the Itinerary&quot; Button8.Text = &quot;Map Custom Assets&quot; Button9.Text = &quot;Map Sales Data&quot; Button10.Text = &quot;Quit&quot; Button2.Enabled = False Button3.Enabled = False Button4.Enabled = False Button5.Enabled = False Button6.Enabled = False Button7.Enabled = False sMePath = &quot;C:\MapPointDemo&quot; ' Path to Symbols collection. MapPath = &quot;C:\MapPointDemo\Templates\New North American Map.ptt&quot; ' Customize this for your system. oApp = CreateObject(&quot;Mappoint.Application&quot;) oApp.Left = 10 oApp.Top = 10 oApp.Visible = True oApp.WindowState = MapPoint.GeoWindowState.geoWindowStateNormal oMap = oApp.NewMap(Template:=MapPath) End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button1.Click 'Map the first location - a bus stop at 3rd and Pine, in Seattle, WA.

' Initialize objects of the form. ListBox1.Items.Clear Label1.Text = &quot;&quot; Label1.Refresh ListBox1.Refresh Button2.Enabled = True Button3.Enabled = True Button8.Enabled = True

' Initialize the MapStyle in case you mapped sales by county before ' clicking here. oMap.MapStyle = MapPoint.GeoMapStyle.geoMapStyleRoad oFindResults = oMap.FindAddressResults(&quot;3rd Ave & Pine St&quot;, &quot;Seattle&quot;,, _          &quot;Wa&quot;, 98101, MapPoint.GeoCountry.geoCountryUnitedStates)      '.Item(1) oLocation1 = oFindResults(1) oLocation1.GoTo oPushPin1 = oMap.AddPushpin(AtLocation:=oLocation1, Name:=&quot;Downtown Seattle&quot;) oPushPin1.BalloonState = MapPoint.GeoBalloonState.geoDisplayBalloon End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button2.Click ' Show the Chinese, steak, and seafood restaurants within 0.2 miles ' of the bus stop. ListBox1.Items.Clear Label1.Text = &quot;&quot; Label1.Refresh ListBox1.Refresh oPushPin1.Symbol = 180 ' Standard Bus Symbol. oPushPin1.Note = &quot;Bus Stop&quot; oPushPin1.BalloonState = MapPoint.GeoBalloonState.geoDisplayName oLocation1.FindNearby(Distance:=0.2) oMap.PlaceCategories.Visible = MapPoint.GeoTriState.geoFalse ' Hide them all at first. Dim i As Integer ListBox1.Items.Add(&quot;Built-in Place Categories&quot;) ' Title for the list to be inserted. For i = 1 To oMap.PlaceCategories.Count ListBox1.Items.Add(&quot;   &quot; & oMap.PlaceCategories(i).Name) ' Show list of built-in place categories. If oMap.PlaceCategories(i).Name = &quot;Restaurants - Chinese&quot; _ Or oMap.PlaceCategories(i).Name = &quot;Restaurants - Steak&quot; _ Or oMap.PlaceCategories(i).Name = &quot;Restaurants - Seafood&quot; Then oMap.PlaceCategories(i).Visible = True ' Show just these three categories. End If       Next i        oFindResults = oLocation1.FindNearby(Distance:=0.2) ' Count the visible ones. Label1.Text = &quot;Count of target restaurants is &quot; & oFindResults.Count - 1 ' Omit the bus stop. Label1.Show End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button3.Click ' Locate a picturesque waterfront location in Kirkland, WA. Label1.Text = &quot;&quot; ListBox1.Items.Clear Label1.Refresh ListBox1.Refresh oMap.MapStyle = MapPoint.GeoMapStyle.geoMapStyleRoad Button4.Enabled = True oPushPin1.BalloonState = MapPoint.GeoBalloonState.geoDisplayName oFindResults = oMap.FindAddressResults(&quot;Central Way & Lake Ave W&quot;, &quot;Kirkland&quot;,, _            &quot;Wa&quot;, 98033, MapPoint.GeoCountry.geoCountryUnitedStates) '.Item(1) oLocation2 = oFindResults(1) oLocation2.GoTo oPushPin2 = oMap.AddPushpin(AtLocation:=oLocation2, Name:=&quot;Favorite Corner&quot;) oPushPin2.BalloonState = MapPoint.GeoBalloonState.geoDisplayBalloon End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button4.Click ' Stretch a transmission line from Kirkland to the bus stop at       ' 3rd and Pine in downtown Seattle. ' Use a custom Pushpin icon for Transmission towers. Label1.Text = &quot;&quot; ListBox1.Items.Clear Label1.Refresh ListBox1.Refresh oMap.MapStyle = MapPoint.GeoMapStyle.geoMapStyleRoad oMap.Altitude = 25 oSymbols = oMap.Symbols

If oSymbols.Count <= 256 Then ' Do not create these more than once. oSymbol1 = oSymbols.Add(sMePath & &quot;\Samples\TransmissionTower.bmp&quot;) oSymbol1.Name = &quot;TransmissionTower&quot; oSymbol2 = oSymbols.Add(sMePath & &quot;\Samples\SubStation.bmp&quot;) oSymbol2.Name = &quot;SubStation&quot; End If

oPushPin2.BalloonState = MapPoint.GeoBalloonState.geoDisplayNone oShape1 = oMap.Shapes.AddLine(BeginLocation:=oLocation1, EndLocation:=oLocation2) oShape1.Line.Weight = 1 ' Pixel oShape1.Line.ForeColor = RGB(255, 64, 0) oPushPin1.Symbol = oSymbol1.ID       ' Custom Symbol for Transmission Tower oPushPin1.Name = &quot;Grid Start&quot; oPushPin2.Symbol = oSymbol1.ID       Button5.Enabled = True End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button5.Click ' Extend the line to a latitude-longitude location in Redmond, WA. ' Use another custom Pushpin icon for &quot;Substation&quot;. Label1.Text = &quot;&quot; ListBox1.Items.Clear Label1.Refresh ListBox1.Refresh oMap.MapStyle = MapPoint.GeoMapStyle.geoMapStyleRoad oLocation3 = oMap.GetLocation(47.63862, -122.13107, 500) oPushPin3 = oMap.AddPushpin(AtLocation:=oLocation3, Name:=&quot;Substation 345&quot;) oPushPin3.BalloonState = MapPoint.GeoBalloonState.geoDisplayName oPushPin3.Symbol = oSymbol2.ID       ' Custom Symbol for Substation oShape2 = oMap.Shapes.AddLine(BeginLocation:=oLocation2, EndLocation:=oLocation3) oShape2.Line.Weight = 1 oShape2.Line.ForeColor = RGB(255, 64, 0) oPushPin3.Highlight = True oMap.Altitude = 30 Button6.Enabled = True End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button6.Click ' Map a driving route and itinerary from Seattle to Redmond. Label1.Text = &quot;&quot; ListBox1.Items.Clear Label1.Refresh ListBox1.Refresh oMap.MapStyle = MapPoint.GeoMapStyle.geoMapStyleRoad oRoute = oMap.ActiveRoute oWaypoint1 = oRoute.Waypoints.Add(Anchor:=oLocation1) oWaypoint2 = oRoute.Waypoints.Add(Anchor:=oLocation3) oRoute.Calculate Button7.Enabled = True End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button7.Click ' Remove the itinerary from the screen. Label1.Text = &quot;&quot; ListBox1.Items.Clear Label1.Refresh ListBox1.Refresh oMap.MapStyle = MapPoint.GeoMapStyle.geoMapStyleRoad oApp.ItineraryVisible = False End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button8.Click ' Map a distribution network from your own records (an Excel worksheet). Label1.Text = &quot;&quot; ListBox1.Items.Clear Label1.Refresh ListBox1.Refresh oMap.MapStyle = MapPoint.GeoMapStyle.geoMapStyleRoad oSymbols = oMap.Symbols oSymbol1 = oSymbols.Add(sMePath & &quot;\Samples\TransmissionTower.bmp&quot;) oSymbol1.Name = &quot;Trans.Tower&quot; oSymbol2 = oSymbols.Add(sMePath & &quot;\Samples\SubStation.bmp&quot;) oSymbol2.Name = &quot;SubStn&quot; Dim MKGrid As String MKGrid = sMePath & &quot;\Samples\Grid.xls!Sheet1!A1:E100&quot; ' More rows than needed ReDim aGrid(4, 1) 'Values are UBounds, not element count. aGrid(0, 0) = &quot;ID&quot; aGrid(0, 1) = MapPoint.GeoFieldType.geoFieldData aGrid(1, 0) = &quot;Structure Name&quot; aGrid(1, 1) = MapPoint.GeoFieldType.geoFieldName aGrid(2, 0) = &quot;Type&quot; aGrid(2, 1) = MapPoint.GeoFieldType.geoFieldData aGrid(3, 0) = &quot;Latitude&quot; aGrid(3, 1) = MapPoint.GeoFieldType.geoFieldLatitude aGrid(4, 0) = &quot;Longitude&quot; aGrid(4, 1) = MapPoint.GeoFieldType.geoFieldLongitude dsGrid = oMap.DataSets.ImportData(DataSourceMoniker:=MKGrid, ImportFlags:= _          MapPoint.GeoImportFlags.geoImportExcelA1 Or _           MapPoint.GeoImportFlags.geoImportFirstRowIsHeadings, _           ArrayOfFields:=aGrid) dsGrid.ZoomTo Dim iRecordCount As Integer iRecordCount = dsGrid.RecordCount Dim PinArray As Object ReDim PinArray(dsGrid.RecordCount - 1, 4) ' UBound =(records in the dataset-1)

Dim iElement As Integer ' Index to rows in PinArray.

dsRs = dsGrid.QueryAllRecords ' Build a recordset so that you can look at the records of the ' dataset. dsRs.MoveFirst

Do While Not dsRs.EOF ' Sequence of records in dsRs is arbitrary, so resequence by ID. iElement = dsRs.Fields(1).Value 'ID of record in dsRs. PinArray(iElement - 1, 0) = iElement 'Save in array as ID. PinArray(iElement - 1, 1) = dsRs.Fields(2).Value 'Structure name. PinArray(iElement - 1, 2) = dsRs.Fields(3).Value 'Type. PinArray(iElement - 1, 3) = dsRs.Fields(4).Value 'Latitude. PinArray(iElement - 1, 4) = dsRs.Fields(5).Value 'Longitude. dsRs.MoveNext Loop dsGrid.Delete ' Remove its Pushpins from the map. Dim i As Integer For i = 1 To iRecordCount - 1 ' Move through records in original order. oLocation2 = oMap.GetLocation(PinArray(i - 1, 3), PinArray(i - 1, 4)) oLocation3 = oMap.GetLocation(PinArray(i, 3), PinArray(i, 4)) If PinArray(i - 1, 2) = 1 Then oPushPin2 = oMap.AddPushpin(AtLocation:=oLocation2, Name:=                  PinArray(i - 1, 1)) oPushPin2.Symbol = oSymbol2.ID               oPushPin2.Highlight = True Else oPushPin2 = oMap.AddPushpin(AtLocation:=oLocation2, Name:= _                  PinArray(i - 1, 1)) oPushPin2.Symbol = oSymbol1.ID           End If            oShape2 = oMap.Shapes.AddLine(BeginLocation:=oLocation2, EndLocation:= _               oLocation3) oShape2.Line.Weight = 1 oShape2.Line.ForeColor = RGB(255, 64, 0) Next i       If PinArray(i - 1, 2) = 1 Then oPushPin3 = oMap.AddPushpin(AtLocation:=oLocation3, Name:= _              PinArray(i - 1, 1)) oPushPin3.Symbol = oSymbol2.ID           ' Substation custom Pushpin. oPushPin3.Highlight = True Else oPushPin3 = oMap.AddPushpin(AtLocation:=oLocation3, Name:= _              PinArray(i - 1, 1)) oPushPin3.Symbol = oSymbol1.ID           ' Transmission Tower custom Pushpin. End If   End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button9.Click ' Map sales data for counties in Massachusetts, showing sized ' circles. ListBox1.Items.Clear Label1.Text = &quot;&quot; Label1.Refresh ListBox1.Refresh Button2.Enabled = False Button3.Enabled = False Button4.Enabled = False Button5.Enabled = False Button6.Enabled = False Button7.Enabled = False Button8.Enabled = False oMap.MapStyle = MapPoint.GeoMapStyle.geoMapStyleData Dim oField As MapPoint.Field dsGrid = oApp.ActiveMap.DataSets.ImportData(DataSourceMoniker:= _   &quot;C:\MapPointDemo\Samples\SampData.xls!Massachusetts Sales!A1:H15&quot;) dsGrid.ZoomTo oField = dsGrid.Fields(&quot;2000 ($)&quot;) ' Chosen column of worksheet. oDataMap = dsGrid.DisplayDataMap(DataMapType:= _           MapPoint.GeoDataMapType.geoDataMapTypeSizedCircle, _            DataField:=oField, _            ShowDataBy:=MapPoint.GeoShowDataBy.geoShowByDefault, _            DataRangeCount:=5, _ DataRangeType:= _            MapPoint.GeoDataRangeType.geoRangeTypeDiscreteEqualRanges, _            DataRangeOrder:= _            MapPoint.GeoDataRangeOrder.geoRangeOrderDefault, _            ColorScheme:=MapPoint.GeoDataConstants.geoColorSchemeDefault) oDataMap.LegendTitle = &quot;Sales By County - Year 2000&quot; Button9.Enabled = False End Sub

Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button10.Click Form_Unload(0) Me.Close End Sub

Private Sub Form_Unload(ByVal Cancel As Integer) If Not (oMap Is Nothing) Then oMap.Saved = True ' Forestall the &quot;Save Changes?&quot; dialog box. oMap = Nothing CType(oApp, MapPoint._Application).Quit ' Cast to base type. ' Resolves ambiguity between Application.Quit and ' Application_Events.Quit. oApp = Nothing End If   End Sub

Private Sub oMap_SelectionChange(ByVal NewSelection As Object,      ByVal OldSelection As Object) Handles oMap.SelectionChange Label1.Text = &quot;Handling An Event!&quot; ListBox1.Items.Clear Label1.Refresh ListBox1.Refresh If TypeName(NewSelection) = &quot;Pushpin&quot; Then oPushPin1 = NewSelection ListBox1.Items.Add(&quot;You selected &quot; & oPushPin1.Name) oPushPin1 = Nothing ElseIf TypeName(NewSelection) = &quot;Shape&quot; Then ListBox1.Items.Add(&quot;Shape&quot;) Else ListBox1.Items.Add(&quot;You selected a &quot; & TypeName(NewSelection)) End If       ListBox1.Items.Add(&quot;Old Selection is a &quot; & TypeName(OldSelection)) End Sub </li> Press F5 to build and run the sample.</li></ol>

back to the top

Supporting Files and Customization
The download contains several supporting files that the sample program uses: a map template (New North American Map.ptt), custom Pushpin bitmaps (Substation.bmp and Transmissiontower.bmp), and Excel workbooks (Sampdata.xls and Grid.xls).

Sampdata.xls is a sample workbook that is included with MapPoint 2002; the workbook contains geographical information for a list of hypothetical companies. MapPointDemo uses the data in this workbook to produce a map that shows sales volume by location.

Grid.xls is a custom workbook that represents one way to provide external data to a MapPoint session. MapPointDemo uses Grid.xls as the data source for the location of elements of the electric power grid. The data denotes the type of asset (transmission tower or substation) and its location in terms of latitude and longitude. External data can come from a variety of sources, including Excel workbooks, Access MDB tables, Microsoft SQL Server databases, or directly from a Global Position Sensor.

Grid.xls contains the following information:
 * ID (numeric; sequential numbers from 1 to 8)
 * Structure name (character data)
 * Type (numeric; used to denote which Pushpin to apply)
 * Latitude
 * Longitude

You can obtain actual values for Latitude and Longitude by using the Location Sensor tool that is found on the MapPoint Tools menu. Note that longitudes west of the Greenwich Prime Meridian are negative.

You can create your own custom Pushpin bitmaps by using Microsoft Paint. The size for Pushpin bitmaps should be set to 32x32 pixels.

back to the top

<div class="references_section">