使用地图工具包在地图视图中显示左右标注附件

时间:2013-07-28 15:27:46

标签: iphone ios6 storyboard mapkit callouts

我有一个简单的问题,我无法找到答案。我从plist中提取长位置和lat位置并将它们放在地图视图上。这部分工作正常。别针显示,标题和副标题(再次来自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。

3 个答案:

答案 0 :(得分:1)

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

annotationView.leftCalloutAccessoryView = IconView;

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

希望这会有所帮助。:)

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

得到了啊。因为安娜对于代表没有被设定的暗示是正确的。我正以错误的方式设置代表。我只需要添加到我的视图控制器标题视图,而不是尝试在我的注释视图中输入它,然后在视图控制器的视图中调用委托给自己加载方法,如下所示:myMapView.delegate = self; 代码一直在工作,但由于缺少委托,我对注释的看法没有被解雇。再一次感谢Anna Karennina的暗示。