iPhone: Create MKAnnotation

谁说我不能喝 提交于 2019-11-26 16:09:07

问题


Can I create an MKAnnotation, or is it read only? I have coordinates, but I am not finding it easy to manually create an MKAnnotation with using setCoordinate.

Ideas?


回答1:


MKAnnotation is a protocol. So you need to write your own annotation object that implements this protocol. So your MyAnnotation header looks like:

@interface MyAnnotation : NSObject<MKAnnotation> {
    CLLocationCoordinate2D coordinate;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

// add an init method so you can set the coordinate property on startup
- (id) initWithCoordinate:(CLLocationCoordinate2D)coord;

and your implementation looks like (MyAnnotation.m):

- (id) initWithCoordinate:(CLLocationCoordinate2D)coord
{
    coordinate = coord;
    return self;
}

So to add your annotation to the map:

MyAnnotation * annotation = [[[MyAnnotation alloc] initWithCoordinate:coordinate] autorelease];
[self.mapView addAnnotation:annotation];

If you wan't a title and subtitle on the annotation callout, you need to add the title and subtitle properties.




回答2:


In iPhone OS 4 there is a new class MKPointAnnotation which is a concrete implementation of the MKAnnotation protocol.




回答3:


Check apple MapCallouts project. Everything you need is in that file: http://developer.apple.com/iphone/library/samplecode/MapCallouts/Introduction/Intro.html



来源:https://stackoverflow.com/questions/2878758/iphone-create-mkannotation

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