MKUserLocation blue userLocation MKAnnotation crashes the application if it is accidentally touched

I have MKMap with a series of MKAnnotations , all of which are red, which is great. I selected "show user location" in IB and change MKAnnotation from red to blue, I have the code in the viewForAnnotation method:

 if (annotation == theMap.userLocation) return nil; 

Everything is fine and the application is working fine, but if the user accidentally deletes the blue dot userlocation, I get the following crash:

 2012-02-01 20:43:47.527 AusReefNSW[27178:11603] -[MKUserLocationView setPinColor:]: unrecognized selector sent to instance 0x79b0720 2012-02-01 20:43:47.528 AusReefNSW[27178:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKUserLocationView setPinColor:]: unrecognized selector sent to instance 0x79b0720' *** First throw call stack: 

If I delete the above code, everything works fine, but the pin is red. I take on the blue icon, but have not yet discovered why the failure occurred. Any ideas would be good. Thanks.

DECIDE! Thanks to Marvin and heres the code if anyone finds this useful. In a nutshell, I had to first check if MKAnnotation is a MyAnnotation class or MKUserLocation class.

 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKPinAnnotationView *)view { theAnnotationSelected = [[mapView selectedAnnotations] objectAtIndex:0]; if ([theAnnotationSelected isKindOfClass:[MyAnnotation class]] ) { view.pinColor = MKPinAnnotationColorGreen; } - (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKPinAnnotationView *)view { if ([theAnnotationSelected isKindOfClass:[MyAnnotation class]] ) { view.pinColor = MKPinAnnotationColorRed; } 
+4
source share
4 answers

For the user's current location, go to the MKMapView property and make a selection in the "Show user location in XIB" section.

then execute MKMapViewDelegate in the controller .. and write this method in your controller

 -(MKAnnotationView *)mapView:(MKMapView *)pmapView viewForAnnotation:(id <MKAnnotation>)annotation { if ([annotation isKindOfClass:[MKUserLocation class]]) return nil; //return nil to use default blue dot view if([annotation isKindOfClass:[MKAnnotationClass class]]) { //Your code } } 

and this one

 - (void)mapView:(MKMapView *)mapView1 didSelectAnnotationView:(MKAnnotationView *)customAnnotationView { if(![annotation isKindOfClass:[MKUserLocation class]]) { //Your code } } 

With this, you can see the blue dot in the user's place.

+19
source

Do you want to check annotation class in

 -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ 

Then do the following.

 id *annotation = view.annotation; if (![annotation isKindOfClass:[MKUserLocation class]]) { //Normal Code here } 
+8
source

This solution works in iOS7 to suppress the presentation of the leader, but that does not stop the user from choosing the blue dot. How to suppress the current location of a map leader

I still need to find a way to disable the selection of the current point of location.

This is not so much a workaround as a “way to deal with it,” but I scale the map to focus on certain types of annotations when using it. If the user lands directly on top of one, scaling helps to establish some distance between the blue dot and my annotation, which makes it easier to use.

 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { if([view.annotation isKindOfClass:[SpecialClass class]]){ SpecialClass *cluster = (SpecialClass *)view.annotation; if(testCriteria){ [self.mapView setRegion:MKCoordinateRegionMakeWithDistance(cluster.coordinate, cluster.radius, cluster.radius) animated:YES]; } } 
+1
source

I had the same problem that when the user clicked the MKUserlocation blue dot, the application crashes. I am using DDAnnotation from Ching-Lan 'digdog' HUANG. The fix I found was

 -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ id annotation = view.annotation; if (![annotation isKindOfClass:[MKUserLocation class]]) { //Your Annotation Code here } } 
+1
source

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


All Articles