Created
January 14, 2011 16:31
-
-
Save nickcharlton/779831 to your computer and use it in GitHub Desktop.
An example using iOS's location services.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LocationServices.h | |
#import <Foundation/Foundation.h> | |
#import <CoreLocation/CoreLocation.h> | |
@interface LocationServices : NSObject <CLLocationManagerDelegate> { | |
CLLocationManager *locationManager; | |
CLLocation *currentLocation; | |
} | |
@property (nonatomic, retain) CLLocationManager *locationManager; | |
@property (nonatomic, retain) CLLocation *currentLocation; | |
- (void)startLocationServices; | |
@end | |
// LocationServices.m | |
#import "LocationServices.h" | |
@implementation LocationServices | |
@synthesize locationManager, currentLocation; | |
- (void)startLocationServices { | |
locationManager = [[CLLocationManager alloc] init]; | |
locationManager.delegate = self; | |
if ([CLLocationManager locationServicesEnabled]) { | |
[locationManager startUpdatingLocation]; | |
} else { | |
NSLog(@"Location services is not enabled"); | |
} | |
} | |
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { | |
self.currentLocation = newLocation; | |
NSLog(@"Latidude %@ Longitude: %@", newLocation.coordinate.latitude, newLocation.coordinate.longitude); | |
} | |
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { | |
[locationManager stopUpdatingLocation]; | |
NSLog(@"Update failed with error: %@", error); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment