【问题标题】:iPhone: Create MKAnnotationiPhone:创建 MKAnnotation
【发布时间】:2010-05-21 00:09:12
【问题描述】:

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

想法?

【问题讨论】:

    标签: iphone mkmapview mapkit mkannotation


    【解决方案1】:

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

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

    【讨论】:

    • 这正是我所需要的。我应该把 - (id) initWithCoordinate:(CLLocationCoordinate2D)coord;方法?在我的 .h 文件或 .m 中?
    • 没问题。我已经用更多细节更新了答案:)
    • 由于某种原因,我在尝试构建和运行时没有找到“-initWithCoord:”方法。
    • 对不起。我的示例代码是 initWithCoord,它应该是 initWithCoordinate。
    • 如何添加注解图片?
    【解决方案2】:

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

    【讨论】:

    • 如果您只需要基本注释,这将非常方便。谢谢!
    • MKPointAnnotation* pointAnnotation = [[MKPointAnnotation alloc] init]; pointAnnotation.coordinate = location.coordinate; [mkMapView addAnnotation:pointAnnotation];
    【解决方案3】:

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

    【讨论】:

    • 太棒了,正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    相关资源
    最近更新 更多