Search code examples
iphonemkmapviewmkannotationviewmkpinannotationview

MKPinAnnotationView versus MKAnnotationView


I've inherited a project which throws this warning

Incompatible pointer types assigning to 'MKPinAnnotationView *' from 'MKAnnotationView *'

at this line

pinView=[[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinID]autorelease];
    }

I'd like to return the project without warnings so I'm hoping someone here has a quick answer

Full code:

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation: (id  <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil; 

    NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];

    if(annotation != mapView.userLocation) 

    {

        static NSString *defaultPinID = @"com.invasivecode.pin";

        pinView = (MKPinAnnotationView *)[mapView  dequeueReusableAnnotationViewWithIdentifier:defaultPinID];


        if (!pinView) {
            pinView=[[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinID]autorelease];
        }

    }   
    pinView.animatesDrop=YES;
    [mapView.userLocation setTitle:@"I am here"];
    [mapView.userLocation setSubtitle:[prefs objectForKey:@"CurrentLocationName"]];
    return pinView;        
}

Thanks!


Solution

  • The pinView variable is declared as an MKPinAnnotationView but that line creates an MKAnnotationView.

    Change this line:

    pinView=[[[MKAnnotationView alloc]initWithAnnotation...
    

    to:

    pinView=[[[MKPinAnnotationView alloc]initWithAnnotation...
    


    You should also have an else part to that if to handle annotation view re-use:

    else
        pinView.annotation = annotation;