我正在使用MKUserTrackingBarButtonItem来跟踪用户。使用用户跟踪栏按钮时,我无法设置缩放。所以我试着放大屏幕.并且应用程序在几个步骤后崩溃。我附近有一个坐标,我正在尝试使用它来跟踪用户相对于坐标。为什么应用程序崩溃。有没有更好的方法可以做到这一点?任何帮助将不胜感激。这是我的代码:-
-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.mapView.delegate = self;
_coordinate1.latitude = 29.9431438;
_coordinate1.longitude = -95.5170326;
self.mapView.showsUserLocation = YES; }
-(void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
MKCoordinateRegion region = [self regionFromLocations:self.currentLoc];
[self displayPoints];
MKCoordinateRegion adjustRegion = [self.mapView regionThatFits:region];
self.mapView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self.mapView setRegion:adjustRegion animated:YES]; }
- (void)viewDidLoad {
[super viewDidLoad];
MKUserTrackingBarButtonItem *locateMeButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:self.mapView];
NSArray *toolbarItems = [NSArray arrayWithObjects: locateMeButton,
nil];
self.toolBar.items = toolbarItems; }
- (void)viewWillDisappear:(BOOL)animated {
NSLog(@"viewwillDisapear");
[self.mapView setUserTrackingMode:MKUserTrackingModeNone];
[super viewWillDisappear:animated];}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
// getting current location
self.currentLoc = userLocation.location.coordinate;
NSLog(@"loc- %f %f", self.currentLoc.longitude, self.currentLoc.latitude); }
通过在视图消失时删除地图视图来解决问题
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.mapView.delegate = nil;
[self.mapView removeFromSuperview];
}