2012-08-30 80 views
0

我是新來的,我把我的頭撞在牆上,所以任何幫助你可以提供將不勝感激。如何讓我的MKMapView註釋顯示出來?

我的應用程序正在下載一系列座標和標題,我將其轉換爲MKAnnotations。我已經實現了MKAnnotation協議不夠好,因爲它停止抱怨說,但這裏的代碼:

Annotator.h:

#import <Foundation/Foundation.h> 
#import "MapKit/Mapkit.h" 

@interface Annotator : NSObject <MKAnnotation> 

+ (Annotator *) createAnnotator:(CLLocationCoordinate2D *)coordinate 
         withTitle:(NSString *)title 
        andSubtitle:(NSString *)subtitle; 

@property (nonatomic, strong) NSString *myTitle; 
@property (nonatomic, strong) NSString *mySubtitle; 
@property (nonatomic) CLLocationCoordinate2D *coordinate; 

@end 

很無聊爲止。這裏的Annotator.m:

#import "Annotator.h" 

@implementation Annotator 

@synthesize myTitle = _myTitle; 
@synthesize mySubtitle = _mySubtitle; 
@synthesize coordinate = _coordinate; 

+ (Annotator *)createAnnotator:(CLLocationCoordinate2D *)coordinate 
        withTitle:(NSString *)title 
        andSubtitle:(NSString *)subtitle 
{ 
    Annotator *annotation = [[Annotator alloc] init]; 
    annotation.myTitle = title; 
    annotation.mySubtitle = subtitle; 
    annotation.coordinate = coordinate; 
    return annotation; 
} 

- (NSString *)title 
{ 
    return self.myTitle; 
} 

- (NSString *)subtitle 
{ 
    return self.mySubtitle; 
} 

@end 

現在,我已經得到了最中央視圖控制器上的其他代碼,以便儘快與數據的HTTP請求返回,我遍歷它,創建我MKAnnotation對象,然後爲每一個做到這一點:

[self.territoryMap addAnnotation:locationPin]; 

...其中self.territoryMap是出口到的MKMapView我拖進我的故事板,並locationPin是MKAnnotation對象我和我上面標註器實現創建。

我看過的一些解決方案已經有了MKMapViewDelegate方法,但在我看來,它們不應該是必需的,因爲我只是在尋找默認行爲。我把一個代表放到了一個代表中,這讓我改變了用戶位置的顏色,但這並不是我所擔心的。

還有什麼我需要做的?我很困惑!感謝您提供的任何幫助!

+0

就在addAnnotation行之前,把'NSLog(@「coord =%f,%f; map =%@」,locationPin.coordinate.latitude,locationPin.coordinate.longitude,self. territoryMap);'。它記錄的座標是什麼(拉特/長褲是否正確)?它爲什麼記錄地圖(是否有任何機會)? – Anna

回答

0

您必須設置MapViews委託。那麼它應該工作得很好。

或者看看這個:http://maybelost.com/2011/01/a-basic-mapview-and-annotation-tutorial/ 它看起來很像你想要做的!

+0

感謝您的回覆!我快速瀏覽了教程,並沒有看到代理需要在那裏,所以我仍然有點困惑,但我仍然很欣賞這個鏈接,因爲這個鏈接看起來像是一個更好的平行於我尋找比我找到的其他人。 我想接下來要做的就是自己實現這個例子,看看它是否適合我。這可能是因爲我的代碼糟糕中有其他東西在破壞事情。祝我好運! –

+0

好的,事實證明我錯了;我原來的協議實現顯然不符合。我認爲它確實是因爲(a)代碼停止了錯誤,並且(b)我可以很好地將標題和座標放到調試日誌中。但改變我的協議(以及我稱之爲的方式)來匹配這個協議(通過一些小的調整)運行良好。 我仍然有點困惑,需要弄清楚我做錯了什麼,但至少它工作。感謝您指點正確的方式! –