This is a bug with MKMapKitDelegate mapView: didUpdateUserLocation?

I created a test application with only one view containing MKMapView and a controller that acts as a MapView delegate.

When I make a new assembly (completely removed from the device before reinstalling) and register callbacks, I see that mapView:didUpdateUserLocation is called twice before the user indicates whether they want to show their current location or not.

MKUserLocation objects passed to the callback are not valid:

 2012-03-13 08:20:17.518 MapTest[3325:707] Did update user location: 0.000000,0.000000 2012-03-13 08:20:17.581 MapTest[3325:707] Did update user location: 0.000000,0.000000 

Is this the expected behavior for MKMapKit or error?

Update

I run this on my iPhone 4, not a simulator. Here is the controller code:

 #import "ViewController.h" @implementation ViewController @synthesize mapView; - (void)viewDidLoad { [super viewDidLoad]; self.mapView.showsUserLocation = YES; self.mapView.delegate = self; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } -(IBAction)trackButtonTapped:(id)sender { self.mapView.showsUserLocation = !self.mapView.showsUserLocation; } #pragma mark - MKMapKitDelegate -(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { NSLog(@"Did update user location: %f,%f", userLocation.coordinate.latitude, userLocation.coordinate.longitude); } -(void)mapViewWillStartLoadingMap:(MKMapView *)mapView { NSLog(@"Will start loading map"); } -(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView { NSLog(@"Did finish loading map"); } -(void)mapViewWillStartLocatingUser:(MKMapView *)mapView { NSLog(@"Will start locating user"); } -(void)mapViewDidStopLocatingUser:(MKMapView *)mapView { NSLog(@"Did stop locating user"); } -(void)mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error { NSLog(@"Did fail loading map"); } -(void)mapView:(MKMapView *)mapView didFailToLocateUserWithError:(NSError *)error { if (error.code == kCLErrorDenied){ NSLog(@"User refused location services"); } else { NSLog(@"Did fail to locate user with error: %@", error.description); } } @end 
+6
source share
4 answers

My opinion is that this is a mistake.

How can you see that the map location was updated when the user did not even grant permission?

The workaround I'm using is to check if userLocation.location nil :

 - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { if (userLocation.location == nil) return; //do something with userLocation... } 
+10
source

instead of checking for nil userlocation (where 0,0 coordinates can really be valid), you should use:

 if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) { // do something with the coords } else { // the coords are invalid } 
+5
source

With "self.mapView.showsUserLocation = YES;" in viewDidLoad, it will start trying to get the user's location right away. If you do, he will wait for the user to click the button.

0
source

Maybe this helps someone. For me, the β€œproblem” was that I also installed an interface constructor for the user interface. Removing it here and setting it up with code made the delegate methods work as suspects.

0
source

Source: https://habr.com/ru/post/910573/


All Articles