I am displaying different annotation points in a mapview. I want to display different titles and subtitles for all the different annotations. I have my viewForAnnotation delegate method implemented in the below manner.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
{
NSLog(@"annotation Class %@", [annotation class]);
if ([annotation isKindOfClass:[HGMovingAnnotation class]]) {
static NSString *kMovingAnnotationViewId = @"HGMovingAnnotationView";
HGMovingAnnotationView *annotationView = (HGMovingAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:kMovingAnnotationViewId];
if (!annotationView) {
annotationView = [[HGMovingAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kMovingAnnotationViewId];
}
//configure the annotation view
annotationView.image = [UIImage imageNamed:@"symbol-moving-annotation.png"];
annotationView.bounds = CGRectMake(0, 0, 20, 20); //initial bounds (default)
annotationView.mapView = mapView;
NSLog(@"Inside Moving Annotation");
return annotationView;
}
static NSString *kCustomAnnotationView = @"CustomAnnotationView";
MKPinAnnotationView* customPinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:kCustomAnnotationView];
if ([annotation isKindOfClass:[CLLocation class]]) {
MKAnnotationView *customAnnotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:kCustomAnnotationView];
customAnnotationView.image = [UIImage imageNamed:@"customAnnotation"];
customAnnotationView.canShowCallout = NO;
customAnnotationView.centerOffset = CGPointMake(2,-12);
NSLog(@"Inside Custom Annotation1");
return customAnnotationView;
} else {
customPinView.annotation = annotation;
customPinView.pinColor = MKPinAnnotationColorPurple;
NSLog(@"Inside Custom Annotation2");
return customPinView;
}
}
I want to display the title and subTitle for the customPinView Annotations. Please help me.
Annotation views do not have a title or subtitle. You need to subclass
MKAnnotationand set the (sub)title on the custom annotation. The viewForAnnotation method is not the right place for this, you should call[annotation setTitle:@"my title"]right before[mapView addAnnotation:annotation].Sample code: