Skip to content

Instantly share code, notes, and snippets.

@myell0w
Created November 17, 2011 12:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myell0w/1373050 to your computer and use it in GitHub Desktop.
Save myell0w/1373050 to your computer and use it in GitHub Desktop.
MTLocation How-To
// 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