Creating A Map
The primary way that you can display geographic information in your iOS application is by using a Map View. The Map View lets you mash up layers from different sources.
How to Create a Map
If you used an ArcGIS Project Template to create your project, the main view controller and the nib file will already contain a Map View and you can skip directly to adding layers to the map.
However, there may be times when you need to add map to an existing project that wasn't created using the ArcGIS project templates. In such cases, you will first need to ensure that the project is properly configured to use the ArcGIS library as described in Using iOS Project Templates. Other times, you may have created a new view controller to which you want to add a map. Either way, the steps below will guide you through adding a map to your application.
Adding a map to the view controller
Let's assume we have a View Controller named 'MyFirstMapAppViewController' to which we want to add a map.
1. Import the "ArcGIS.h" interface file in your view controller's class interface (.h) file:
#import "ArcGIS.h"
2. Add an AGSMapView instance variable to your view controller's class interface and add a declared property to access it:
@interface MyFirstMapAppViewController : UIViewController {
AGSMapView *_mapView;
}
@property (nonatomic, retain) IBOutlet AGSMapView *mapView;
@end
The IBOutlet keyword will allow us to connect to an AGSMapView control in Interface Builder. Also, notice that the instance variable is prefixed with an '_', while the @property is not. This will be explained in the next step.
3. In the view controller's implementation file (.m), use the @synthesize directive to synthesize getter and setter methods for the mapView property:
#import "MyFirstMapAppViewController.h"
@implementation MyFirstMapAppViewController
@synthesize mapView = _mapView;
Notice that the mapView property will be represented by the _mapView instance variable. This helps eliminate certain programming errors and is considered a 'best practice'.
4. In the dealloc method, you need to set the mapView property to nil:
- (void)dealloc {
self.mapView = nil;
[super dealloc];
}
Adding a map to the user interface
Let's assume that our view controller displays the user interface stored in a nib file called MyFistMapAppViewController.xib . We will need to add a Map View to the user interface. In Xcode double click on the nib file in the resources directory. This will open it in Interface Builder.
Notice the UIView object. We will add a Map View as a subview.
Open the Library window by clicking tools -> Library. The library window contains all of the visual components you can use when developing iPhone applications. We are looking for a UIView component that will display our map data. In the Search bar on the Library window type "UIView".
Notice that the components are filtered by what you type.
Click and drag the UIView object from the library window to the main window and drop it on top of the UIView that already exists. If you are successful in doing this your main window should look like:
Now, we need to tell the UIView what type of view it is. In this case it is an instance of AGSMapView.
To do this ensure the UIView object you just put in to the main window is highlighted.
Bring up the Identity Inspector by clicking Tools -> Identity Inspector.
In the class drop down type "AGSMapView" and hit enter. It should look like this:
Your main window will look like this. Notice the type of view has changed to AGSMapView.
Connecting between the view controller and the user interface
The last step is to hook up our AGSMapView in the nib file with the mapView outlet in the view controller.
In the main window click on the File's Owner and bring up the Connections Inspector window (Tools -> Connections Inspector)
Notice in the inspector there is an object called mapView. This corresponds to the outlet we created in Xcode.
In the Connections Inspector window click and drag the empty circle (far left) to the AGSMapView in the main Window.
The Connection Inspector should look like this:
Save your project in Interface Builder and return to Xcode.
You could now build and run the application, but the application will display a blank map. The next step is to add layers to the map to display some content.
Add layers to the map
Locate the view controllers viewDidLoad method. This method is executed when the user interface is displayed giving the view controller a chance to add some behaviour or tweek the display. We will create a Tiled layer that uses an ArcGIS Online map service, and add it to the map:
- (void)viewDidLoad {
[super viewDidLoad];
AGSTiledMapServiceLayer *tiledLayer =
[[AGSTiledMapServiceLayer alloc]
initWithURL:[NSURL URLWithString:@"http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"]];
[self.mapView addMapLayer:tiledLayer withName:@"Tiled Layer"];
[tiledLayer release];
}
Notice we release the instance of AGSTiledMapServiceLayer to avoid memory leaks.
Now if we run the application, the map should display contents from the ArcGIS Online map service.
Spatial References
The first layer with a valid spatial reference defines the spatial reference for the map. Dynamic ArcGIS Server map and image services as well as feature layers (FeatureLayer) will be reprojected to the map's spatial reference, if necessary. Tiled map services layers will not be reprojected - the spatial reference of the layer and map must match for the layer to display in the map.
Because the spatial reference cannot be changed once it has been defined, the map view's spatialReference property is read-only.