Mobile walkthrough for the PPC

from:http://resources.esri.com/help/9.3/ArcGISmobile/adf/gs_mobile_walk_pocketpc.htm

This walkthrough is for developers who wish to learn about creating and deploying a simple ArcGIS Mobile application for the PocketPC environment. It uses step by step instructions to demonstrate how to create the application using the ArcGIS Mobile Map Control, the ArcGIS Mobile assembly and parts of ADO.NET.

You can find this sample in: <ArcGIS_install_location>\DeveloperKit\SamplesNET\Server\Mobile_Applications\Walkthrough_PPC05CSharp.zip

Project Description

The application will display map data that has been extracted from an ArcGIS Map Service using a Map Control, 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 then extend the application you build with additional functionality.

Concepts

There are some important concepts you should understand before starting this walkthrough. As a prerequisite, please read through the Mobile SDK conceptual documentation found in the book Undestanding ArcGIS Mobile to gain an understanding of the mobile framework and architecture. You should also have a good understanding of Visual Studio .NET 2005 or 2008 and how to create a Pocket PC application.

ArcGIS Server Mobile SDK Components

The ArcGIS Server Mobile SDK provides several Visual Studio components to help you develop mobile applications. The primary components that you will work with in this scenario are the MobileService and the Map. The MobileService component is used to interact with the Map Service published on a ArcGIS Server, and a local copy of that data compressed and stored in a folder called the CacheStoragePath.. The local data you will use has previously been extracted from a map 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 MobileService cache that has been created for you without having to connect to a server. Additional components are used to navigate the map itself and identify attribute information for given features. For more information on the components used in this walkthrough please refer to the ArcGIS Mobile Developer Help.

Requirements

In order to walk through this scenario you will need the following installed on your machine:

  • Visual Studio .NET 2005(SP1)/2008 with C#
  • Microsoft Mobile SDK 5.0 or higher
  • The ArcGIS Mobile SDK

Go to the Mobile Development environment book in the Developer Help system, for more details and links to downloads and instructions.

This example is written in C# and will be deployed to the Mobile 5.0 PocketPC emulator installed with Mobile SDK, unless you have a mobile device connected. Using the ArcGIS Mobile class diagrams while proceeding through this example, will help you learn about the classes and their use. You can find the class diagrams in the Mobile Assembly book of the ArcGIS Mobile Developer Help.


Implementation

In this example you will create a simple application within the PocketPC 2005 emulator that allows you to open a mobile map cache and display the map layers in a map control. You will then add additional controls that will let you navigate the map and identify features within the layers. The first step is to create the new project.

Creating a new project using Visual Studio 2005

  1. Start Visual Studio .NET 2005.
  2. On the File menu, point to New, then click Project .
  3. In the New Project dialog box, under Project Types, click Visual C#, expand Smart Device then click Windows Mobile 5.0 Pocket PC .
  4. In the Templates pane click Device Application .
  5. Enter an appropriate name for the project.
  6. Click OK. This will create a new project for you.

 


 

Creating a new project using Visual Studio 2008

  1. Start Visual Studio .NET 2008.
  2. On the File menu, point to New, then click Project .
  3. In the New Project dialog box, under Project Types, expand Visual C#, click Smart Device and select Smart Device Project from the Templates pane.
  4. Select .NET Framework 2.0, from the dropdown list at the top of the dialog.
  5. Enter an appropriate name for the project.
  6. Click OK. This will bring up the Add New Smart Device Project - dialog.
  7. For the Target Platform select Windows Mobile 5.0 Pocket PC SDK.
  8. For the .NET Compact Framework version select .NET Compact Framework Version 2.0.
  9. In the Templates pane click Device Application .
  10. Click OK. This will create a new project for you.

 


Using the Map Control at design time

Since the application was created as Mobile 5.0 Pocket PC Application, Visual Studio will create the proper solution files and structure, and display a form for a Pocket PC. The environment will also be configured will the toolboxs appropiate to the platform and using the developer's previous settings if any.

