I have to set custom image for different pins. With my code I'm able to set custom color for each of them but when I try to set custom image instead of colors, it doesn't work.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MyAnnotation *)annotation {
static NSString *identifier = @"MyLocation";
if ([annotation isKindOfClass:[MyAnnotation class]]) {
MKPinAnnotationView *annotationView = (MKPinAnnotationView *)[self.myMap dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = NO;
NSString *stringType = [NSString stringWithFormat:@"%@", [(MyAnnotation *)annotationView.annotation stringType]];
if ([stringType isEqualToString:@"0"]) {
annotationView.image = [UIImage imageNamed:@"IconUserCerco.png"];
//annotationView.pinColor = MKPinAnnotationColorGreen;
} else if ([stringType isEqualToString:@"1"]) {
annotationView.image = [UIImage imageNamed:@"IconUserDefault.png"];
//annotationView.pinColor = MKPinAnnotationColorPurple;
} else if ([stringType isEqualToString:@"2"]) {
annotationView.image = [UIImage imageNamed:@"PinUniversity.png"];
//annotationView.pinColor = MKPinAnnotationColorRed;
}
return annotationView;
}
return nil;
}
Any help will be much appreciated. Thanks in advance.
Instead of MKPinAnnotationView
can you use MKAnnotationView
? You'll have to use following instead of MKPinAnnotationView
MKAnnotationView *annotationView = [self.myMap dequeueReusableAnnotationViewWithIdentifier:identifier];
iOS 9.0 onwards its recommended to use MKAnnotationView
. That should solve your problem.