Building your first ArcGIS SDK for Windows Mobile application for Windows

This walkthrough describes how to create and deploy a simple mobile application for the Windows tablet or laptop device. It uses step-by-step instructions to demonstrate how to create an application using the MapControl, the Mobile assembly, and parts of ADO.NET.

Project description

The application will display map data using a MapControl, provide basic navigation tools such as zoom and pan, and include an identify function to show feature attributes. Once you complete this walkthrough, you can extend the application with additional functionality, or use what you learned to build your own application. While this walkthrough is written in C#, the same functionality can be created with VB .NET.

Concepts

Even though this walkthrough does not require previous experience with ArcGIS Runtime SDK for Windows Mobile, there are some important concepts you should understand before starting. As a prerequisite, read the conceptual documentation in this Help system to gain an understanding of the mobile framework and architecture. Using the class diagrams while proceeding through this walkthrough will help you learn about the classes and their relationships. You can find the class diagrams for each namespace in the Mobile Assembly book of the API Help. In addition, you should have a good understanding of Visual Studio .NET 2008 and how to create a Windows application.

Where to get the sample

The sample is available for download from the ArcGIS.com code gallery..

Developer components

The mobile assembly provides several Visual Studio components to help you develop mobile applications. The primary components you'll work with are MobileServiceConnection, MobileCache, SyncAgent, and the Map. The MobileServiceConnection component represents the connection to the MobileService published on an ArcGIS Server, the MobileCache represents a local compressed copy of that data, and the SyncAgents control the flow of data in this connection. While this sample can run without a connection to a server, it is important to understand the role each of these components play in the system and how they are linked together and operate as a group to reflect the unique tasks each of them perform. The local data used in this walkthrough was extracted from a web service that was published with mobile capabilities. You do not need to create a mobile web service to complete this walkthrough or have a mobile device. The Map component will display the contents of the MobileCache without connecting to a server. Additional components are used to navigate the map and identify attribute information for given features. For more information on the components used in this walkthrough, see the Developer Help.

Requirements

To complete this walkthrough, you need the following installed on your machine:

See the Developer Requirements topic for more details and links to downloads and instructions.

Implementation

In this walkthrough, you will create a simple Windows Forms application that allows you to connect to a mobileservice, open a mobilecache, and display the data in a map control. Then you'll add controls to navigate the map and identify selected features.

