0

我正在尝试将 MKAnnotations 放在 MKMapView 上。我正在遵循各种可用教程中建议的程序。我已经从 NSobject 类创建了 PlaceMark.h/.m 文件,如下所示。PlaceMark.h

#import <Foundation/Foundation.h>
#import <MapKit/MKAnnotation.h>
@interface PlaceMark : NSObject<MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString *title;
}
@property (nonatomic,copy) NSString *title;
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
@end

地标.m

#import "PlaceMark.h"

@implementation PlaceMark
@synthesize coordinate,title;

-(void)dealloc
{
   [title release];
   [super dealloc];
}

@end

在我持有 MKMapView 的 viewController 中,在 viewDidload 我有以下代码

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

MKCoordinateRegion region;
region.center.latitude=37.3305262;
region.center.longitude=-122.0290935;
region.span.latitudeDelta=0.01;
region.span.longitudeDelta=0.01;

[parkingMap setRegion:region animated:YES];

PlaceMark *ann=[[[PlaceMark alloc]init]autorelease]; // I have also tired it using explicitly defining method -(id)initwithTitle.... but it did not worked.

ann.title=@"Test";
ann.coordinate= region.center;
[parkingMap addAnnotation:ann];
}

谁能告诉我我做错了什么?顺便说一句,我正在使用 Xcode4.2 和 iOS sdk5.0 并且不使用情节提要/自动引用计数

谢谢苏米特

4

1 回答 1

0

您已将coordinate属性定义为readonly无法设置。

将其更改为readwrite.

此外,如果您只需要一个带有 、 和 settable 的基本注释对象titlesubtitlecoordinate可以使用内置MKPointAnnotation类而不是定义自己的类。创建和初始化将是相同的(只需将您的类名替换为MKPointAnnotation)。

于 2011-10-21T21:15:08.530 回答