MKUserLocation blue userLocation MKAnnotation causes app to crash if inadvertently touched
Asked Answered
W

4

5

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

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

All is good and the app works fine, but if the user inadvertently taps the blue userlocation dot 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 remove the above code, all works well but the pin is red. I perfer to have the blue icon but as yet have not discovered why the crash. Any ideas would be appreciated. Thanks.

SOLVED! Thanks Marvin and heres the code incase anyone finds it useful. In a nutshell, I had to first check to see if the MKAnnotation was of MyAnnotation Class or of 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;
}
Wolfsbane answered 1/2, 2012 at 9:46 Comment(0)
A
24

For Current User Location go to MKMapView property and make selection on Shows User Location in XIB.

then Implement MKMapViewDelegate in your 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

- (void)mapView:(MKMapView *)mapView1 didSelectAnnotationView:(MKAnnotationView *)customAnnotationView
{

    if(![annotation isKindOfClass:[MKUserLocation class]])
    {
          //Your code
    }

}

Using this you can see blue dot on User Location..

Albuquerque answered 1/2, 2012 at 9:55 Comment(1)
My problem was different, but ended up here. First part of this answer helped me. I was doing everything right via code it's just that my mapView object originating from storyboard did not have "Shows User Location" checked. Maybe this will help somebody else too!Onfroi
B
9

You will want to check the class of the annotation in

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

Then do the following..

id *annotation = view.annotation;
    if (![annotation isKindOfClass:[MKUserLocation class]]) {
//Normal Code here
}
Beaded answered 1/2, 2012 at 9:49 Comment(1)
id is already a pointer so id *annotation should be id annotation or id<MKAnnotation> annotation (no asterisk).Cosmic
C
1

This solution works in iOS7 to suppress the callout view, but it does not stop the user from selecting the blue dot. How to suppress the "Current Location" callout in map view

I have yet to find a way to disable selection of the current location dot.

It isn't so much a workaround as a "way to deal with it", but I zoom the map in to focus on certain types of annotations when tapped. If the user happens to be located right on top of one, the zooming helps put some distance between the blue dot and my annotation, making it easier to tap.

- (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];
    }
}
Cooke answered 1/2, 2014 at 18:30 Comment(0)
V
1

I had the same issue such that when a user would tap the MKUserlocation "blue dot" the application would crash. I am using DDAnnotation by 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
}
}
Vasilikivasilis answered 7/10, 2015 at 20:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.