parisianastro97
parisianastro97

Reputation: 51

Set different actions for different annotations in MKMapView

I am trying to find a way to make multiple annotation disclosure buttons open other views. For example, annotation1 disclosure button needs to open ViewController 1.xib, annotation2 needs to open ViewController2.xib, and so on. Is this possible?

I've got this code so far, which sets the location according to coordinates, and sets the user location.

- (void)viewDidLoad
{
[super viewDidLoad];
_mapView.showsUserLocation = YES;
CLLocationCoordinate2D annotationCoord;

annotationCoord.latitude = 40.714353;
annotationCoord.longitude = -74.005973;
coord = 1;
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = @"New York";
[_mapView addAnnotation:annotationPoint];

CLLocationCoordinate2D annotationCoord1;

annotationCoord1.latitude = 51.511214;
annotationCoord1.longitude = -0.119824;
coord = 2;
MKPointAnnotation *annotationPoint1 = [[MKPointAnnotation alloc] init];
annotationPoint1.coordinate = annotationCoord1;
annotationPoint1.title = @"London";
[_mapView addAnnotation:annotationPoint1];
}

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *mapPin = nil;
if(annotation != map.userLocation)
{
    static NSString *defaultPinID = @"defaultPin";
    mapPin = (MKPinAnnotationView *)[map dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if (mapPin == nil )
    {
        mapPin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation
                                                  reuseIdentifier:defaultPinID] autorelease];
        mapPin.canShowCallout = YES;
        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        mapPin.rightCalloutAccessoryView = infoButton;
    }
    else
        mapPin.annotation = annotation;

}
return mapPin;
}

What do I put into here:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
//   ???
}

All help is appreciated. Thanks

Upvotes: 1

Views: 541

Answers (1)

Ludvig Odin
Ludvig Odin

Reputation: 69

You could do something like this,

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
  if ([view.annotation.title isEqualToString:@"The titel of your annotation"]) 
  {
    // Do something
  }
}   

Upvotes: 1

Related Questions