【发布时间】:2011-06-27 15:44:45
【问题描述】:
有MKAnnotation 的简单示例项目吗?我不知道在“addAnnotation”要传递什么,因为它想要一些“id<Annotation>”。
开发者网站上的示例都是数组或解析器左右,我只需要一个非常简单的示例来首先了解整个事情是如何工作的。
提前谢谢..
编辑:
我开设了一个班级Pin。在 .h 中它写道
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Pin : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subTitle;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly) NSString *title;
@property (nonatomic, readonly) NSString *subTitle;
- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description;
@end
根据 onnoweb 的回答。
在Pin.m中,我是这样实现的,根据不同的例子:
#import "Pin.h"
@implementation Pin
@synthesize title, subTitle;
- (CLLocationCoordinate2D) coordinate {
CLLocationCoordinate2D coords;
coords.latitude = coordinate.latitude; //i want it that way
coords.longitude = 7.1352260; //this way it works
return coords;
- (NSString *) title {
return @"Thats the title";
}
- (NSString *) subtitle {
return @"Thats the sub";
}
- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description {
return self;
}
- (void) dealloc {
[super dealloc];
}
@end
所以如果我手动设置坐标,正如评论中所指出的,它可以工作。但我希望能够像在第一条评论中那样动态设置值。我尝试了各种方法,但都没有奏效。由于您没有指定- (CLLocationCoordinate2D) coordinate,因此我尝试仅合成coordinate,但这样也行不通。
你能给我看看你的 MapPin.m 吗?
编辑 2:
我设置mapCenter喜欢
- (void)viewWillAppear:(BOOL)animated {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
CLLocation *curPos = locationManager.location;
location = curPos.coordinate;
CLLocationCoordinate2D mapCenter;
mapCenter.latitude = location.latitude;
mapCenter.longitude = location.longitude;
MKCoordinateSpan mapSpan;
mapSpan.latitudeDelta = 0.005;
mapSpan.longitudeDelta = 0.005;
MKCoordinateRegion mapRegion;
mapRegion.center = mapCenter;
mapRegion.span = mapSpan;
mapKitView.region = mapRegion;
mapKitView.mapType = MKMapTypeStandard;
mapKitView.showsUserLocation=TRUE;
}
这有什么问题吗?
【问题讨论】:
标签: iphone objective-c mkannotation