地图视图 - 披露按钮到视图

时间:2013-07-31 14:27:36

标签: ios mkmapview mkannotation

我最近开始编程,我有一个问题!我已经使用注释和显示按钮创建了一个mapview。

如何使用公开按钮创建多个注释,点按后,根据注释选项转到不同的描述?

这是我的代码:

@synthesize mapView;

-(void)viewDidLoad {    

    [super viewDidLoad];
    [mapView setMapType:MKMapTypeHybrid];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    [mapView setDelegate:self];

    MKCoordinateRegion bigBen = { {0.0, 0.0} , {0.0, 0.0} };
    bigBen.center.latitude = 51.50063;
    bigBen.center.longitude = -0.124629;
    bigBen.span.longitudeDelta = 0.02f;
    bigBen.span.latitudeDelta = 0.02f;
    [mapView setRegion:bigBen animated:YES];

    Annotation *ann1 = [[Annotation alloc] init];
    ann1.title = @"Big Ben";
    ann1.subtitle = @"Your subtitle";
    ann1.coordinate = bigBen.center;
    [mapView addAnnotation: ann1];

    MKCoordinateRegion Bridge = { {0.0, 0.0} , {0.0, 0.0} };
    Bridge.center.latitude = 51.500809;
    Bridge.center.longitude = -0.120914;
    Bridge.span.longitudeDelta = 0.02f;
    Bridge.span.latitudeDelta = 0.02f;
    [mapView setRegion:Bridge animated:YES];

    Annotation *ann2 = [[Annotation alloc] init];
    ann2.title = @"Westminster Bridge";
    ann2.subtitle = @"Your subtitle";
    ann2.coordinate = Bridge.center;
    [mapView addAnnotation:ann2];

}

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
    MyPin.pinColor = MKPinAnnotationColorPurple;

    UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [advertButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];

    MyPin.rightCalloutAccessoryView = advertButton;
    MyPin.draggable = NO;
    MyPin.highlighted = YES;
    MyPin.animatesDrop=TRUE;
    MyPin.canShowCallout = YES;

    return MyPin;
}

1 个答案:

答案 0 :(得分:0)

通常的做法是在地图视图委托中实施-mapView:annotationView:calloutAccessoryControlTapped:。您可以使用annotaionView参数来决定点击哪个注释以及要显示哪些信息。