有人用过mkannotation吗?本人菜鸟

2012-02-25 13:51:36 +08:00
 yishenggudou
想给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用错了
本人菜鸟..欢迎拍砖
3598 次点击
所在节点    iDev
9 条回复
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
2012-02-25 14:11:15 +08:00
话说v2ex里会不会有用户名为property end synthesize implementation的人在啊
yishenggudou
2012-02-25 14:24:33 +08:00
@ultragtx 额.... 可以赶紧抢注一个
yishenggudou
2012-02-25 14:31:53 +08:00
@ultragtx 谢谢鼓励 原来这个叫 Advanced Memory Management
yishenggudou
2012-02-25 15:01:18 +08:00
@ultragtx 再请教下,这个class写好之后 怎么加到Mapview上?
map addAnnotation: 这个后面怎么调用这个类?
ultragtx
2012-02-25 15:06:46 +08:00
AdoptingAnAnnotation *annotation = [[AdoptingAnAnnotation alloc] initWithLatitude:lat longitude:lon];

[mapView addAnnotation:annotation];
yishenggudou
2012-02-25 15:15:30 +08:00
@ultragtx 谢谢..前面这个AdoptingAnAnnotation 是根据类名来把,类名叫什么就是什么? 我发现我真的太菜了 sigh...
ultragtx
2012-02-25 15:23:02 +08:00
刚开始都这样 硬着头皮上 写多了自然就会了
yishenggudou
2012-02-25 15:34:27 +08:00
@ultragtx 恩 ...谢谢鼓励

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/28132

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX