MKAnnotation, simple example

后端 未结 1 366
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 03:18

Is there a simple example project for MKAnnotation? I don\'t know what to pass at \"addAnnotation\", as it wants some \"id<

相关标签:
1条回答
  • 2020-12-03 04:08

    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.

    0 讨论(0)
提交回复
热议问题