I am trying to show current location of user by setting the property of MKMapView
i.e setShowsUserLocation
to YES
. As by default an arrow appears on the top left of iPhone screen when the application starts updating the user location.But after showing the current location the arrow should disappear but its still present as long as the app is running which would mean the app is still updating the location in the background? so how can i stop updating the current location? i have implemented the delegate which gets called up immediately..
If the map is visible and has showUserLocation set to YES, it continues to update in the background.
You need to unset this when the view disappears or when the Application goes to background. The best way would probably be to register your viewController to be notified for UIApplicationDidEnterBackgroundNotification
and UIApplicationDidBecomeActiveNotification
.
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appToBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appReturnsActive) name:UIApplicationDidBecomeActiveNotification object:nil];
}
Then in the method called by this notification, change the map view properties regarding userLocation
:
- (void)appToBackground{
[mapview setShowsUserLocation:NO];
}
And
- (void)appReturnsActive{
[mapview setShowsUserLocation:YES];
}
Check that these methods are indeed called by setting a breakpoint there and returning to the home screen.
I agree with Michael Kernahan, I'm pretty sure there is a race condition inside MKMapView
.
MKMapView
appears to try to remove the userLocationAnnotation
(the blue dot) by itself as soon as it realises it lost access to location services.
However, that does not seems protected and if the developer also issues a [mapView setShowsUserLocation:NO]
, it is very likely to get a crash because of the race between MKMapView
's internal thread trying to remove the userLocationAnnotation
, and the thread that is calling setShowsUserLocation:NO
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With