有人用过mkannotation吗?本人菜鸟 - V2EX
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 4035 次点击
    这是一个创建于 5002 天前的主题,其中的信息可能已经有所发展或是发生改变。
    想给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     Solana     1249 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 17:23 PVG 01:23 LAX 09:23 JFK 12:23
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86