My First iOS Application

Using the ArcGIS API for iOS, you can build applications that run on iOS devices like iPhone, iPad, and iPod touch.

Developing applications for the iPhone

The following tutorial will walk you through the process of creating your first iPhone application.

Creating a Project

To make it easier to create ArcGIS applications for iOS, we've provided a number of ArcGIS project templates. These templates import all of the necessary ArcGIS header files and set the required build parameters and library dependencies.

Start by opening Xcode and creating a new project. In the New Project Window, you will be asked to choose from a list of User Templates. On the left panel of the dialog you will see that templates are organized into categories and one of the categories is titled "User Templates". Select ArcGIS from the list of User Templates and you will see a number of ArcGIS templates appear.

Choose ArcGIS Template

Select the View-based ArcGIS Mapping Application template and then click the Choose... button.

Once you have chosen a template, you will be prompted to save the project. Title the project "MyFirstMapApp" and click Save.

The project contains a nib file (MyFirstMapAppViewController.xib) that represents the view of your application. Locate the nib file in the Resources folder of your project, and double-click it to open it. You will notice that it already contains a Map View for your convenience.

Nib file with Map view

The template also provides a view controller (MyFirstMapAppViewController) that manages the nib file. Look at the view controller's header file - MyFirstAppViewController.h. You will notice that it contains a reference to the map view in the nib file. This enables us to access the Map View programatically.

@interface MyFirstMapAppViewController : UIViewController {
 	AGSMapView *_mapView;
}  

@property (nonatomic, retain) IBOutlet AGSMapView *mapView;  

@end

Let's take a look at the implementation of the view controller. Open the MyFirstAppViewController.m file, and look at the viewDidLoad method. This method is run when the view controller first displays the view it is managing. You will notice that a tiled layer using ArcGIS Online is added to the map view, and that the map view is zoomed into an area of the map.

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 - (void)viewDidLoad {
  [super viewDidLoad];
 	/* ##################################################### */
  // TODO
  // Replace the following block of code with your own.
  //
  
  NSURL *mapUrl = [NSURL URLWithString:@"http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"];
  AGSTiledMapServiceLayer *tiledLyr = [AGSTiledMapServiceLayer tiledMapServiceLayerWithURL:mapUrl];
  [self.mapView addMapLayer:tiledLyr withName:@"Tiled Layer"];

 	AGSSpatialReference *sr = [AGSSpatialReference spatialReferenceWithWKID:102100];
  AGSEnvelope *env = [AGSEnvelope envelopeWithXmin:-13626513.829723023 ymin:4549088.827634182 xmax:-13626131.64458163	ymax:4549638.218774935	spatialReference:sr];
  [self.mapView zoomToEnvelope:env animated:YES];
  	
  /* ##################################################### */ 

}

Testing the Application

At this point we should be able to build and run the application successfully. The application should display a map from ArcGIS Online and zoom into an area.

Experiment with some of the navigation functionality that's included with the template.

To zoom in:

  • Double-click
  • Use pinch navigation within the simulator by holding the option key, clicking your mouse and dragging away from the center of the map

To Zoom out:

  • Use pinch navigation within the simulator by holding the option key, clicking your mouse and dragging towards the center of the map

Notice that more detail in the map is visible as you zoom in:

BuildAndRun

NoteNote:

If you are unable to successfully run the application, check the location of the libArcGIS.a library. It should be located in the proper directories as outlined in the Creating a New Xcode Project topic.

Rotating the Map

To allow the map to rotate uncomment the shouldAutoRotateToInterfaceOrientation method in the MyFirstAppViewController.m file and add the following code:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortrait);
}

Build and run the application.

To rotate the map click Hardware -> Rotate Left (or the command-Left Arrow buttons).

Notice when the device is rotated the map also rotates.

RotateMap

Developing applications for the iPad

As you have seen above, ArcGIS project templates make is very easy to create iPhone applications.

Eventhough there are no separate ArcGIS project templates for the iPad, it is possible to configure your existing iPhone Xcode projects to create both iPhone & iPad applications. For more information how to configure your Xcode project, and how to port iPhone applications to iPad, please refer to Apple's iPad Programming Guide.

You can also start with an iOS project template specially designed for building iPad applications. However, you will need to manually configure this project as described in Using iOS Project Templates to develop with the ArcGIS API.

9/14/2011