所以,我在 MKMapView 上为注释图钉定位时遇到了问题。
我只是写了一个可以从 NSObject 继承的类,叫做“Annotation”,它使用协议。
所以,这里的代码:
注释.h
#import <Foundation/Foundation.h>
@interface Annotation : NSObject <MKAnnotation>
{
CLLocationCoordinate2D _coordinate;
NSUInteger _pinID;
}
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate andID:(NSUInteger)pinID;
@end
注释.m
#import "Annotation.h"
@implementation Annotation
@synthesize coordinate=_coordinate;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate andID:(NSUInteger)pinID{
self = [super init];
if (self != nil) {
_coordinate = coordinate;
_pinID = pinID;
}
return self;
}
- (NSString *)title {
return [NSString stringWithFormat:@"Pin %u", _pinID];
}
- (NSString *)subtitle {
return [NSString stringWithFormat:@"%.4f, %.4f", _coordinate.latitude, _coordinate.longitude];
}
@end
然后,我尝试使用以下代码将其连接到我的地图
MapViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
CLLocationCoordinate2D coordinate;
coordinate.latitude = 49.2802;
coordinate.longitude = -123.1182;
mapView.delegate = self;
mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000);
CLLocationCoordinate2D newCoord = {2000, 2000};
Annotation* annotation = [[Annotation alloc] initWithCoordinate:newCoord andID:1];
[mapView addAnnotation:annotation];
}
但这没有任何意义,引脚甚至没有显示。我究竟做错了什么?我寻求帮助和教训,但没有工作成果。