Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Created November 27, 2012 22:51
Show Gist options
  • Save ChrisRisner/4157744 to your computer and use it in GitHub Desktop.
Save ChrisRisner/4157744 to your computer and use it in GitHub Desktop.
ios day 19
#import <Foundation/Foundation.h>
#import "CoreLocation/CoreLocation.h"
@protocol CoreLocationControllerDelegate
@required
- (void)update:(CLLocation *)location;
- (void)locationError:(NSError *)error;
@end
@interface CoreLocationController : NSObject <CLLocationManagerDelegate>
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) id delegate;
@end
#import "CoreLocationController.h"
#import "CoreLocation/CoreLocation.h"
@implementation CoreLocationController
- (id)init {
self = [super init];
if(self != nil) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[self.delegate update:newLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
[self.delegate locationError:error];
}
@end
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import "CoreLocationController.h"
@interface ViewController : UIViewController <CoreLocationControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *lblLongitude;
@property (weak, nonatomic) IBOutlet UILabel *lblLatitude;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic, retain) CoreLocationController *locationController;
@end
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *lblLongitude;
@property (weak, nonatomic) IBOutlet UILabel *lblLatitude;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.showsUserLocation = YES;
self.locationController = [[CoreLocationController alloc] init];
self.locationController.delegate = self;
[self.locationController.locationManager startUpdatingLocation];
}
- (void)update:(CLLocation *)location {
self.lblLatitude.text= [NSString stringWithFormat:@"Latitude: %f", [location coordinate].latitude];
self.lblLongitude.text = [NSString stringWithFormat:@"Longitude: %f", [location coordinate].longitude];
}
- (void)locationError:(NSError *)error {
self.lblLatitude.text = [error description];
self.lblLongitude.text = nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.mapView.showsUserLocation = YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment