2011-10-21 17 views
0

我試圖在MKMapView上添加MKAnnotations。我正在按照各種可用教程中提出的程序進行操作。我已經從NSobject Class創建了PlaceMark.h/.m文件,如下所示。 PlaceMark.h無法在MKAnnotation類中初始化CLLocationCoordinate2D iVar類

#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 

PlaceMark.m

#import "PlaceMark.h" 

@implementation PlaceMark 
@synthesize coordinate,title; 

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

@end 
在我的viewController持有的MKMapView

,在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. 

[email protected]"Test"; 
ann.coordinate= region.center; 
[parkingMap addAnnotation:ann]; 
} 

誰能告訴我我在做什麼錯誤?順便說一句,我使用Xcode4.2與iOS sdk5.0和 不使用故事板/自動引用計數

感謝 薩米特

回答

0

您已經定義了coordinate財產readonly,所以你不能對它進行設置。

將其更改爲readwrite

另外,如果你只需要一個基本的標註對象與titlesubtitle,並設定coordinate,您可以使用內置的MKPointAnnotation類,而不是定義自己的類。創建和初始化將是相同的(只需用MKPointAnnotation替換您的類名稱)。

+0

我做到了,但仍然給了我同樣的錯誤。 – slonkar

+0

什麼是錯誤? – Anna

+0

它向我展示了同樣的錯誤。之後,我刪除了整個事情,並重新開始,這一次它的工作。 – slonkar

相關問題