V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
yishenggudou
V2EX  ›  iDev

有人用过mkannotation吗?本人菜鸟

  •  
  •   yishenggudou · 2012-02-25 13:51:36 +08:00 · 3590 次点击
    这是一个创建于 4445 天前的主题,其中的信息可能已经有所发展或是发生改变。
    想给mapview上添加一个annotation,所以创建一个class
    如下
    直接上代码
    .h文件
    #import <Foundation/Foundation.h>
    #import <MapKit/MapKit.h>


    @interface AdoptingAnAnnotation: NSObject {

    }



    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
    - (NSString *) title;
    - (NSString *) subtitle;
    @end


    .m文件


    #import "AdoptingAnAnnotation.h"

    @implementation AdoptingAnAnnotation

    @synthesize latitude;
    @synthesize longitude;

    - (id) initWithLatitude:(CLLocationDegrees) lat longitude:(CLLocationDegrees) lng {
    latitude = lat;
    longitude = lng;
    return self;
    }
    - (CLLocationCoordinate2D) coordinate {
    CLLocationCoordinate2D coord = {self.latitude, self.longitude};
    return coord;
    }
    - (NSString *) title {
    return @"217 2nd St";
    }
    - (NSString *) subtitle {
    return @"San Francisco CA 94105";
    }
    @end

    老是报错,不知是oc语法问题还是 class用错了
    本人菜鸟..欢迎拍砖
    9 条回复    1970-01-01 08:00:00 +08:00
    ultragtx
        1
    ultragtx  
       2012-02-25 14:09:54 +08:00
    .h
    @interface AdoptingAnAnnotation: NSObject <MKAnnotation> {} // 加上MKAnnotation 的protocol

    @property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

    @property (nonatomic, readonly, copy) NSString *title;
    @property (nonatomic, readonly, copy) NSString *subtitle;

    // title 和 subtitle按你之前的写也没啥事

    @end

    .m

    @synthesize coordinate; // @synthesize 是跟 @property 配合的 详细去看文档 Advanced Memory Management 貌似叫这个
    @synthesize title, subtitle;

    - (id) initWithLatitude:(CLLocationDegrees) lat longitude:(CLLocationDegrees) lng {
    self = [super init]
    if (self) {
    coordinate.latitude = lat;
    coordinate.longitude = lng;
    }
    return self;
    }

    - (NSString *) title {
    return @"217 2nd St";
    }

    - (NSString *) subtitle {
    return @"San Francisco CA 94105";
    }

    @end

    // 这样差不多了吧

    新手就看文档 多参考文档中的sample project 多看看xxxxx Guide
    另外先把objc的基本内容弄明白
    ultragtx
        2
    ultragtx  
       2012-02-25 14:11:15 +08:00
    话说v2ex里会不会有用户名为property end synthesize implementation的人在啊
    yishenggudou
        3
    yishenggudou  
    OP
       2012-02-25 14:24:33 +08:00
    @ultragtx 额.... 可以赶紧抢注一个
    yishenggudou
        4
    yishenggudou  
    OP
       2012-02-25 14:31:53 +08:00
    @ultragtx 谢谢鼓励 原来这个叫 Advanced Memory Management
    yishenggudou
        5
    yishenggudou  
    OP
       2012-02-25 15:01:18 +08:00
    @ultragtx 再请教下,这个class写好之后 怎么加到Mapview上?
    map addAnnotation: 这个后面怎么调用这个类?
    ultragtx
        6
    ultragtx  
       2012-02-25 15:06:46 +08:00
    AdoptingAnAnnotation *annotation = [[AdoptingAnAnnotation alloc] initWithLatitude:lat longitude:lon];

    [mapView addAnnotation:annotation];
    yishenggudou
        7
    yishenggudou  
    OP
       2012-02-25 15:15:30 +08:00
    @ultragtx 谢谢..前面这个AdoptingAnAnnotation 是根据类名来把,类名叫什么就是什么? 我发现我真的太菜了 sigh...
    ultragtx
        8
    ultragtx  
       2012-02-25 15:23:02 +08:00
    刚开始都这样 硬着头皮上 写多了自然就会了
    yishenggudou
        9
    yishenggudou  
    OP
       2012-02-25 15:34:27 +08:00
    @ultragtx 恩 ...谢谢鼓励
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   932 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:37 · PVG 06:37 · LAX 15:37 · JFK 18:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.