2012-08-30 47 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)我可以很好地将标题和坐标放到调试日志中。但改变我的协议(以及我称之为的方式)来匹配这个协议(通过一些小的调整)运行良好。 我仍然有点困惑,需要弄清楚我做错了什么,但至少它工作。感谢您指点正确的方式! –