Skip to content

Instantly share code, notes, and snippets.

@nickcharlton
Created January 14, 2011 16:31
Show Gist options
  • Save nickcharlton/779831 to your computer and use it in GitHub Desktop.
Save nickcharlton/779831 to your computer and use it in GitHub Desktop.
An example using iOS's location services.
// 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