Search code examples
iosannotationscallout

IOS event on uimap callout subview click


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 ?


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