Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Building Location-Based Applications for the iPhone : Page 2

Knowing where a device is opens the door to all kinds of innovative applications—and the iPhone's Core Location framework makes discovering location simple.




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

Obtaining Location Coordinates

Using Xcode, create a new View-based Application project and name it LBS. In the new project, double-click on the LBSViewController.xib file to edit it in Interface Builder.

Populate the View window with the following views (see Figure 1):

Figure 1. Example Location View: Populate the View window with TextField and Label views.
  • Label
  • TextField

Right-click on the Frameworks group in Xcode and select Add → Existing Frameworks.... Select Framework/CoreLocation.framework.

Add the statements in bold text in the following code to the LBSViewController.h file:

#import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface LBSViewController : UIViewController <CLLocationManagerDelegate> { IBOutlet UITextField *latitudeTextField; IBOutlet UITextField *longitudeTextField; IBOutlet UITextField *accuracyTextField; CLLocationManager *lm; } @property (retain, nonatomic) UITextField *latitudeTextField; @property (retain, nonatomic) UITextField *longitudeTextField; @property (retain, nonatomic) UITextField *accuracyTextField; @end

To use the CLLocationManager class, you need to implement the CLLocationManagerDelegate protocol in your view controller class. You also need to create three outlets that you'll connect to the three TextField views in the View window.

Back in Interface Builder, control-click and drag the File's Owner item to each of the three TextField views, then select latitudeTextField, longitudeTextField, and accuracyTextField, respectively.

Insert the statements shown in bold text below into the LBSViewController.m file:

#import "LBSViewController.h" @implementation LBSViewController @synthesize latitudeTextField, longitudeTextField, accuracyTextField; - (void) viewDidLoad { lm = [[CLLocationManager alloc] init]; if ([lm locationServicesEnabled]) { lm.delegate = self; lm.desiredAccuracy = kCLLocationAccuracyBest; lm.distanceFilter = 1000.0f; [lm startUpdatingLocation]; } } - (void) locationManager: (CLLocationManager *) manager didUpdateToLocation: (CLLocation *) newLocation fromLocation: (CLLocation *) oldLocation{ NSString *lat = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude]; latitudeTextField.text = lat; NSString *lng = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.longitude]; longitudeTextField.text = lng; NSString *acc = [[NSString alloc] initWithFormat:@"%g", newLocation.horizontalAccuracy]; accuracyTextField.text = acc; [acc release]; [lat release]; [lng release]; } - (void) locationManager: (CLLocationManager *) manager didFailWithError: (NSError *) error { NSString *msg = [[NSString alloc] initWithString:@"Error obtaining location"]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:msg delegate:nil cancelButtonTitle: @"Done" otherButtonTitles:nil]; [alert show]; [msg release]; [alert release]; } - (void) dealloc{ [lm release]; [latitudeTextField release]; [longitudeTextField release]; [accuracyTextField release]; [super dealloc]; }

The preceding code creates an instance of the CLLocationManager class when the View is loaded. Before using the object, you should check to see whether the user has enabled location services on the device. If so, you can then proceed to specify the desired accuracy using the desiredAccuracy property. Use the following constants to specify the accuracy that you want:

  • kCLLocationAccuracyBest
  • kCLLocationAccuracyNearestTenMeters
  • kCLLocationAccuracyHundredMeters
  • kCLLocationAccuracyKilometer
  • kCLLocationAccuracyThreeKilometers

You should note that while you're perfectly free to specify the best accuracy all the time, the actual accuracy—whatever you select—is not guaranteed. In addition, specifying a location with greater accuracy than you need takes up significant device time and battery power.

The distanceFilter property allows you to specify the distance a device must move laterally before it should generate a location update. The unit for this property is in meters, relative to its last position. If you want to be notified of all movements, use the kCLDistanceFilterNone constant. Finally, you start the location manager using the startUpdatingLocation method.

Figure 2. Location Test: When you test the sample application on the iPhone Simulator, you'll always get these (fixed) values.

To obtain location information, you need to handle two events:

  • locationManager:didUpdateToLocation:fromLocation:
  • locationManager:didFailWithError:

When a new location value is available, the device fires the locationManager:didUpdateToLocation:fromLocation: event. If the location manager cannot determine a location value, it will fire the locationManager:didFailWithError: event.

When the device can determine a location, you want to display its latitude, longitude, and accuracy, which you do using the CLLocation object. This object's horizontalAccuracy property specifies the radius of accuracy, in meters.

Press Command-r to test the application on the iPhone Simulator. Figure 2 shows the simulator displaying the latitude and longitude of the location returned. It also shows the accuracy of the result.

Author's Note: For the simulator, the device always reports a fixed position. There is no prize for guessing correctly where this position refers to.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date