我有一個簡單的問題,我無法找出答案。我從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。
你確定你的viewForAnnotation方法獲取調用?它被寫入的方式是創建普通的MKAnnotationViews(而不是MKPinAnnotationViews)而不設置圖像,因此它們將不可見。如果您的註釋顯示爲紅色的針腳,那是因爲該方法並未首先被調用(可能是因爲未設置地圖視圖的代表),並且地圖視圖創建了默認針腳視圖。另請注意,如果您打算使用calloutAccesoryControlTapped委託方法,則不應在附件按鈕上執行手動addTaget。 – Anna
@AnnaKarenina,謝謝你的建議。我今晚會回到這個項目,我會在我編寫代碼後請求你的幫助。你可能對代表是正確的。 –