MKPinAnnotationView vs MKAnnotationView

I inherited a project that throws this warning

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

in this line

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

I want to return the project without warning, so I hope someone 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;        
}

Thank!

+3
source share
2 answers

The variable is pinViewdeclared as MKPinAnnotationView, but this line creates MKAnnotationView.

Change this line:

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

at

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


You should also have a part elsefor this ifto handle reuse of annotations:

else
    pinView.annotation = annotation;
+3
source

pinview, !!! , . , .

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation: (id  <MKAnnotation>)annotation {


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


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

..........
..........

}
+2

All Articles