问题描述
我可以创建 MKAnnotation
,还是只读?我有坐标,但我发现使用 setCoordinate
手动创建 MKAnnotation
并不容易。
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
.
想法?
推荐答案
是一种协议。因此,您需要编写自己的实现此协议的注释对象。所以你的MyAnnotation标题如下所示:
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;
您的实现看起来像(MyAnnotation.m):
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.
这篇关于iPhone:创建MKAnnotation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!