The ArcGIS Mobile controls are added to the Visual studio toolbox when you install the ArcGIS Mobile SDK. They are located within the ArcGIS Mobile Controls tab within the toolbox. To use the controls within your application you simply drag and drop them from the Toolbox to your windows form.

 

 

Within the following section, you will add both a Map and MapService component to the application and configure their properties.

  1. Double-Click the Map component from the toolbox onto the form.
  2. In the Properties Window change the Dock property to Fill so that the mapcontrol will utilize the full display area of the device as indicated by the screen shot above.

When the Map component is added to the form, a MobileService component will be added to your project automatically. Your form should now contain a Map, called Map1, and a MobileService component called MobileService1. Additionally two new references have been added to your project, one to the the ESRI.ArcGIS.Mobile assembly and the other the System.Web.Services assembly.

You now need to configure the components using their properties.

  1. Right click the Map Control and choose DataSources... on the context menu to display the DataSources window.
  2. Within the DataSources window for Map1, there should be MobileService1 in the list, use the Add button if it is not there.
  3. Now ensure that the CacheOpenMode is set to OpenorCreate in the Mobile Serivces properties panel.

 

You now add code to specify where the mobileservice cache is located on disk, and then open it which also draws the map to the display. (The cachestoragepath can also be set explicitly using the property window.)

  1. Doing this as part of the Form's Load event process is a common practise, In the Properties window of the Form select the Event tab and Double-Click on the Load event row.
  2. First add a using statement at the bottom of the list of other using statements already present to include the ArcGIS Mobile assembly:
    [C#]
    using ESRI.ArcGIS.Mobile;
  3. Add the following code to the class for Form1_Load() method
    [C#]
     private void Form1_Load(object sender, EventArgs e)
     {
       // Set the mapcachestorage location on disk
       mobileService1.CacheStoragePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\Walkthrough_Cache";
    
      if (!mobileService1.IsValid)
                {
                    MessageBox.Show("Map Cache is not valid!");
                    return;
                }
                try
                {
                    mobileService1.Open(ESRI.ArcGIS.Mobile.MobileServices.CacheOpenMode.Open);
                }
                catch
                {
                    MessageBox.Show("Cannot open map cache");
                }
      }
      

This code will check the mobileservice cache to ensure it is valid, then open the mobileservice cache, which draws it to the display. Notice that most of the code is used to ensure that this method will execute without crashing, and to provide the end user with understandable error messages.

Application Hint: By default the application will display an X in the top right hand corner of the device when it is running. This allows you to place the application in the background but not shut it down. To stop the application completely you will have to stop the program from the device memory settings dialog. You can change this behavior by setting the MinimizeBox property on the form to False. This will display an Ok button on the running application form instead of an X. Clicking this button will stop the application. You may optionally set this property for the walkthrough.

Compile the application. On the Build menu, choose Build Solution. The solution should compile fine but if any errors appear in the Errors List window, use the messages to correct the problems.

 

Configuring the PocketPC Emulator

You will use the PocketPC Emulator - WM 5.0 to test the application but before you can it must be started and then virtually cradled.

  1. From the Visual Studio Tools menu choose Device Emulation Manager.
  2. In the Device Emulator Manager dialog, locate the Windows Mobile 5.0 Pocket PC Emulator, right click and select Connect. This will start the device emulator.
  3. Right click again on the Windows Mobile 5.0 Pocket PC Emulator entry and select Cradle. This will cradle the emulator via ActiveSync. Select a Guest partnership if asked for the type of ActiveSync relationship. Hint: If ActiveSync does not automatically connect to the device you can try this manually in ActiveSync by selecting File -> Connection Settings, then click the Connect button and follow the wizard.

 


 

NOTE:  Before running the application, you need to copy the Redlands map cache from the samples' data directory to the emulators' \My Documents folder. From the Tools menu in ActiveSync, click Explore Device and navigate to \My Documents and paste the MapCache folder from <ArcGIS_Server_Install_Location>\DeveloperKit\SamplesNET\Server\Mobile_Applications\Walkthrough_PPC05CSharp\CSharp\Mapcache.

You will also need to install the ArcGIS Mobile runtime onto the emulator or device. The easiest way to do this is to copy the Runtime.CAB file onto the device\emulator and execute it to unpack.

You may now compile and run the application. Select the Windows Mobile 5.0 Pocket PC Device emulator as the target device. At this stage the form should display layers from the map service in the map control.

Changing the Mouse behavior

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 pan mouse action listens to the MouseDown, MouseMove, and MouseUp events to begin, execute, and complete panning action.

A map can have multiple map actions associated with it however, by design only one is active or current. For instance a map can have zoom in, zoom out, pan, identify, and polygon sketch actions available, but at any given time only one action is executed. In this section you will add mapaction components and controls to the project to alter the mouse behavior.

 


 

  1. From Properties window of the Map control, find the MapActions item and click the button appearing in the cell next to (Collection), or use the context menu on the Map control. In the MapAction Collection Editor window, use the Add button to add PanMapAction, ZoomInMapAction and ZoomOutMapAction to the collection as shown in the graphic above. The Add button becomes a dropdown list when a right mouse click is used to expand the button, or the dropdown arrow is selected.
  2. Each of the components defaults their Map property to Map1, ensure they are set correctly and press OK to dismiss the window.
  3. At the Botom of the Map display on the devive their is a strip that is used for menus. Move the mouse into this area and click to begin adding menu items, that will be linked to the MapActions. The text for the Menu items should be ZoomIn, ZoomOut and Pan to standardize their appearance and look similar to the screenshot below.
  4. Double click on each of the menu items to create a code block for the Click Event for each item.
  5. Switch to the code view for the form and add the following using statement, below the rest of the list.
    [C#]
    using ESRI.ArcGIS.Mobile.MapActions;
  6. Add the following code to the Click Events
    [C#]
            private void menuItem2_Click(object sender, EventArgs e)
            {
                map1.CurrentMapAction = zoomInMapAction1;
            }
    
            private void menuItem3_Click(object sender, EventArgs e)
            {
                map1.CurrentMapAction = zoomOutMapAction1;
            }
    
            private void menuItem4_Click(object sender, EventArgs e)
            {
                map1.CurrentMapAction = panMapAction1;
            }
    
  7. Compile and run your application.

The application should display the map data from the MobileServiceCache and you should be able to change the mouse action by simply selecting the menu items. Zoom in on the map and notice how the diplay changes the data being dipslayed automatically, using scale-dependent drawing properties embedded in the map document.

 

Adding Identify Capability

Much like the functionality contained within other ArcGIS applications you can identify features inside the MapControl by querying the location identified by the mouse. In this section we will add some custom code to enable identify.

  1. Select the Menu on the sample form, and add a new item.
  2. Set the text to Identify .
  3. 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 will create a code block for the event.
  4. Add the following code to the MapControl_MouseDown event.
    [C#]
    if (map1.CurrentMapAction != 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 (MobileService MService in map1.DataSources)
            {
                foreach (MobileServiceLayer MSLayer in MService.Layers)
                {
                    //Only Feature layers are used in identify
                    if (MSLayer is FeatureLayer)
                    {
                        txtResult += "\r\n Layer " + MSLayer.Name;
                        FeatureLayer FLayer;
                        //cast the MobileServiceLayer to a featureLayer
                        FLayer = (FeatureLayer)MSLayer;
                        using (FeatureDataReader featReader = FLayer.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());
        }
    
  5. Compile and run the application.

Experiment with the application. Change the mouse modes via the device softkeys to explore the Pocket PC experience.

 


posted @ 2011-01-06 16:17  http_it  阅读(333)  评论(0编辑  收藏  举报