Brittany
Brittany

Reputation: 1449

Obj-c - Set two different callout actions for two different annotations on the same mapView?

I have two different classes of annotations on my mapView. When each type of annotation is tapped a different callout appears. When the first type of annotation callout is tapped, it takes my user to a different screen. However, when the second type is tapped, I want an alert to display.

How can I execute this? I'm just not sure where I should put TapGesture for calloutTappedThree. See code below so far:

MapViewController.m

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

    if([annotation isKindOfClass:[meetupAnn class]]) {
        static NSString *identifier = @"currentLocation";
        MKAnnotationView *pulsingView = (MKAnnotationView *)[self.friendsMapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if(pulsingView == nil) {
            pulsingView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];

            pulsingView.canShowCallout = YES;

            pulsingView.image = [UIImage imageNamed:@"meetupbeacon.png"];
            UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"meetupbeacon.png"]];
            pulsingView.leftCalloutAccessoryView = iconView;
        }

        return pulsingView;
    }


    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    if ([annotation isKindOfClass:[MKPointAnnotation class]])
    {

        MKAnnotationView *pinView = (MKAnnotationView*)[self.friendsMapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationIdentifier"];
        if (!pinView)
        {

            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"AnnotationIdentifier"];

            pinView.image = [UIImage imageNamed:@"mapann3.png"];

        } else {
            pinView.annotation = annotation;
        }
        pinView.canShowCallout = YES;
        pinView.calloutOffset = CGPointMake(0, 0);

        UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mapann3.png"]];
        pinView.leftCalloutAccessoryView = iconView;

        return pinView;
    }
    return nil;

}



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



        UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(calloutTappedTwo:)];
        [view addGestureRecognizer:tapGesture2];



    }

-(void)calloutTappedTwo:(UITapGestureRecognizer *) sender
{
    NSLog(@"CALL OUT TWO TAPPED");

    MKAnnotationView *view = (MKAnnotationView*)sender.view;

    id <MKAnnotation> annotation = [view annotation];
    if ([annotation isKindOfClass:[MKPointAnnotation class]])
    {
        PointAnnotation *selectedPoint = (PointAnnotation *) view.annotation;

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

        OtherUserViewController *yourViewController = (OtherUserViewController *)[storyboard instantiateViewControllerWithIdentifier:@"OtherUserViewController"];

        NSMutableDictionary *dictionary = self.friendData[selectedPoint.index];
        yourViewController.frienduserData = dictionary;

        [self.navigationController pushViewController:yourViewController animated:YES];

    }

}


-(void)calloutTappedThree:(UITapGestureRecognizer *) sender
{

          NSLog(@"Callout Three was tapped");

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"RSVP for Meet-Up?"
                                                    message:@"Test Alert!"
                                                   delegate:self
                                          cancelButtonTitle:@"Join"
                                          otherButtonTitles:@"Maybe Next Time...",nil];
    [alert show];


}

Upvotes: 0

Views: 65

Answers (1)

Kosuke Ogawa
Kosuke Ogawa

Reputation: 7451

You don't need to use TapGesture. Use didSelectAnnotationView method.

e.g.

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

    if ([view.annotation isKindOfClass:[meetupAnn class]]) {
        // Call calloutTappedTwo
    } else if ([view.annotation isKindOfClass:[MKPointAnnotation class]]) {
        // Call calloutTappedThree
    }
}

Upvotes: 1

Related Questions