Steps:
  1. Creating a new project using Visual Studio .NET 2008
    1. Start Visual Studio .NET 2008.
    2. On the main menu, go to File > New > Project.
    3. In the New Project dialog box, under Project Types, expand Visual C#, click Windows and select Windows Forms Application from the Templates pane.
    4. Select .NET Framework 3.5 from the drop-down list at the top of the dialog box.
    5. Type a project name.
    6. Click OK. This creates a new project.

    New Project dialog box creating a C# based Windows Forms Application

  2. Adding Map and MobileCache controls to your form

    The ArcGIS Mobile controls are added to the Visual Studio toolbox when you install ArcGIS Runtime SDK for Windows Mobile. To use the controls within your application, you can drag them from the toolbox to your Windows form.

    In this step, you'll add a Map component and a MobileCache control to the application and configure it.

    1. Double-click or drag d the Map component from the toolbox onto the form.
    2. Resize the map to fill the desired area as indicated by the following screen shot.
    3. Double-click or drag the MobileCache control from the toolbox onto the form.

    Visual Studio IDE with the Map control added to the form

    Your form now contains a Map, called Map1, and a MobileCache, called mobileCache1. Since the MobileCache component is not a visible component, it does not show on the form. Instead, it appears on the lower bar in the designer window.

  3. Configure the components

    You now need to add code to initialize the Map control with the data layers from the mobileCache1 on disk. If you have a valid mobile service, additional steps are required to create a new map cache and retrieve data from the server while the application is running.

    1. Create a form load event (double-click the form).
    2. Switch to code view, and add the following using statements to include the ArcGIS Mobile assembly:

      using ESRI.ArcGIS.Mobile;
      using ESRI.ArcGIS.Mobile.WinForms;
      using ESRI.ArcGIS.Mobile.FeatureCaching;
      using ESRI.ArcGIS.Mobile.FeatureCaching.Synchronization;
      

    3. Add the following code to the form1_load event:

      try 
        { 
        mobileCache1.StoragePath = @"C:\temp\MapCache";
        mobileCache1.Open(); 
        map1.MapLayers.Add(mobileCache1);
        } 
      catch 
        {
        MessageBox.Show("Cannot open map cache");
        }
      

    This code uses the MobileCache data stored at C:\temp\MapCache and draws it to the display. This cache is available from the MapCache folder from the downloaded archive.

    Before you compile the app, right-click the project name in Solution Explorer, choose Properties, tap Build on the page, and change Platform target from Any CPU to x86. This is required because the mobile core software development kit (SDK) is built for x86.

    Click Build > Build Solution to compile the application.

    NoteNote:

    Before running the application, you need to copy the map data from the samples data directory to the C:\Temp\MapCache folder on your computer. Using Windows Explorer, navigate to C:\Temp and paste the MapCache folder from your download folder.

    You can now run the application. At this stage, the form displays layers from the mapcache in the Map control.

  4. Interacting with the Map

    Map actions are components designed to listen and react to events raised by the mouse, keyboard, and other input devices against the map surface. For instance, the pan mouse action listens to the MouseDown, MouseMove, and MouseUp events to begin, execute, and complete the panning action.

    Now that you have the data drawing in the display, you'll add navigation capabilities.

    A map can have multiple map actions associated with it. However, by design, only one is active or current at a time. For instance, a map can have zoom in, zoom out, pan, identify, and sketch actions available, but at any given time, only one action can be executed. In this step, you'll add MapActions to the collection held by the Map control and add a context menu to control the MapAction being executed. Navigating a map is an essential part of any mapping application, and there are many different ways to implement it; a context menu is used in this walkthrough to keep the mapping application as compact as possible while providing maximum usability.

    1. From the code window of your form, move to the top of the code to just above the Form1_Load() code block. Within the code for the Form class just below the InitialComponent() section, insert the following code block to create the mapactions:

      PanMapAction panMapAction1 = new PanMapAction();
            ZoomInMapAction zoomInMapAction1 = new ZoomInMapAction();
            ZoomOutMapAction zoomOutMapAction1 = new ZoomOutMapAction();
      

    2. Find the ContextMenuStrip control on the Menus & Toolbars tab of the Visual Studio toolbox. Double-click or drag it to add it to the Form components.
    3. Select the Context Menu and add Pan, Zoom In, and Zoom Out as shown in the following screen shot:

      Visual Studio IDE with a Context menu added to the form

    4. Double-click each of the context menu items to create a code block for the Click Event for each menu item.
    5. Add the following code to the Click Events:

      private void panToolStripMenuItem_Click(object sender, EventArgs e)
            {
              map1.MapAction = PanMapAction1;
            }
      
            private void zoomInToolStripMenuItem_Click(object sender, EventArgs e)
            {
              map1.MapAction = ZoomInMapAction1;
            }
      
            private void zoomOutToolStripMenuItem_Click(object sender, EventArgs e)
            {
              map1.MapAction = ZoomOutMapAction1;
      

    6. From the Properties window of the Map control, find the ContextMenuStrip item and click the cell to get the drop-down list containing the newly created ContextMenuStrip1 and select it.
    7. Compile and run your application.

    Your application is now in a state where it will compile and run. In this state, you can change the mouse action by right-clicking the map and selecting the desired context menu item. The selected MapAction or navigation type will allow you to pan and zoom on the map display.

  5. Adding identify capability

    Similar to the functionality within other ArcGIS applications, you can identify features inside the Map control by querying the location identified by the mouse. In this step, you'll add custom code to enable the identify function.

    1. Select the ContextMenuStrip control from the Design view of the Form and add another menu item with the text Identify.
    2. Double-click the Identify menu item to create a code block for the Click event.
    3. Add the following code to the Click event:

      private void identifyToolStripMenuItem_Click(object sender, EventArgs e)
            {
              map1.MapAction = null;
            }
      

    4. Add a MouseDown event to the Map control. With the Map control active in the form, click Events in the Properties window. Find the MouseDown event in the list of events and double-click it. This creates a code block for the event.
    5. Adding this using namespace statement at the start of your class is required to compile your new code, when the following is added:

      using ESRI.ArcGIS.Mobile.Geometries;
      

    6. Add the following code to the MapControl_MouseDown event:

      private void map1_MouseDown(object sender, MapMouseEventArgs e)
            {
              //right mouse button used to display the context menu
              if (e.Button == MouseButtons.Right)
                return;
               
              if (map1.MapAction != null)
                return;
      
                Cursor.Current = Cursors.WaitCursor;
                MapMouseEventArgs me = e as MapMouseEventArgs;
                Envelope qEnv = new Envelope(me.MapCoordinate, me.MapCoordinate);
      
                double mapTolerance = map1.ToMap(3);
                qEnv.Resize(mapTolerance, mapTolerance);
      
                QueryFilter qFilter = new QueryFilter(qEnv, GeometricRelationshipType.Intersect);
      
                string txtResult = "Identify Results: ";
                int intFields;
      
          
                foreach (FeatureSource FSource in mobileCache1.FeatureSources)
                {
                
                  txtResult += "\r\n Layer " + FSource.Name;
                  using (FeatureDataReader featReader = FSource.GetDataReader(qFilter))
                    {
                      intFields = featReader.FieldCount;
                      while (featReader.Read())
                      {
                         for (int i = 0; i < intFields; i++)
                           txtResult += "\r\n" + featReader.GetName(i) + ": " + featReader.GetValue(i).ToString();
                      }
                    }
      
                }
             
              Cursor.Current = Cursors.Default;
              
              MessageBox.Show(txtResult.ToString());
            }
      

    7. Compile and run the application.
1/7/2015