MKAnnotation,简单的例子

Nar*_*lle 17 iphone objective-c mkannotation

有一个简单的示例项目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
Run Code Online (Sandbox Code Playgroud)

根据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
Run Code Online (Sandbox Code Playgroud)

因此,如果我按照注释中的指示手动设置坐标,它就可以工作.但我希望能够像第一条评论一样动态设置值.我试过各种各样的方法,但都没有成功.因为你没有指定一个- (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;
}
Run Code Online (Sandbox Code Playgroud)

那有什么不对吗?

onn*_*web 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
Run Code Online (Sandbox Code Playgroud)

然后,在您创建地图的地方,您将调用:

pin = [[MapPin alloc] initWithCoordinates:[track startCoordinates] placeName:@"Start" description:@""];
[map addAnnotation:pin];
Run Code Online (Sandbox Code Playgroud)

编辑:

这是实施:

@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
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助,Onno.