iPhone:创建MKAnnotation

Nic*_*ard 15 iphone mapkit mkmapview mkannotation

我可以创建一个MKAnnotation,还是只读?我有坐标,但我发现手动创建MKAnnotation使用时并不容易setCoordinate.

想法?

Red*_*ing 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;
Run Code Online (Sandbox Code Playgroud)

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

- (id) initWithCoordinate:(CLLocationCoordinate2D)coord
{
    coordinate = coord;
    return self;
}
Run Code Online (Sandbox Code Playgroud)

所以要将注释添加到地图中:

MyAnnotation * annotation = [[[MyAnnotation alloc] initWithCoordinate:coordinate] autorelease];
[self.mapView addAnnotation:annotation];
Run Code Online (Sandbox Code Playgroud)

如果您不想在注释标注上添加标题和副标题,则需要添加标题和副标题属性.


jos*_*hrl 32

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

  • MKPointAnnotation* pointAnnotation = [[MKPointAnnotation alloc] init]; pointAnnotation.coordinate = location.coordinate; [mkMapView addAnnotation:pointAnnotation]; (4认同)

ale*_*ash 9

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