2013-07-28 24 views
0

我有一個簡單的問題,我無法找出答案。我從plist中提取多個位置並放置在地圖視圖中。這部分工作得很好。引腳顯示以及標題和副標題(再次來自plist)也會顯示。這裏是我的視圖控制器.h類的代碼:使用地圖工具包在地圖視圖中顯示左側和右側標註配件

#import "ViewController.h" 
#import "Annotation.h" 

@interface ViewController() 

@end 


#define WIMB_LATITUDE 51.434783; 
#define WIMB_LONGITUDE -0.213428; 


#define ARSENAL_LATITUDE 51.556899; 
#define ARSENAL_LONGITUDE -0.106403; 

#define CHELSEA_LATITUDE 51.481314; 
#define CHELSEA_LONGITUDE -0.190129; 


#define THE_SPAN 0.10f; 

@implementation ViewController 
@synthesize myMapView; 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

MKCoordinateRegion myRegion; 
CLLocationCoordinate2D center; 
center.latitude = ARSENAL_LATITUDE; 
center.longitude = ARSENAL_LONGITUDE; 
MKCoordinateSpan span; 
span.latitudeDelta = THE_SPAN; 
span.longitudeDelta = THE_SPAN; 
myRegion.center = center; 
myRegion.span=span; 
[myMapView setRegion:myRegion animated:YES]; 

NSMutableArray *annotations = [[NSMutableArray alloc]init]; 
NSString *path = [[NSBundle mainBundle] pathForResource:@"Stadiums" ofType:@"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; 
NSArray *anns = [dict objectForKey:@"Stadiums"]; 
NSLog(@"read1"); 

for(int i = 0; i < [anns count]; i++) { 
    NSLog(@"read2"); 
    float realLatitude = [[[anns objectAtIndex:i] objectForKey:@"Latitude"] floatValue]; 
    float realLongitude = [[[anns objectAtIndex:i] objectForKey:@"Longitude"] floatValue]; 
    NSLog(@"read3"); 

    Annotation *myAnnotation = [[Annotation alloc] init]; 
    CLLocationCoordinate2D theCoordinate; 
    theCoordinate.latitude = realLatitude; 
    theCoordinate.longitude = realLongitude; 
    myAnnotation.coordinate = theCoordinate; 
    myAnnotation.title = [[anns objectAtIndex:i] objectForKey:@"Title"]; 
    myAnnotation.subtitle = [[anns objectAtIndex:i] objectForKey:@"Subtitle"]; 
    [myMapView addAnnotation:myAnnotation]; 
    [annotations addObject:myAnnotation]; 
    //[myAnnotation release]; 
} 

} 

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation 
{ 
static NSString *AnnotationViewID = @"annotationViewID"; 

MKAnnotationView *annotationView = (MKAnnotationView *)[myMapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; 

if (annotationView == nil) 
{ 
    annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID]; 
} 

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
[rightButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside]; 
annotationView.rightCalloutAccessoryView = rightButton; 

UIImageView *IconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"toilets.png"]]; 
annotationView.leftCalloutAccessoryView = IconView; 

annotationView.canShowCallout = YES; 
annotationView.annotation = annotation; 
return annotationView; 
} 


- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

@end 

我的問題是,左側和右側的標註配件永遠不會被調用。當按下引腳時,我只是得到標題和副標題。我嘗試了很多不同的方式,但似乎無法解決這個問題。任何人都可以幫助我修復此代碼,以便我可以獲取配件以顯示。

謝謝百萬

p.s.我使用的是iOS 6和故事板,該項目僅基於iPhone。

+1

你確定你的viewForAnnotation方法獲取調用?它被寫入的方式是創建普通的MKAnnotationViews(而不是MKPinAnnotationViews)而不設置圖像,因此它們將不可見。如果您的註釋顯示爲紅色的針腳,那是因爲該方法並未首先被調用(可能是因爲未設置地圖視圖的代表),並且地圖視圖創建了默認針腳視圖。另請注意,如果您打算使用calloutAccesoryControlTapped委託方法,則不應在附件按鈕上執行手動addTaget。 – Anna

+0

@AnnaKarenina,謝謝你的建議。我今晚會回到這個項目,我會在我編寫代碼後請求你的幫助。你可能對代表是正確的。 –

回答

0

啊,明白了。因爲安娜對她沒有設置代表的暗示是正確的。我以錯誤的方式設置代表。我所要做的只是添加到我的視圖控制器標題視圖,而不是嘗試在我的註釋視圖中輸入它,然後調用視圖控制器的視圖中的委託負載方法自我,如下所示:myMapView.delegate = self; 代碼一直在工作,但由於缺少委託,我的註釋視圖沒有被解僱。再一次感謝Anna Karennina的提示。

0

我認爲你需要實現以下方法:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control 
+0

這是在已經有註釋顯示附件按鈕時調用的方法。我的問題是,註釋並沒有顯示我爲了註釋而聲明的附件按鈕。 –

1

您應該在– mapView:didSelectAnnotationView:委託方法中實施左右標註。 所以,你的代碼

annotationView.leftCalloutAccessoryView = IconView; 

annotationView.canShowCallout = YES;必須與上面方法猶豫。 嘗試調用默認註釋。你會清楚明白的。

希望這有助於:)

+0

感謝您的回答,但我已經回答了我的問題。這是我在錯誤的課堂上宣佈的代表方法。但謝謝你的嘗試。 –

+0

好的。祝一切順利。 :) – Vish

相關問題