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

我的问题是左右标注配件永远不会被调用。当按下别针时,我只得到标题和副标题。我尝试了很多不同的方法,但似乎无法解决这个问题。任何人都可以帮我修复此代码,以便我可以展示配件。

太感谢了

ps 我正在使用 iOS 6 和情节提要,该项目仅基于 iphone。

4

3 回答 3

1

– mapView:didSelectAnnotationView:您应该在委托方法中实现左右标注。所以,你的代码

annotationView.leftCalloutAccessoryView = IconView;

annotationView.canShowCallout = YES;必须按上述方法。尝试调用默认注释。你会明白的。

希望这可以帮助。:)

于 2013-07-29T17:45:49.417 回答
0

啊,明白了。因为安娜在暗示没有设置代表时是正确的。我以错误的方式设置了代表。我所要做的就是将其添加到我的视图控制器标题视图中,而不是尝试在我的注释视图中输入它,然后在视图控制器的视图中调用委托确实将方法加载到 self,如下所示: myMapView.delegate = self; 代码一直在工作,但由于缺少委托,我的注释视图没有被触发。再次感谢 Anna Karennina 的提示。

于 2013-07-29T13:33:38.050 回答
0

我认为您需要实现以下方法:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
于 2013-07-28T15:38:08.103 回答