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
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
Run the Sample
- Extract the contents of Mappoint2002demo.exe to the C:\MapPointDemo folder.
- Open Mappointdemo.sln in Microsoft Visual Studio .NET.
- Press F5 to build and run the sample.
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.
- 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.
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 = "First Location" Button2.Text = "Target Restaurants" Button3.Text = "Next Location" Button4.Text = "Wire Location 1 to Location 2" Button5.Text = "Extend Line to Latitude-Longitude" Button6.Text = "Get the Route" Button7.Text = "Hide the Itinerary" Button8.Text = "Map Custom Assets" Button9.Text = "Map Sales Data" Button10.Text = "Quit" Button2.Enabled = False Button3.Enabled = False Button4.Enabled = False Button5.Enabled = False Button6.Enabled = False Button7.Enabled = False sMePath = "C:\MapPointDemo" ' Path to Symbols collection. MapPath = "C:\MapPointDemo\Templates\New North American Map.ptt" ' Customize this for your system. oApp = CreateObject("Mappoint.Application") 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 = "" 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("3rd Ave & Pine St", "Seattle", , _ "Wa", 98101, MapPoint.GeoCountry.geoCountryUnitedStates) '.Item(1) oLocation1 = oFindResults(1) oLocation1.GoTo() oPushPin1 = oMap.AddPushpin(AtLocation:=oLocation1, Name:="Downtown Seattle") 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 = "" Label1.Refresh() ListBox1.Refresh() oPushPin1.Symbol = 180 ' Standard Bus Symbol. oPushPin1.Note = "Bus Stop" 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("Built-in Place Categories") ' Title for the list to be inserted. For i = 1 To oMap.PlaceCategories.Count ListBox1.Items.Add(" " & oMap.PlaceCategories(i).Name) ' Show list of built-in place categories. If oMap.PlaceCategories(i).Name = "Restaurants - Chinese" _ Or oMap.PlaceCategories(i).Name = "Restaurants - Steak" _ Or oMap.PlaceCategories(i).Name = "Restaurants - Seafood" 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 = "Count of target restaurants is " & 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 = "" ListBox1.Items.Clear() Label1.Refresh() ListBox1.Refresh() oMap.MapStyle = MapPoint.GeoMapStyle.geoMapStyleRoad Button4.Enabled = True oPushPin1.BalloonState = MapPoint.GeoBalloonState.geoDisplayName oFindResults = oMap.FindAddressResults("Central Way & Lake Ave W", "Kirkland", , _ "Wa", 98033, MapPoint.GeoCountry.geoCountryUnitedStates) '.Item(1) oLocation2 = oFindResults(1) oLocation2.GoTo() oPushPin2 = oMap.AddPushpin(AtLocation:=oLocation2, Name:="Favorite Corner") 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 = "" 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 & "\Samples\TransmissionTower.bmp") oSymbol1.Name = "TransmissionTower" oSymbol2 = oSymbols.Add(sMePath & "\Samples\SubStation.bmp") oSymbol2.Name = "SubStation" 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 = "Grid Start" 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 "Substation". Label1.Text = "" 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:="Substation 345") 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 = "" 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 = "" 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 = "" ListBox1.Items.Clear() Label1.Refresh() ListBox1.Refresh() oMap.MapStyle = MapPoint.GeoMapStyle.geoMapStyleRoad oSymbols = oMap.Symbols oSymbol1 = oSymbols.Add(sMePath & "\Samples\TransmissionTower.bmp") oSymbol1.Name = "Trans.Tower" oSymbol2 = oSymbols.Add(sMePath & "\Samples\SubStation.bmp") oSymbol2.Name = "SubStn" Dim MKGrid As String MKGrid = sMePath & "\Samples\Grid.xls!Sheet1!A1:E100" ' More rows than needed ReDim aGrid(4, 1) 'Values are UBounds, not element count. aGrid(0, 0) = "ID" aGrid(0, 1) = MapPoint.GeoFieldType.geoFieldData aGrid(1, 0) = "Structure Name" aGrid(1, 1) = MapPoint.GeoFieldType.geoFieldName aGrid(2, 0) = "Type" aGrid(2, 1) = MapPoint.GeoFieldType.geoFieldData aGrid(3, 0) = "Latitude" aGrid(3, 1) = MapPoint.GeoFieldType.geoFieldLatitude aGrid(4, 0) = "Longitude" 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 = "" 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:= _ "C:\MapPointDemo\Samples\SampData.xls!Massachusetts Sales!A1:H15") dsGrid.ZoomTo() oField = dsGrid.Fields("2000 ($)") ' 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 = "Sales By County - Year 2000" 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 "Save Changes?" 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 = "Handling An Event!" ListBox1.Items.Clear() Label1.Refresh() ListBox1.Refresh() If TypeName(NewSelection) = "Pushpin" Then oPushPin1 = NewSelection ListBox1.Items.Add("You selected " & oPushPin1.Name) oPushPin1 = Nothing ElseIf TypeName(NewSelection) = "Shape" Then ListBox1.Items.Add("Shape") Else ListBox1.Items.Add("You selected a " & TypeName(NewSelection)) End If ListBox1.Items.Add("Old Selection is a " & TypeName(OldSelection)) End Sub
- Press F5 to build and run the sample.
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
REFERENCES
For additional information about Automating MapPoint, click the article numbers below to view the articles in the Microsoft Knowledge Base:
302885 HOWTO: Use the MapPoint 2002 Control and Automation With Visual Basic to Save a Map as HTML
302897 HOW TO: Automate the MapPoint 2002 Control and Save the Map as HTML in Visual Basic .NET
Keywords: kbhowtomaster kbdownload kbfile kbautomation KB305200