MKAnnotation, simple example

为君一笑 提交于 2019-11-27 12:50:27
onnoweb

You need to have a class implementing the MKAnnotation protocol. Here is a snippet from one of my projects:

@interface MapPin : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly) NSString *title;
@property (nonatomic, readonly) NSString *subtitle;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description;

@end

Then, where you create the map you'll call:

pin = [[MapPin alloc] initWithCoordinates:[track startCoordinates] placeName:@"Start" description:@""];
[map addAnnotation:pin];

EDIT:

Here is the implementation:

@implementation MapPin

@synthesize coordinate;
@synthesize title;
@synthesize subtitle;

- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:placeName description:description {
    self = [super init];
    if (self != nil) {
        coordinate = location;
        title = placeName;
        [title retain];
        subtitle = description;
        [subtitle retain];
    }
    return self;
}

- (void)dealloc {
    [title release];
    [subtitle release];
    [super dealloc];
}


@end

Hope this helps, Onno.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!