2014-03-03 38 views
0

我想添加標題的註釋,但我沒有看到它時,點擊地圖上的相關點。MKPointAnnotation不會顯示標題 - 非常奇怪的行爲

要添加它,我用:

for(NSDictionary *dic in [GlobalData sharedGlobals].currentBusinessList) 
    { 
     NSString *title=[dic objectForKey:@"name"]; 
     double latitude=[[dic objectForKey:@"latitude"] doubleValue]; 
     double longitude=[[dic objectForKey:@"longitude"] doubleValue]; 
     CLLocationCoordinate2D location = CLLocationCoordinate2DMake(latitude,longitude); 

     MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; 
     MKPinAnnotationView *pointV = [[MKPinAnnotationView alloc] initWithAnnotation:point reuseIdentifier:nil]; 
     [point setCoordinate:location]; 
     point.title = title; 
     [self.mapView addAnnotation:pointV.annotation]; 


     MKCoordinateRegion region = self.mapView.region; 
     region.center = location; 
     region.span.longitudeDelta /= 5.0; 
     region.span.latitudeDelta /= 5.0; 
     [self.mapView setRegion:region]; 

    } 

現在這個偉大的工程。但你不能設置顏色在這裏,它不會工作,但只有當我在這裏添加回調來設置顏色:

- (MKAnnotationView *) mapView:(MKMapView *)mapView 
      viewForAnnotation:(id <MKAnnotation>) annotation 
{ 


     MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"]; 
    //[email protected]"ran"; also not working 
     annView.pinColor = MKPinAnnotationColorPurple; 
     annView.animatesDrop=YES; 
     return annView; 
} 

但比似乎增加這個人會從標題防止露面。

似乎我只能得到其中的一個,標題或顏色。 如果我拿出第二種方法,我可以看到標題,但比在第一種方法中設置顏色不起作用。

什麼是正確的方式來標題和顏色幾個註釋?

回答

1

不,您可以更改顏色並顯示標題。你的代碼看起來不錯,但迄今爲止這一行添加到(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation

pinAnno.canShowCallout = YES; 
+0

哇!你解決了我的問題。但爲什麼他們會這樣做?爲什麼我必須添加這個和它不明顯? – Curnelious

+0

閱讀[文檔]時它變成了obvoius(https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKAnnotationView_Class/Reference/Reference.html#//apple_ref/occ/instp/MKAnnotationView/canShowCallout ) – hacker2007

1

您需要創建符合NSAnnotation協議一類,並使用它,而不是使用MKPointAnnotation

的創建自己的類,例如MyAnnotation

MyAnnotation .h文件中

#import <MapKit/MapKit.h> 

@interface MyAnnotation : NSObject <MKAnnotation> 

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; 
@property (nonatomic, readonly, copy) NSString *title; 
@property (nonatomic, readonly, copy) NSString *subtitle; 

- (instancetype)initWithTitle:(NSString *)title andSubTitle:(NSString *)subtitle atCoordinate:(CLLocationCoordinate2D)coordinate; 

@end 

MyAnnotation .m文件

#import "MyAnnotation.h" 

@implementation MyAnnotation 

- (instancetype)initWithTitle:(NSString *)title andSubTitle:(NSString *)subtitle atCoordinate:(CLLocationCoordinate2D)coordinate 
{ 
    self = [super init]; 
    if (self) { 
     _title = title; 
     _subtitle = subtitle; 
     _coordinate = coordinate; 
    } 
    return self; 
} 

@end 

然後使用你的類來創建註釋

,而不是這個代碼

MKPointAnnotation *point = [[MKPointAnnotation alloc] init]; 
MKPinAnnotationView *pointV = [[MKPinAnnotationView alloc] initWithAnnotation:point reuseIdentifier:nil]; 
[point setCoordinate:location]; 
point.title = title; 
[self.mapView addAnnotation:pointV.annotation]; 

添加以下代碼

MyAnnotation *annotation = [[MyAnnotation alloc] initWithTitle:title andSubTitle:nil atCoordinate:location]; 
[self.mapView addAnnotation:annotation];