【发布时间】:2010-05-21 00:09:12
【问题描述】:
我可以创建一个MKAnnotation,还是只读的?我有坐标,但我发现使用setCoordinate 手动创建MKAnnotation 并不容易。
想法?
【问题讨论】:
标签: iphone mkmapview mapkit mkannotation
我可以创建一个MKAnnotation,还是只读的?我有坐标,但我发现使用setCoordinate 手动创建MKAnnotation 并不容易。
想法?
【问题讨论】:
标签: iphone mkmapview mapkit mkannotation
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];
如果注释标注上没有标题和副标题,则需要添加标题和副标题属性。
【讨论】:
在 iPhone OS 4 中有一个新的类 MKPointAnnotation,它是 MKAnnotation 协议的具体实现。
【讨论】:
检查苹果 MapCallouts 项目。您需要的一切都在该文件中: http://developer.apple.com/iphone/library/samplecode/MapCallouts/Introduction/Intro.html
【讨论】: