【发布时间】:2014-01-25 15:31:51
【问题描述】:
我正在尝试在地图视图中标记一个位置。
首先我在像这样的一个单独的类中实现了MKAnnotation 协议。
AddressAnnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface AddressAnnotation : NSObject <MKAnnotation>
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
- (id)initWithCoordinates:(CLLocationCoordinate2D)location;
@end
AddressAnnotation.m
#import "AddressAnnotation.h"
@implementation AddressAnnotation
- (id)initWithCoordinates:(CLLocationCoordinate2D)location
{
self = [super init];
if (self)
{
self.coordinate = location;
}
return self;
}
@end
然后在视图控制器中,我实现了MKMapViewDelegate。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
static NSString *myIdentifier = @"myIndentifier";
MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:myIdentifier];
if (!pinView)
{
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:myIdentifier];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.animatesDrop = NO;
}
return pinView;
}
在viewDidLoad 方法中,我初始化了AddressAnnotation 类的一个实例。
- (void)viewDidLoad
{
[super viewDidLoad];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(34.421496, -119.70182);
AddressAnnotation *pinAnnotation = [[AddressAnnotation alloc] initWithCoordinates:coordinate];
[self.mapView addAnnotation:pinAnnotation];
}
但我不断收到以下错误。
-[AddressAnnotation setCoordinate:]:无法识别的选择器发送到实例
我不确定我在这里做错了什么。有人可以帮帮我吗?
谢谢。
【问题讨论】:
标签: ios mkmapview mapkit mkannotation mkmapviewdelegate