所以,我在MKMapView上为注释引脚设置位置时遇到了问题。
我只是从NSObject编写一个可遗传的类,名为“Annotation”,它使用协议。
所以,这是代码:
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
Annotation.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];
}
但这并没有任何意义,针甚至没有显示。我究竟做错了什么? 我寻求帮助和教训,但没有工作成果。
答案 0 :(得分:0)
注释的坐标无效:
CLLocationCoordinate2D newCoord = {2000, 2000};
Annotation* annotation = [[Annotation alloc] initWithCoordinate:newCoord ...
纬度,经度2000,2000无效。
您可能希望使用coordinate
变量(您在viewDidLoad
中定义了几行):
Annotation* annotation = [[Annotation alloc] initWithCoordinate:coordinate andID:1];
答案 1 :(得分:0)
试试这个
- (void)viewDidLoad
{
[super viewDidLoad];
CLLocationCoordinate2D coordinate;
coordinate.latitude = 49.2802;
coordinate.longitude = -123.1182;
mapView.delegate = self;
mapView.region = MKCoordinateRegionMakeWithDistance(coordinate, 2000, 2000);
Annotation* annotation = [[Annotation alloc] initWithCoordinate:coordinate andID:1];
[mapView addAnnotation:annotation];
}