Wednesday 11 May 2011

location manager

Code samples for using the iPhone location API.

in the header:


#import CoreLocation/CoreLocation.h
@interface FugitiveDetailViewController : UIViewController<CLLocationManagerDelegate> {



in the main:



- (void)viewWillAppear:(BOOL)animated {

self.locationManager=[[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}

// Implement these 2 methods:
-(void) locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*) oldLocation {
NSLog(@"Core location has position!");
capturedToggle.enabled=YES;
}
-(void) locationManager:(CLLocationManager*)manager didFailWithError:(NSError*)error {
NSLog(@"Core location has NO position :-(");
capturedToggle.enabled=NO;
}

//to read location:
CLLocation *curPos = self.locationManager.location;
latitude = [NSNumber numberWithDouble:curPos.coordinate.latitude];
longitude = [NSNumber numberWithDouble:curPos.coordinate.longitude];

No comments:

Post a Comment