Now you have the second view added in the last lesson, you can add a map to show where the mountain is located. You can use the longitude and latitude that are part of the MountainItem data passed to the second view.
Detail View Screen with MapView
To work with a map, you need to include the MapKit framework. There are frameworks included automatically with a new IOS XCode project. They are UIKit, Foundation and CoreGraphics. You have probably noticed the import statement for UIKit in the header for MainViewController and DetailViewController.
In XCode 4 a group called Frameworks contains the frameworks you include with your project.
Typical Frameworks Group
It is a good practice to assure any new ones you add are in that group for easy reference.
Step 1: Add the MapKit Framework
Download and uncompress the Starting XCode Project file and open in XCode.
In the project explorer select the top node.
Select Project Node
In the center of XCode follow these steps:
Steps to Open Frameworks and Libraries Dialog
Type map into the search text field and you should see the list narrow down to the MapKit.framework choice and then select the Add button.
Frameworks and Libraries Dialog
Locate the MapKit.framework file in the project explorer and drag it into the Frameworks group. You could leave MapKit.framework where it was grouped, but keeping the frameworks together makes sense.
Drag MapKit Framework To Frameworks Group
Step 2: DetailViewController.h – Replace TextView with MapView
Open the DetailViewController.h file in the project explorer.
You are going to replace the UITextView with a MKMapView. Lines that are being replaced are included here for convenience.
The standard map type is set on line 59. You could also use MKMapTypeSatellite and MKMapTypeHybrid if you want to experiment.
Lines 60 and 61 are self explanatory.
The zoom and center point of the map is done on lines 62 to 67. Then center point is the longitude and latitude data passed into the class in the mountainItem object.
The span is an offset for the zoom. You can experiment with positive float numbers such as .01 which will zoom in tight. Because many of the mountains do not have any feature in standard map view, the value of 1 seems to show enough map details to keep from being disoriented.
Step 5: DetailViewController.xib – Replace the TextView with the MapView
Open the DetailViewController.xib.
Delete the TextView in the design window.
Delete TextView
Drag the MapView from the Objects panel to the view in the design window and fill the view.
Drag MapView
With the MapView still selected open the Connections Inspector and drag a “New Referencing Outlet” to the File’s Owner and when you release the mouse select mapView, the name your are using in our DetailViewController to control the MapView. The Connections Inspector should look as follows:
Connections Inspector For MapView
Then finally as a double check select the File’s Owner and the Connections Inspector should appear as follows:
Connections Inspector for File’s Owner
You should be good to check this out in the Simulator. Remember you need to hold down the Option button and drag the mouse to get the multi touch over the map for zooming. Scroll the map by dragging the mouse.