This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Chapter 24 iPhone Location API Having location available on the phone changes your experience while traveling. You probably expect that when youre in a major metropolitan area, there is a great museum close to where you are. If instead you are in the middle of a remote part of the world on a hike, there probably isnt a museum nearby, but there might be an amazing spot to take photos. The Core Location service is part of every iPhone, so our iPhone knows where we are and can therefore help us not miss the best museums or the most amazing sites. Instead of looking in a book that has places around famous spots, we have a device that can say you are here and then find great spots near where we are. As with the other iPhone features, its important that you stop to think about how best to take advantage of Core Location. Once youve decided what you want to do with location, youll find that its surprisingly easy to use the Core Location service on the iPhone. With just a handful of lines of code, we can fire up the location-oriented hardware and start getting updates about the location of the device. Lets get started. 24.1 Knowing Where Youll use the CLLocationManager to turn the location services off and on and to specify the level of service you need. Lets build an exam- ple application that simply displays the location as reported by Core Location. To get started, create a new view-based project called LocationDisplay . You will also need to add the Core Location framework to the project. Prepared exclusively for Joseph Camp KNOWING WHERE 454 Location Managers Delegate In a typical application, youd want the delegate object to be the controller that will be interacting with whatever model you have to keep track of the applications location data. In this example, we dont have a separate model, so we are sim- ply going to have our view controller be the delegate. In a more complex application, you might want to do something like keep a running weighted average of the location data over time (as an example). The controller that manages the weighted average of the data would be a great candidate to be the delegate. To do that, select the target for your project, and click D- i . At the bottom of the General tab, click the + button, and then choose the Core Location framework. In LocationDisplayViewController.h , declare a property named locationMan- ager that is of type CLLocationManager . You also need to add the import for the Core Location header and the declaration that this class imple- ments the CLLocationManagerDelegate protocol. The next step is to create a new Core Location CLLocationManager object in LocationDisplayViewController s viewDidLoad method and set the dele- gate to the view controller. The delegate does not have to be a view controller; we could use any object as long as it implements the dele- gate protocol, but it is often convenient. After we create the manager and set its delegate, we call the...
View Full Document
- Fall '10