0

I started programming recently, and I have a question! I have created a mapview with the annotation and disclosure button.

How can I create multiple annotations with disclosure button that, after a tap, go to different descriptions based on the annotation choice?

This is my code:

@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;
}
  • Have you implemented the `button:` method? If yes, show that code otherwise see [this](http://stackoverflow.com/questions/4565197/how-to-find-which-annotation-send-showdetails) and [this](http://stackoverflow.com/questions/9876042/annotation-details-after-detail-disclosure-pressed/9876255#9876255) to move forward a little bit. Are you using storyboard? –  Aug 01 '13 at 02:32

1 Answers1

0

The usual way to do it is to implement -mapView:annotationView:calloutAccessoryControlTapped: in your map view delegate. You can use the annotaionView parameter to decide which annotaion was tapped and what information you want to display.

Caleb
  • 124,013
  • 19
  • 183
  • 272