15

我可以创建一个MKAnnotation,还是只读的?我有坐标,但我发现手动创建MKAnnotationwith using并不容易setCoordinate

想法?

4

3 回答 3

40

MKAnnotation是一个协议。所以你需要编写自己的注解对象来实现这个协议。所以你的 MyAnnotation 标头看起来像:

@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;

你的实现看起来像(MyAnnotation.m):

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

因此,要将注释添加到地图:

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

如果注释标注上没有标题和副标题,则需要添加标题和副标题属性。

于 2010-05-21T00:21:54.503 回答
32

在 iPhone OS 4 中有一个新的类 MKPointAnnotation,它是 MKAnnotation 协议的具体实现。

于 2010-06-24T11:10:44.780 回答
9

检查苹果 MapCallouts 项目。您需要的一切都在该文件中:http: //developer.apple.com/iphone/library/samplecode/MapCallouts/Introduction/Intro.html

于 2010-06-03T01:14:49.450 回答