0
votes

Sorry, I have read a bunch of tutorials how to create a custom Callout for MapKit Annotation. It works with NSLog, but I cannot display the information in the Callouts.

I have two type of icons on the map. This is my viewForAnnotation method:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

    if (! [annotation isKindOfClass:[IGAMapAnnotation class]]) {
        return nil;
    }

    IGAMapAnnotation *myLocation = (IGAMapAnnotation *) annotation;

    self.typeIsFix = [myLocation.navaidType isEqualToString:@"FIX"];
    self.typeIsPort = [myLocation.navaidType isEqualToString:@"PORT"];

    int planeImageViewTag = 42;

    NSString *reuseId;

    if (self.typeIsPort)
        reuseId = @"IGAMapAnnotationPort";

    else if (self.typeIsFix)
        reuseId = @"IGAMapAnnotationFix";

    else
        reuseId = @"IGAMapAnnotationOther";


    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];

    if (annotationView == nil)
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
        annotationView.enabled = YES;

        UIButton *annotationInfo = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        annotationView.rightCalloutAccessoryView = annotationInfo;
        annotationView.canShowCallout = YES;


        if (self.typeIsPort)
        {
            annotationView.image = [UIImage imageNamed:@"mapPORT.png"];
            annotationView.centerOffset = CGPointMake(0, 0);

        }

        else if (self.typeIsFix)
        {
            annotationView.image = [UIImage imageNamed:@"mapFIX.png"];
            annotationView.centerOffset = CGPointMake(0, 0);

        }

        else
            return nil;
    }

    else
    {
        annotationView.annotation = annotation;
    }

    return annotationView;
}

Then I have a calloutAccessoryControlTapped method

- (void)mapView:(MKMapView *)mapview annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    IGAAnnotationInfoViewController *popOverCallout = [[IGAAnnotationInfoViewController alloc]init];

    UIPopoverController *popOver = [[UIPopoverController alloc] initWithContentViewController:popOverCallout];

    popOver.popoverContentSize = CGSizeMake(300, 200);

    [popOver presentPopoverFromRect:view.bounds
                             inView:view
           permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

I have also created a UIViewController which I assigned to UIPopoverController.

Now, when I tap the button on my annotation I see a white space for text. Great. If I assign text to a label in UIViewController, it also works great (the following is my UIViewController.m):

- (void)viewDidLoad {
    [super viewDidLoad];

    txtCallout = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 300, 200) ];
    txtCallout.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(14.0)];
    txtCallout.numberOfLines = 0;
    txtCallout.clipsToBounds = YES;
    txtCallout.backgroundColor = [UIColor clearColor];
    txtCallout.textColor = [UIColor blackColor];
    txtCallout.textAlignment = NSTextAlignmentLeft;
    txtCallout.text = @"text\ntext\ntext";
    [self.view addSubview:txtCallout];
}

But how do I insert the text from my annotation method? Also the text must be different depending on the icon type, say @"PORT, PORT" or @"FIX,FIX". How do I do it?

EDIT:

I have managed to display callouts with the necessary information passed. My last problem is that sometimes my callout is 3 lines, sometimes -- as many as 15. How is it possible to make the callout adjust automatically to the number of lines in my string? Should I modify my popoverContentSize or my label size in the UIViewController?

Thank you so much!

1

1 Answers

0
votes

I have figured out how to adjust the MK Annotation callout to a UILabel.

Implement the calloutAccessoryControlTapped method

- (void)mapView:(MKMapView *)mapview annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    // OPTIONAL: Deselecting Annotation View when Callout is tapped
    //[mapview deselectAnnotation:view.annotation animated:YES];

    NSString *calloutDetails;

    IGAMapAnnotation *annotationTapped = (IGAMapAnnotation *)view.annotation;

    calloutDetails = [NSString stringWithFormat:@"YOUR TEXT:\nYOURTEXT\n"];

    // Declare and initialize the UIViewController that has the label to contain the callout information
    IGAAnnotationInfoViewController *detailViewController = [[IGAAnnotationInfoViewController alloc]initWithText:calloutDetails];

    UIPopoverController *popOver = [[UIPopoverController alloc] initWithContentViewController:detailViewController];

    // Size of the UIPopoverController = size of the label + 40 pts
    popOver.popoverContentSize = CGSizeMake(detailViewController.txtCallout.frame.size.width+40,detailViewController.txtCallout.frame.size.height+40);

    // Show popover controller
    [popOver presentPopoverFromRect:view.bounds
                             inView:view
           permittedArrowDirections:UIPopoverArrowDirectionAny
                           animated:YES];
}

Now, IGAAnnotationInfoViewController.h

@interface IGAAnnotationInfoViewController : UIViewController   {
    CGRect calloutSize;
}

@property (strong,nonatomic) NSString *calloutInformation;
@property (strong,nonatomic) IGACalloutLabel *txtCallout;

-(IGAAnnotationInfoViewController*) initWithText : (NSString*) calloutText;

IGAAnnotationInfoViewController.m

 @implementation IGAAnnotationInfoViewController

 @synthesize calloutInformation,txtCallout;

-(IGAAnnotationInfoViewController*) initWithText : (NSString*) calloutText {
    self = [super init];

    if ( self ) {
        calloutInformation = calloutText;

        // Creating a label that will display the callout information (passed from IGAAcarasViewController - Map Annotation)
        txtCallout = [[IGACalloutLabel alloc] initWithFrame:CGRectMake(20, 20, 0, 0)];

        txtCallout.lineBreakMode = NSLineBreakByWordWrapping;
        txtCallout.numberOfLines=0;
        txtCallout.backgroundColor = [UIColor clearColor];
        txtCallout.textColor=[UIColor blueColor];
        txtCallout.text = calloutInformation;
        [txtCallout drawTextInRect:CGRectMake(10,10,0,0)];

        [txtCallout sizeToFit];
        [self.view addSubview:txtCallout];
    }

    return self;
}

Finally, subclass the UILabel class:

implementation IGACalloutLabel

@synthesize topInset, leftInset, bottomInset, rightInset;

- (void)drawTextInRect:(CGRect)rect
{
    UIEdgeInsets insets = {topInset,leftInset,bottomInset,rightInset};

    return [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)];
}

Regards,