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;
}
有什么问题吗?
答案 0 :(得分:36)
您需要有一个实现MKAnnotation
协议的类。以下是我的一个项目的片段:
@interface MapPin : 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
然后,在您创建地图的地方,您将调用:
pin = [[MapPin alloc] initWithCoordinates:[track startCoordinates] placeName:@"Start" description:@""];
[map addAnnotation:pin];
修改强>
以下是实施:
@implementation MapPin
@synthesize coordinate;
@synthesize title;
@synthesize subtitle;
- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:placeName description:description {
self = [super init];
if (self != nil) {
coordinate = location;
title = placeName;
[title retain];
subtitle = description;
[subtitle retain];
}
return self;
}
- (void)dealloc {
[title release];
[subtitle release];
[super dealloc];
}
@end
希望这有帮助, ONNO。