Empezando Con Map Objet

Embed Size (px)

Citation preview

  • 8/3/2019 Empezando Con Map Objet

    1/26

    Getting Started with MapObjectsContents Introduction 5

    Loading MapObjects 6

    Getting help 6

    Adding a map 7

    Adding pan and zoom controls 9

    Adding a toolbar 11

    Creating a find tool 14

    Handling resize 16

    Displaying map layers based on scale 17

    Adding a spatial query tool 18

    Statistical mapping 20

    Event tracking 21

    Working with DataConnection objects 24

    Working with ImageLayer objects 25

    Congratulations 27

  • 8/3/2019 Empezando Con Map Objet

    2/26

    Getting StartedIN THIS BOOK

    Loading MapObjects into Visual

    Basic

    Adding simple display tools

    Creating basic spatial query

    tools

    Displaying map layers

    Tracking events

    Working with theDataConnection object

    Adding an image layer

    In this introductory document you will use MapObjects and Microsoft

    Visual Basic to build an application that uses maps. Along the way you

    will learn how to:Display a map with multiple layers.

    Control panning and zooming.

    Create a toolbar control.

    Display map layers based on scale.

    Perform spatial and logical queries.

    Draw simple graphics on the map.

    Display features with thematic renderers.

    Dynamically display real-time data with an event tracking layer.

    Programmatically add data to a map.

    Note If you accepted the defaults when installing MapObjects, thegeographic data that this tutorial refers to can be found in C:\ProgramFiles\ESRI\MapObjects2\Samples\Data\Usa. The bitmaps you can use arein the Samples\Bitmaps folder.

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 2

  • 8/3/2019 Empezando Con Map Objet

    3/26

  • 8/3/2019 Empezando Con Map Objet

    4/26

    Getting help

    The MapObjects online help system is integrated with theVisual Basic Object Browser.

    1. Click Symbol in the Classes/Modules list.

    2. Click Rotation in the Methods/Properties list.

    3. Click the help (question mark) button.

    The help system provides help for every object,property, method, event and constant inMapObjects. In addition to the Object Browser,the help system is accessible form the VisualBasic Code window. Simply type in the name of

    an object, property, method, event or constant,and press F1.

    Adding a mapThe Map control is the object you use to display maps.

    Add the Map control to the form

    1. Double-click the Map control button in the Toolbox to add a new map to

    the form.

    2. Resize the map to fill the form.

    Select the data to display on the map

    You can specify the data that is displayed in the map by setting propertiesin the Map controls property sheet.

    1. Right-click the mouse on the map to display the context menu.

    2. Choose Properties to display the property sheet.

    3. Click the Add button and locate the folder containing the States sampledata. If you selected the defaults when you installed MapObjects, this

    will be in c:\Program Files\ ESRI\MapObjects2\Samples\Data.

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 4

  • 8/3/2019 Empezando Con Map Objet

    5/26

    4. Click the States.shp file and then click Open.

    5. Add the file USHigh.shp in the same manner.

    Set properties for the layers

    1. Click the States layer in the Layers list and then click

    Properties.

    2. Click the Color button to select a color for the States

    layer.3. Click OK to close the dialog.

    4. Select a color for the USHigh layer in the same manner.

    5. Click OK to close the property sheet.

    Save the project1. Click the File menu and then click Save Project.

    2. Browse to a suitable folder, then in the File Name box type

    StarterMap.frm.

    3. Click Save.

    4. In the second Save dialog, type StarterMap.vbp in the File Name box.

    5. Click Save.

    Run

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 5

  • 8/3/2019 Empezando Con Map Objet

    6/26

    Test your application

    1. Click the Run button in the Visual Basic toolbar.

    Stop

    2. To stop running your application and return to design

    mode, click the Stop button in the Visual Basic toolbar.

    Adding pan and zoom controlsAt this point your application can display the map at its fullextent. In this section you will add some simple pan andzoom controls that your application will activate inresponse to mouse clicks inside the map. You will writesome code that the application will execute in response tothe MouseDown event on the map.

    Respond to the MouseDown event

    1. Double-click the map to display the Visual Basic Code

    window.

    2. Add code to Map1s MouseDown procedure.

    Private Sub Map1_MouseDown(Button As Integer, _Shift As Integer, x As Single, y As Single)

    Set Map1.Extent = Map1.TrackRectangle

    End Sub

    Test your change

    1. Click the Run button in the Visual Basic toolbar.

    2. Click the map with the left mouse button and drag out a rectangle.

    3. Release the mouse button and notice that the map is redrawn at the

    location you specified.

    TrackRectangle is a method that applies to a map. It tracks themovement of the mouse while the user presses the mousebutton, rubber-banding a rectangle at the same time. When theuser releases the mouse button, the TrackRectangle methodreturns a Rectangle object that the application assigns into theExtent property of the map, causing the map to be redrawnwith a new map extent.

    4. Click the stop button in Visual Basic to return to design mode.

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 6

  • 8/3/2019 Empezando Con Map Objet

    7/26

    Add panning

    1. Double-click the map to display the Visual Basic Code

    window again.

    2. Change the code for the MouseDown event.

    Private Sub Map1_MouseDown(Button As Integer,_Shift As Integer, x As Single, y As Single)

    If Button = vbLeftButton Then

    Set Map1.Extent = Map1.TrackRectangle

    Elseif Button = vbRightButton then

    Map1.Pan

    End If

    End Sub

    If the Button parameter is equal to vbLeftButton when theMouseDown event occurs, then the zooming code from the

    previous step will be executed. Otherwise, the code willcall another map method, Pan. If the user clicks the leftmouse button, the Button parameter will be vbLeftButton.If the user clicks the right mouse button, the value ofButton will be vbRightButton.

    Add a FullExtent button

    Your application now supports panning and zooming, butonce the user has zoomed into the map, there is no way toget back to the full extent again. In this section you willadd a button to the form that zooms the map to the fullextent.

    1. Double-click the CommandButton button in the Toolbox

    to add a button to the form.

    2. Move the button to the upper right of the form.

    3. Press F4 to display the Properties window.

    4. Click in the Caption box and type Full Extent to change the buttons

    caption.5. Resize the Map control so that it is not obscured by the button.

    6. Double-click the Full Extent button to display the Code window.

    7. Add code for the Click event.

    Private Sub Command1_Click()

    Set Map1.Extent = Map1.FullExtent

    End Sub

    The FullExtent property of the map returns a Rectangle object thatdefines the bounding box of all the layers of the map.

    Test your change

    1. Click the Run button in the Visual Basic toolbar.

    2. Click the map with the left mouse button and drag out a rectangle.

    3. Release the mouse button to redraw the map.

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 7

  • 8/3/2019 Empezando Con Map Objet

    8/26

    4. Click the map with the right mouse button and drag to

    pan the map.

    5. Release the mouse button to redraw the map.

    6. Click the Full Extent button to redraw the map at the full

    extent.

    Save the project

    1. Click the Stop button in the Visual Basic toolbar to return

    to design mode.

    2. Click the Save Project button in the Visual Basic toolbar

    to save your changes.

    Adding a toolbarYour applications pan and zoom capabilities are somewhathidden from the user. In this section you will create atoolbar with pan and zoom buttons.

    Adding a toolbar

    Visual Basic includes a toolbar control that can be used inconjunction with an ImageList control to display acollection of buttons at the top of a form.

    1. Delete the Full Extent button from the form.

    2. Double-click the Toolbox, and select MS Windows

    Common Controls. You will notice new tools are added

    to your Toolbox.

    3. Double-click the Toolbar button in the Toolbox to add a

    Toolbar control to the form.

    4. Double-click the ImageList button in the Toolbox to add

    an ImageList to the form.

    5. Resize the map so that it is not obscured by the toolbar.

    The ImageList control may obscure the map; however, this is not aproblem because the ImageList control will not be visible when yourapplication is running.

    Adding images to the ImageList control

    1. Right-click the ImageList control to display the context menu.

    2. Click Properties to display the property sheet.

    3. Click the Images tab.

    4. Click Insert Picture and locate the folder that contains the sample

    bitmaps.

    5. Click the Zoom.bmp file and then click Open.

    6. Add the files Pan.bmp, Globe.bmp, Bex.bmp, and Pennant.bmp in the

    same manner.

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 8

  • 8/3/2019 Empezando Con Map Objet

    9/26

    Set the MaskColor of the ImageListSetting the MaskColor property of an ImageList controlspecifies a color that will act as a mask for any imagescontained by the control. The mask color will not be drawn,resulting in an image with a transparent background.

    1. Click the Colors tab.

    2. In the Properties list, click the arrow and then click

    MaskColor.

    3. Click Dark Cyan in the Color Palette list.

    4. Click OK to dismiss the property sheet.

    Associate the ImageList with the toolbar

    You can associate the Toolbar control with an ImageList control toprovide the graphic images for the buttons.

    1. Right-click the Toolbar control to display the context menu.

    2. Click Properties to display the property sheet.

    3. In the ImageList box, click the arrow and then click ImageList1. This

    associates the toolbar with the ImageList control.

    Adding buttons to the Toolbar control

    In this section you will add five buttons and a separator to the toolbar.You will set the Style of two buttons to Placeholder. The Placeholderswill be used later in this document.

    1. In the Toolbar Control Properties dialog click the Buttons tab, then click

    Insert Button.

    2. Set the buttons style to Button Group, its Image to 1, and its Value to

    Pressed.

    3. Add a second button and set its Style to Button Group and its Image to2.

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 9

  • 8/3/2019 Empezando Con Map Objet

    10/26

    4. Add a third button and set its Style to Placeholder.

    5. Add a fourth button and set its Style to Placeholder.

    6. Add a fifth button and set its Style to Separator.

    7. Add a sixth button and set its Image to 3.

    8. Click OK to dismiss the property sheet.

    Change the MouseDown event

    1. Double-click the map to display the Visual Basic Code

    window.

    2. Modify the code attached to Map1s MouseDown

    procedure.

    Private Sub Map1_MouseDown(Button As Integer, _Shift As Integer, x As Single, y As Single)

    If Toolbar1.Buttons(1).Value = 1 Then

    Set Map1.Extent = Map1.TrackRectangle

    ElseIf Toolbar1.Buttons(2).Value = 1 ThenMap1.Pan

    End If

    End Sub

    Selecting the first button in the toolbar sets the mouse to be a zoom tool;selecting the second button lets you use the mouse to pan.

    Implement the FullExtent buttonIn this section you will reimplement the Full Extent button that youdeleted.

    1. Double-click the toolbar to display the Code window.

    2. Add code to Toolbar1s ButtonClick event.

    Private Sub Toolbar1_ButtonClick(ByVal Button _As MSComctLib.Button)

    If Button.Index = 6 ThenSet Map1.Extent = Map1.FullExtent

    End If

    End Sub

    The ButtonClick event is generated whenever a click occurs on a buttonin the toolbar. If the index of the button is 6, then the map is zoomed toits full extent.

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 10

  • 8/3/2019 Empezando Con Map Objet

    11/26

    Test your changes

    1. Click the Run button in the Visual Basic toolbar.

    2. Click somewhere on the map and drag a rectangle to

    zoom in.

    3. Click the pan button in your applications toolbar.

    4. Click somewhere on the map and then drag to pan.

    5. Click on the full extent button (the globe) in your

    applications toolbar to draw the map at its full extent.

    Save your changes

    1. Click the Stop button in the Visual Basic toolbar to return

    to design mode.

    2. Click the Save Project button in the Visual Basic toolbar

    to save your changes.

    Creating a find toolIn this section you will add additional controls to your application toimplement a simple function for locating a state by name.

    Add controls to the form1. Double-click the Label button in the Toolbox to add a label to the form.

    2. Reposition the label in the lower left corner of the form.

    3. Display the Properties window by pressing F4, and set the caption of

    the label to be State:.

    4. Double-click the TextBox button in the Toolbox to add a TextBox to the

    form, and position it next to the label.

    5. Clear the Text property of the TextBox using the Properties window.

    6. Resize the map so that it is not obscured by the new controls.

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 11

  • 8/3/2019 Empezando Con Map Objet

    12/26

    Attach code to the TextBox

    You will use the text the user types into the TextBox toperform a logical query.

    1. Double-click the TextBox to show the Code window.

    2. Add code to Text1s KeyPress procedure, by selectingKeyPress from the right hand drop down list:

    Private Sub Text1_KeyPress(KeyAscii As Integer)

    If KeyAscii = vbKeyReturn Then

    Dim recs As MapObjects2.Recordset

    Dim shp As Object

    Dim rect As MapObjects2.Rectangle

    Dim exp As String

    build a search expression

    exp = STATE_NAME = & Text1.Text &

    perform the search

    Set recs = Map1.Layers(States). _SearchExpression(exp)

    show the results, if any

    If Not recs.EOF Then

    Set shp = recs.Fields(Shape).Value

    Set rect = shp.Extent

    rect.ScaleRectangle 2

    Set Map1.Extent = rect zoom to state

    Map1.Refresh force redraw of the map

    Map1.FlashShape shp, 3 flash the state

    End If

    End IfEnd Sub

    The code first builds a simple SQL query expression using thevalue of the TextBoxs Text property, then searches the Stateslayer using the SearchExpression method; the result is aRecordset object. If the value of the Recordsets EOF property

    is False, the code positions Recordset on the first record thatsatisfies the search expression. In that case, the code gets thevalue of the Shape field for the first record. The code scalesthe Extent of the shape and then sets it to be the extent of themap. The code then redraws the map explicitly, using theRefresh method; and finally, flashes the shape three times.

    Test your changes

    1. Run your application.2. Type the name of a state, e.g. Vermont, into the TextBox.

    3. Press the Enter key.

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 12

  • 8/3/2019 Empezando Con Map Objet

    13/26

    4. When you are finished running your application, click the

    Stop button in the Visual Basic toolbar.

    Handling Resize

    When you run your application and resize the form, youwill notice that the map is not automatically resized.

    Respond to the Resize event

    1. Double-click the form to display the Code window.

    2. Add code to the forms Resize procedure, by selecting

    Resize from the right-hand drop down list:

    Private Sub Form_Resize()

    y coordinate of the find controls

    Dim yFind As Integer

    a constant spacing

    Dim space As Integer

    space = Text1.Top - (Map1.Top + Map1.Height)

    yFind = ScaleHeight - Text1.Height - space

    move the controls that make up the find tool

    Label1.Move Label1.Left, yFind

    Text1.Move Text1.Left, yFind

    move the map itself

    Dim mapTop As Integer

    mapTop = Toolbar1.Top + Toolbar1.Height

    Map1.Move 0, mapTop, ScaleWidth, _yFind - space - mapTop

    End Sub

    When the user resizes the form, the code resizes the controlsusing the Move method.Notice that when you run your application, it draws the maptwice initially. This is due to the fact that controls on the formare initially displayed using the size and position specifiedduring design time. To fix this problem, you will resize thecontrol when the form is initialized. You have already writtenthe code to resize the controls, you just need to call theprocedure.

    3. Double-click the form to show the Code window.

    4. Add code to the forms Initialize procedure.

    Private Sub Form_Initialize()Form_Resize

    End Sub

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 13

  • 8/3/2019 Empezando Con Map Objet

    14/26

    Displaying map layers based on scaleIn this section you will add a new layer to your map and add code thatcontrols whether or not that layer is visible at a given time.

    Add another layer

    1. Right-click the map to display the context menu. Click Properties to

    show the property sheet.

    2. Click the Add button and locate the folder where the sample data is

    stored.

    3. Click the Counties.shp file and then click Open.

    4. Click the Counties layer in the list to select it.

    5. Click the down arrow to move the Counties layer below the USHighlayer.

    6. Click Properties to change the color of the Counties layer.

    7. Click OK to dismiss the Layer Properties dialog.

    8. Click OK to dismiss the property sheet.

    If you run your application now you will notice that it displays everycounty in the United States. At the full extent, there is no need to displaythat much detail, so in response to the BeforeLayerDraw event, you will

    selectively make the Counties and States layers visible or invisible,depending on the current extent of the map.

    Respond to the BeforeLayerDraw event

    1. Double-click the map control to display the Code window.

    2. Add code to Map1s BeforeLayerDraw procedure:

    Private Sub Map1_BeforeLayerDraw(ByVal index _As Integer, ByVal hDC As stdole.OLE_HANDLE)

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 14

  • 8/3/2019 Empezando Con Map Objet

    15/26

  • 8/3/2019 Empezando Con Map Objet

    16/26

    Adding a spatial query toolIn this section you will add a new tool to the toolbar thatwill perform spatial queries on the map. You will add codeto your application that will draw the results of the spatialquery on the map.

    Add a new button to the toolbar

    1. Right-click the toolbar to display the context menu. Click

    Properties to show the property sheet.

    2. Click the Buttons tab.

    3. Click the right arrow twice to change the Index to 3.

    4. Change the buttons Style to Button Group, and its

    Image to 4.

    5. Click OK to dismiss the property sheet.

    Add a variable to the form

    1. Double-click the form to display the Code window.

    2. In the General section, declare a variable that will be the

    results of the spatial query. The type of the variable will

    be a Recordset.

    Dim gSel As MapObjects2.Recordset

    It is necessary to completely qualify the Recordset typewith MapObjects in order to avoid a name conflict withVisual Basics built-in Recordset type.

    Implement the query tool

    Modify Map1s MouseDown procedure.

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 16

  • 8/3/2019 Empezando Con Map Objet

    17/26

    Private Sub Map1_MouseDown(Button As Integer, _Shift As Integer, x As Single, y As Single)

    zoom

    If Toolbar1.Buttons(1).Value = 1 Then

    Set Map1.Extent = Map1.TrackRectangle

    pan

    ElseIf Toolbar1.Buttons(2).Value = 1 Then

    Map1.Pan

    spatial query

    ElseIf Toolbar1.Buttons(3).Value = 1 Then

    Dim p As MapObjects2.Point

    Dim recs As MapObjects2.Recordset

    Set p = Map1.ToMapPoint(x, y)

    search for a highway

    Set recs = Map1.Layers(USHigh). _SearchByDistance(p, Map1. _ToMapDistance(100), )

    nothing is found

    If recs.EOF Then

    Set gSel = Nothing counties that intersect highways

    Else

    Set gSel = Map1.Layers(Counties). _SearchShape(recs).Fields(Shape). _Value, moEdgeTouchOrAreaIntersect, )

    End If

    trigger a redraw of the map

    Map1.RefreshEnd If

    End Sub

    When the current tool is the spatial query tool, two searchesare performed. The first search is a point proximity search onthe USHigh layer. The code obtains the point by converting

    the X and Y coordinates of the event, which are in controlunits, into map units. If the first search is successful, the codeuses the highway it found as the input to the second search thatit performs on the Counties layer. The code stores the result ofthe second search in the variable named gSel.

    Draw the results1. Modify Map1s AfterLayerDraw procedure.

    2. Add code to display the results of the query on top of the Counties

    layer.

    Private Sub Map1_AfterLayerDraw(ByVal index _As Integer, ByVal canceled As Boolean, ByVal _hDC As stdole.OLE_HANDLE)

    If index = 1 Then countiesIf Not gSel Is Nothing Then

    Dim sym as New MapObjects2.Symbol

    sym.Color = moYellow

    Map1.DrawShape gSel, sym

    End If

    End If

    End Sub

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 17

  • 8/3/2019 Empezando Con Map Objet

    18/26

    Test your changes

    1. Run your application and zoom into an area so that the

    Counties layer becomes visible.

    2. Click the spatial query tool, then click on a highway.

    3. Click the Stop button in the Visual Basic toolbar.

    4. Click the Save Project button to save your changes.

    Statistical mappingIn this section youwill modify your application so that itdraws the Counties layer using the underlying attribute

    information.

    Attach a renderer to the Counties layer

    Each MapLayer object has a Renderer property. A Renderer objectcontrols how MapObjects draws a MapLayer. You can use theClassBreaksRenderer to display continuous data, in this case, the numberof mobile homes per capita by county.

    1. Double-click the form to display the Code window.

    2. Add code to the Load procedure for the Form.

    Private Sub Form_Load()

    counties layer

    Dim rC as New MapObjects2.ClassBreaksRenderer

    Set Map1.Layers(Counties).Renderer = rC

    rC.Field = MOBILEHOMESet stats = Map1.Layers(Counties).Records _.CalculateStatistics(MOBILEHOME)

    calculate breaks away from the mean,

    only add breaks within the range of values

    Dim breakVal As Double

    breakVal = stats.Mean - (stats.StdDev * 3)

    For i = 0 To 6If breakVal >= stats.Min And _breakVal

  • 8/3/2019 Empezando Con Map Objet

    19/26

    Attach a renderer to the States layer

    1. Modify the forms Load procedure.

    2. Append this code at the end of the form's Load

    procedure.

    states layer

    Dim rS as New MapObjects2.DotDensityRenderer

    Set Map1.Layers(States).Renderer = rS

    rS.Field = HOUSEHOLDS

    Set stats = Map1.Layers(States).Records. _CalculateStatistics(HOUSEHOLDS)

    rS.DotValue = stats.Max / 40

    Test your changes

    1. Run your application and look at the States layer. Notice

    that the application draws polygons with dots that

    indicate the number of households.

    2. Zoom into an area so that the Counties layer becomes visible. Notice

    that the application now draws the counties in different colors,

    depending on the underlying attribute values.

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 19

  • 8/3/2019 Empezando Con Map Objet

    20/26

    3. Click the Stop button in the Visual Basic toolbar.

    4. Click the Save Project button to save your changes.

    Event trackingSome applications must display geographic entities on topof the map, especially if those entities have a tendency to

    move. For example, a vehicle tracking system would wantto display vehicles on the map at the appropriate locationsand update those locations over time, without redrawing allthe layers of the map each time a vehicle changes location.

    In this section, you will add an event tracking layer to yourapplication to facilitate this requirement.

    Add an event tool to your applications

    toolbar1. Right-click the toolbar to display the context menu. Click

    Properties to show the property sheet.

    2. Click the Buttons tab.

    3. Click the right arrow three times so that the Index displayed is 4.

    4. Change the buttons Style to Button Group, and its image to 5.

    5. Click OK to dismiss the property sheet. Your toolbar should now have

    five buttons, as shown below.

    Implement the event tool

    1. Double-click the map to display the Code window.

    2. Change Map1s MouseDown procedure.

    Private Sub Map1_MouseDown(Button As Integer, _Shift As Integer, x As Single, y As Single)

    If Toolbar1.Buttons(1).Value = 1 Then

    zoom

    Map1.Extent = Map1.TrackRectangle

    ElseIf Toolbar1.Buttons(2).Value = 1 Then

    pan

    Map1.Pan

    ElseIf Toolbar1.Buttons(3).Value = 1 Then

    spatial query

    Dim p As MapObjects2.Point

    Dim tol As Double

    Dim recs As MapObjects2.Recordset

    Set p = Map1.ToMapPoint(x, y)

    Set tol = Map1.ToMapDistance(100)

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 20

  • 8/3/2019 Empezando Con Map Objet

    21/26

    search for a highway within the tolerance

    Set recs = Map1.Layers(USHigh). _SearchByDistance(p, tol, )

    If recs.EOF Then nothing is found

    Set gSel = Nothing

    Else

    counties that intersect the highways

    Set gSel = Map1.Layers(Counties). _SearchShape(recs.Fields(Shape).Value, _moEdgeTouchOrAreaIntersect, )

    End If

    trigger a redraw of the map

    Map1.Refresh

    ElseIf Toolbar1.Buttons(4).Value = 1 Then add an event

    Dim pt as MapObjects2.Point

    Set pt = Map1.ToMapPoint(x, y)

    With Map1.TrackingLayer.Symbol(0)

    .SymbolType = moPointSymbol

    .Size = 4

    .Style = moSquareMarker

    End With

    Map1.TrackingLayer.AddEvent pt, 0

    End If

    End Sub

    Test the event tool

    1. Run your application.

    2. Zoom into an area.

    3. Click the event tool, then click in the map to add events.

    4. Click the Stop button in the Visual Basic toolbar.

    Add a timer to your form

    To trigger the movement of the events, a Timer control will be used.

    1. Double-click the Timer button in the Toolbox to add a timer to the form.

    The Timer control will not be visible when your application is running.

    2. Double-click the timer to display the Code window.

    3. Add code to the timer procedure.

    Private Sub Timer1_Timer()

    Dim maxDist As Double

    Dim nEventCount As Integer

    maxDist = Map1.Extent.Width / 20

    nEventCount = Map1.TrackingLayer.EventCount

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 21

  • 8/3/2019 Empezando Con Map Objet

    22/26

    For iIndex = 0 To nEventCount - 1

    Set gEvt = Map1.TrackingLayer.Event(iIndex)

    move each event randomly

    gEvt.Move maxDist * (Rnd - 0.5), _maxDist * (Rnd - 0.5)

    Next iIndex

    End Sub

    Add a CheckBox to your form

    To control the timer, you will add a CheckBox control toyour application.

    1. Double-click the CheckBox button in the Toolbox to add

    a CheckBox to the form.

    2. Move the CheckBox to the lower right corner of the form.

    3. Open the Properties window and set the Caption to Data

    collection.

    4. Double-click the CheckBox control to open the Code

    window.

    5. Add code to Check1s Click procedure.

    Private Sub Check1_Click()Timer1.Interval = Check1.Value * 500

    End Sub

    Test your changes

    1. Run your application.

    2. Zoom into an area.

    3. Click the event tool, then click in the map to add events.

    4. Click the Data collection check box. Notice that the events start movingrandomly on top of the map.

    5. Click the check box again to stop the events.

    Working with DataConnection objectsIn each of the previous sections, you have worked with MapLayer objectsthat were specified interactively using the Map controls property sheet.

    In this section, you will add code to your application that createsMapLayer objects programmatically using a DataConnection object.

    Remove the existing layers

    1. Right-click the mouse on the map to display the context menu.

    2. Choose Properties to display the property sheet.

    3. Click on the Ushigh layer, then click Remove to delete the layer.

    4. Remove Counties and States in the same manner, then click OK.

    Add a procedure that will initialize the map

    1. Double-click the form to display the Code window.

    2. In the General section, declare a procedure.

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 22

  • 8/3/2019 Empezando Con Map Objet

    23/26

    Sub InitializeMap()

    Dim dc As New DataConnection

    Dim layer As MapLayer

    dc.Database = C:\Program Files\ESRI\ _MapObjects2\Samples\Data\USA

    If dc.Connect Then

    Set layer = New MapLayer

    layer.GeoDataset = dc.FindGeoDataset(States)

    layer.Symbol.Color = moPaleYellow

    Map1.Layers.Add layer

    Set layer = New MapLayer

    layer.GeoDataset =dc.FindGeoDataset(Counties)

    Map1.Layers.Add layer

    Set layer = New MapLayer

    layer.GeoDataset = dc.FindGeoDataset(USHigh)

    layer.Symbol.Color = moRedMap1.Layers.Add layer

    Else

    MsgBox The data could not be located.

    End exit the application

    End If

    End Sub

    3. Add a call to your procedure in the Forms Load procedure.

    Private Sub Form_Load()

    InitializeMap

    4 Run your application to test the changes. The map should appear as

    before, but the colors of the MapLayers are specified in the code, and

    may be different to the colors you selected in the Layer Properties

    dialog box.

    Working with ImageLayer objects

    In each of the previous sections, you have worked with MapLayer objectsbased upon vector data sources. In this section, you will see how to addlayers to your map that are based on images. MapObjects allows you touse a wide range of image types as ImageLayers, including such commonimage types as windows bitmaps (.bmp), tagged image file format (.tiff),and CompuServe bitmaps (.gif). For a full, up-to-date list of the imageformats you can use in a map, see "Supported Image Formats" in the"Using MapObjects" section of the online help.

    Add a new form to your project

    1. Right-click on the StarterMap project in the Project Explorer.

    2. From the context menu, choose Add and then choose Form.

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 23

  • 8/3/2019 Empezando Con Map Objet

    24/26

    3. In the Add Form dialog box, choose Form.

    A new Form called Form1 should now be added to your

    project.

    4. Add a Map to the new form by double-clicking the Map

    control in the Visual Basic toolbox.

    5. Resize the Map to fill the form.

    Save the new form

    1. From the File menu, choose save.

    2. In the File Name box type ImageLayer.frm.

    3. Click Save.

    Set the new form as the Startup object

    Before you run your project, you must specify that the new form shouldbe the Startup Object. A form specified as the Startup Object is loaded assoon as the project is run.

    1. Right-click on the StarterMap project in the Project Explorer.

    2. From the context menu, choose StarterMap Properties.

    3. Select Form2 from the Startup Object combo box.

    4. Click OK.

    Select an image layer to display on the map

    You can specify an image to display as an ImageLayer by settingproperties in the Map controls property sheet.

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 24

  • 8/3/2019 Empezando Con Map Objet

    25/26

    1. Right-click the mouse on the map to display the context

    menu.

    2. Choose Properties to display the property sheet.

    3. Click the Add button, and then select "All supported

    Image formats" from the "Files of type" combo box.

    4. Navigate to the Washington sample data folder.

    5. Click the Wash.bmp file and then click Open.

    6. Click Add again and then choose the Roads shapefile.

    Test your changes

    1. Click the Run button in the Visual Basic toolbar.

    2. To stop running your application and return to design

    mode, click the Stop button in the Visual Basic toolbar.

    Adding an ImageLayer in code

    Previously, you added a MapLayer programmatically, using theDataConnection object. Now you will add an ImageLayer

    programmatically. To do this, you use the File property of theImageLayer object.

    Remove the existing layers

    1. Right-click the mouse on the map to display the context menu.

    2. Choose Properties to display the property sheet.

    3. Select Wash.bmp, then click Remove to delete the layer.

    4. Select Roads, then click Remove.

    5. Click OK.

    Add a procedure that will initialize the map

    1. Double-click the form to display the Code window.

    2. In the Form_Load section, add the code:

    Private Sub Form_Load()

    Dim imgLayer As New MapObjects2.ImageLayer

    imgLayer.File = "C:\Program Files\ESRI\ _MapObjects2\Samples\Data\Washington\Wash.bmp"

    If imgLayer.Valid Then

    Map1.Layers.Add imgLayer

    Else

    MsgBox "Could not load Image"

    End If

    End Sub

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 25

  • 8/3/2019 Empezando Con Map Objet

    26/26

    Test your changes

    1. Click the Run button in the Visual Basic toolbar.

    2. To stop running your application and return to design

    mode, click the Stop button in the Visual Basic toolbar.

    CongratulationsYou have built a simple application with MapObjects.Although these exercises showed you many of thecapabilities of MapObjects, there is lots more to discover.To learn more, read the book named Building Applicationswith MapObjects, consult the MapObjects ProgrammersReference, or use the MapObjects online help for the most

    up-to-date information.

    GETTING STARTEDWITH MAPOBJECTSUSING VISUAL BASIC 26