Created
November 17, 2011 12:41
-
-
Save myell0w/1373050 to your computer and use it in GitHub Desktop.
MTLocation How-To
This file contains hidden or 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
// MTLocation How-To | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// 1. Create MapView | |
self.mapView = [MKMapView mapViewInSuperview:self.view]; | |
// 2. Create BarButtonItem | |
self.locationItem = [MTLocateMeBarButtonItem userTrackingBarButtonItemForMapView:self.mapView]; // @property (nonatomic, strong) UIBarButtonItem *locationItem; | |
self.navigationItem.rightBarButtonItem = self.locationItem; | |
// 3. Configure MTLocationDelegate | |
[MTLocationManager sharedInstance].mapView = self.mapView; | |
} | |
- (void)viewWillAppear:(BOOL)animated { | |
[super viewWillAppear:animated]; | |
self.mapView.showsUserLocation = YES; | |
[self.locationItem startListeningToLocationUpdates]; | |
[self.locationItem setFrameForInterfaceOrientation:$appOrientation]; | |
// begin listening to end-heading notifications | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(locationManagerDidStopUpdatingHeading:) | |
name:kMTLocationManagerDidStopUpdatingHeading | |
object:nil]; | |
} | |
- (void)viewWillDisappear:(BOOL)animated { | |
[super viewWillDisappear:animated]; | |
[[MTLocationManager sharedInstance] stopAllServices]; | |
[self.locationItem stopListeningToLocationUpdates]; | |
self.mapView.showsUserLocation = NO; | |
// end listening to location update notifications | |
[[NSNotificationCenter defaultCenter] removeObserver:self name:kMTLocationManagerDidStopUpdatingHeading object:nil]; | |
} | |
- (void)locationManagerDidStopUpdatingHeading:(NSNotification *)notification { | |
// rotate map back to Identity-Transformation | |
[self.mapView resetHeadingRotationAnimated:YES]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment