我能创建一个MKAnnotation
吗,还是只读的?我有坐标,但手动使用setCoordinate
创建MKAnnotation
并不容易。
有什么想法吗?
我能创建一个MKAnnotation
吗,还是只读的?我有坐标,但手动使用setCoordinate
创建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];
如果你想在注释气泡上添加标题和副标题,需要添加title和subtitle属性。在iPhone OS 4中,有一个新的类MKPointAnnotation,它是MKAnnotation协议的具体实现。
请查看苹果MapCallouts项目。你需要的一切都在那个文件中: http://developer.apple.com/iphone/library/samplecode/MapCallouts/Introduction/Intro.html