First, define a custom, not just a title and subtitles. We want to resize MKAnnotation and include some custom graphics.
There are two parts to the annotation you can customize:
- MKaNnotation
- MKAnnotationView
MKAnnotation , . MKMapView, addAnnotation: in viewDidLoad, .
MKAnnotation
@interface CPAnnotation : NSObject <MKAnnotation> {
@private
CLLocationCoordinate2D _coordinate;
NSString *_title;
NSString *_subtitle;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
@end
MKAnnotation
@implementation CPAnnotation
@synthesize coordinate = _coordinate;
@synthesize title = _title;
@synthesize subtitle = _subtitle;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate {
self = [super init];
if (self != nil) {
self.coordinate = coordinate;
}
return self;
}
- (NSString *)title {
return _title;
}
- (NSString *)subtitle {
return _subtitle;
}
@end
. MKAnnotationView. Apple, . , , . . leftCalloutAccessoryView rightCalloutAccessoryView. MKMapViewDelegate MKMapView, viewForAnnotation:.
MKAnnotationView MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
static NSString *const kAnnotationReuseIdentifier = @"CPAnnotationView";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:kAnnotationReuseIdentifier];
if (annotationView == nil) {
annotationView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:kAnnotationReuseIdentifier] autorelease];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeInfoLight];
}
return annotationView;
}
, drawRect, , MKAnnotationView XIB. .
Apple WeatherAnnotationView drawRect.