Question

I'm creating a map on IOS and on each annotation click I'm adding a custom subview like the following:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {

    //load pin callout
    nearbyMapCallout *calloutView  = [[nearbyMapCallout alloc]init];
    calloutView = (nearbyMapCallout *)[[[NSBundle mainBundle] loadNibNamed:@"nearbyMapCallout" owner:self options:nil] objectAtIndex:0];

    CGRect calloutViewFrame = calloutView.frame;
    calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 37, -calloutViewFrame.size.height + 35);
    calloutView.frame = calloutViewFrame;

    [view addSubview:calloutView];

}

So now how can I trigger an event when clicking a specific annotation callout subview ?

Was it helpful?

Solution

You can try this

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view 
{
    if(![view.annotation isKindOfClass:[MKUserLocation class]]) {
        CGSize  calloutSize = CGSizeMake(100.0, 80.0);
        UIView *calloutView = [[UIView alloc] initWithFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y-calloutSize.height, calloutSize.width, calloutSize.height)];
        calloutView.backgroundColor = [UIColor whiteColor];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(5.0, 5.0, calloutSize.width - 10.0, calloutSize.height - 10.0);
        [button setTitle:@"OK" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(checkin) forControlEvents:UIControlEventTouchUpInside];
        [calloutView addSubview:button];
        [view.superview addSubview:calloutView];
    }
}

Ref: https://stackoverflow.com/a/17772487/2553526

OTHER TIPS

You can use UITapGestureRecognizer to capture tap event of your view when you are adding:

UITapGestureRecognizer *singleTap = 
  [[UITapGestureRecognizer alloc] initWithTarget:self 
                                          action:@selector(handleSingleTap:)];
[calloutView addGestureRecognizer:singleTap];
[singleTap release];

Following is the method called on tapping calloutView:

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
  CGPoint location = [recognizer locationInView:[recognizer.view superview]];

  //Add your code here..
}

Hope this helps.

The following answer worked for me:

https://stackoverflow.com/a/17772487/2915167

It adds a view to the super view but be careful, you will have to move it around programmatically when you move the map.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top