0

The following is my code for adding annotation into a mapview. This code is called in viewdidload method

-(void)displayMYMap
{
MKCoordinateRegion region = mapView.region; 
MKCoordinateSpan span; 
span.latitudeDelta=0.2; 
span.longitudeDelta=0.2; 

addAnnotation = [[[AddressAnnotation alloc] initWithCoordinate:mapCenter title:firstName SubTitle:lastName Recordid:passedRecordID]autorelease];

[mapView addAnnotation:addAnnotation];
region.span=span; 
region.center=mapCenter; 
}

I am trying to add a button to my annotation bubble. I am getting an exception while I am trying to add the button to the annotationview.

UIButton *informationButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure ];
addAnnotation.rightCalloutAccessoryView = informationButton;

This is the code I have written to add the button. I cant figure out a reason for the exception.

Xavi Valero
  • 2,047
  • 7
  • 42
  • 80

2 Answers2

4

use delegate method of Annotation,

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    static NSString *identifier = @"RoutePinAnnotation";
        MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                         initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
        pinView.animatesDrop=YES;
        pinView.canShowCallout=YES;
        if ([(UICCircleAnnotation *)annotation annotationType] == UICCircleAnnotationTypeOther) {
            pinView.pinColor = MKPinAnnotationColorPurple;
        } else if ([(UICCircleAnnotation *)annotation annotationType] == UICCircleAnnotationTypeContact) {
            pinView.pinColor = MKPinAnnotationColorRed;
        }

        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];
        [rightButton addTarget:self
                        action:@selector(buttonMethod:)
              forControlEvents:UIControlEventTouchUpInside];
        pinView.rightCalloutAccessoryView = rightButton;

        UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profile.png"]];
        pinView.leftCalloutAccessoryView = profileIconView;
        [profileIconView release];

        return pinView;
}
Ankur
  • 5,086
  • 19
  • 37
  • 62
  • So, in addition to the method I am using, I should use the - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation delegate method too. Right. – Xavi Valero Nov 15 '11 at 09:58
  • hope this links will help you http://stackoverflow.com/questions/3994231/subclasses-of-mkannotationview and http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKAnnotationView_Class/Reference/Reference.html – Ankur Nov 15 '11 at 10:15
0

in delegate method of Annotation View,

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    static NSString *identifier = @"RoutePinAnnotation";
        MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                         initWithAnnotation:annotation reuseIdentifier:identifier] autorelease];
        pinView.animatesDrop=YES;
        pinView.canShowCallout=YES;
        if ([(UICCircleAnnotation *)annotation annotationType] == UICCircleAnnotationTypeOther) {
            pinView.pinColor = MKPinAnnotationColorPurple;
        } else if ([(UICCircleAnnotation *)annotation annotationType] == UICCircleAnnotationTypeContact) {
            pinView.pinColor = MKPinAnnotationColorRed;
        }

        pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];


        return pinView;
}

You will get disclosure button on the annotation bubble.

iCoder4777
  • 1,682
  • 1
  • 14
  • 35