Xavi Valero
Xavi Valero

Reputation: 2057

adding button to annotation bubble in mapview

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.

Upvotes: 0

Views: 1614

Answers (2)

iCoder4777
iCoder4777

Reputation: 1692

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.

Upvotes: 0

user907729
user907729

Reputation:

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;
}

Upvotes: 4

Related Questions