Show current user location using MKMapView?

I am trying to show the current location of the user by setting the MKMapView ie setShowsUserLocation to YES . By default, an arrow appears in the upper left corner of the iPhone screen when the application starts updating the user's location. But after displaying the current location, the arrow should disappear, but it is still present while the application is running, which means that the application still remains updating the location in the background? so how can i stop updating the current location? I have implemented a delegate that is immediately called.

+7
source share
3 answers

If the map is visible and has showUserLocation installed on YES, it continues to update in the background.

You need to undo this when the view disappears or when the application goes into the background. The best way would probably be to register your viewController to receive notifications 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 display properties relative to userLocation :

 - (void)appToBackground{ [mapview setShowsUserLocation:NO]; } 

AND

 - (void)appReturnsActive{ [mapview setShowsUserLocation:YES]; } 

Make sure that these methods are actually called by setting a breakpoint there and returning to the main screen.

+15
source

I agree with Michael Kernakhan, I am sure that there is a race condition inside MKMapView .

MKMapView appears to try to remove userLocationAnnotation (the blue dot) by itself as soon as he realizes that he has lost access to location services.

However, this does not seem secure, and if the developer also issues [mapView setShowsUserLocation:NO] , he will most likely crash due to the race between the MKMapView internal thread trying to remove userLocationAnnotation and the thread that calls setShowsUserLocation:NO .

+1
source

You can stop updating the location by sending a message to the location manager [self.locationManager stopUpdatingLocation];

As for your case, you tried to stop showing the location of users?

0
source

All Articles