3

I am using below code for add button in mapView but button click event is working on only left size it's not working on right side.

MKPinAnnotationView *pinAnnotation = nil;
if(annotation != beerMapView.userLocation)
{
    static NSString *defaultPinID = @"myPin";
    pinAnnotation.pinColor = MKPinAnnotationColorRed;
    pinAnnotation = (MKPinAnnotationView *)[beerMapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinAnnotation == nil )
        pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
    NSLog(@"PinAnnotation: %@",NSStringFromCGRect(pinAnnotation.frame));
    pinAnnotation.canShowCallout = YES;
    pinAnnotation.image = [UIImage imageNamed:@""]; //Set blank pin image

    UIImageView *pinImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    pinImage.image = [UIImage imageNamed:@"ic_pin.png"];

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [infoButton setImage:[UIImage imageNamed:@"ic_pin.png"] forState:UIControlStateNormal];
    infoButton.frame = CGRectMake(0, 0, 40, 40);
    pinAnnotation.rightCalloutAccessoryView = infoButton;
    [infoButton addTarget:self action:@selector(detailBtnClk:) forControlEvents:UIControlEventTouchUpInside];
    [pinAnnotation addSubview:infoButton];
}

infoButton - > left portion(0 to 20) button click event working. (Call detailBtnClk)
infoButton - > right portion(21 to 40) button click event is not working. (Can not call detailBtnClk)

Any help is appreciated.

Chirag Pipaliya
  • 1,281
  • 12
  • 20
  • What functionality and UI are you trying to achieve exactly? Why is the code creating an MKPinAnnotationView and then setting image to blank (instead of just creating a plain MKAnnotationView and setting its image)? What exactly is the "left portion" here that is working? Why is infoButton set as the rightCalloutAccessoryView _and_ added as a subview to the annotation view itself? What is considered the "right portion" here exactly? Is the callout popping up when an annotation is tapped? By the way, when views are re-used, the code adds infoButton to the view again (and again). –  Aug 05 '13 at 13:07
  • @AnnaKarenina : My button width 40 so left portion contain 0 yo 20 and right portion contain 21 to 40. I am trying to set star image instead of pin image. In my code pinAnnotation.image is for blank pin image. – Chirag Pipaliya Aug 06 '13 at 03:59
  • Try setting the frame of the annotation view itself (pinAnnotation.frame) so it's at least as large as the button frame. The default frame width for pin view is less than 40. Also consider the other comments for improving the code. –  Aug 06 '13 at 10:59
  • i already try that pinAnnotation.frame = infoButton.frame; but it is not working for me and i also try to change frame like CGRectMake(20, 0, 40, 40); but it working opposite. – Chirag Pipaliya Aug 06 '13 at 11:32
  • If all you need is a clickable image, you don't need to add a button. Create a plain MKAnnotationView, set its image, and when tapped, map view will call didSelectAnnotationView delegate method. –  Aug 06 '13 at 11:57
  • @AnnaKarenina : Can you give me any tutorial link ? – Chirag Pipaliya Aug 06 '13 at 12:01

1 Answers1

0

Here is this link Which helps you.

Monotouch MapKit - annotations - adding a button to bubble

Tutorial-

http://www.altinkonline.nl/tutorials/xcode/corelocation/add-a-button-to-an-annotation/

Instead of giving button type roundRect give it UIButtonTypeDetailDisclosure

Hope Helps you.

Community
  • 1
  • 1
Chitra Khatri
  • 1,260
  • 2
  • 14
  • 31