Klipper
Klipper

Reputation: 5

How to make the callout button in an annotation in MKMapKit go to another view controller

I want the button in the callout to go to another view controller to display extra information, and then be able to come back to the map view. the map is inside of a tabbed view controller. At the moment, if you click on the button in the callout, the app crashes and gives you a thread error. Not sure on what to do at the moment.

this is the header file:

#import UIKit/UIKit.h>
#import MapKit/MapKit.h>
#define METERS_PER_MILE 1609.344

@interface MSKSecondViewController :UIViewController<MKMapViewDelegate>
{
    IBOutlet MKMapView *stillwellMapView;
}
@property (nonatomic, assign) CLLocationCoordinate2D mainEntranceToStillwellCoordinate;
@end

and this is the implementation file:

@interface MSKSecondViewController ()
@end
@implementation MSKSecondViewController
@synthesize mainEntranceToStillwellCoordinate;

- (void)viewDidLoad
{
    [super viewDidLoad];
    stillwellMapView.delegate=self;
    // Do any additional setup after loading the view, typically from a nib.
    [self mainEntrancetoStillwellCoordinate];
    [self bambooForestCoordinate];
}

- (void)mainEntrancetoStillwellCoordinate
{
    MKPointAnnotation * main = [[MKPointAnnotation alloc]init];
    CLLocationCoordinate2D mainLocation;
    mainLocation.latitude = 40.831685;
    mainLocation.longitude = -73.477453;
    [main setCoordinate:mainLocation];
    [main setTitle:@"Entrance"];
    [main setSubtitle:@"Main"];
    [stillwellMapView addAnnotation:main];
}
- (void)bambooForestCoordinate
{
    MKPointAnnotation * bambooForest = [[MKPointAnnotation alloc]init];
    CLLocationCoordinate2D bambooForestLocation;
    bambooForestLocation.latitude = 40.829118;
    bambooForestLocation.longitude = -73.466443;
    [bambooForest setCoordinate:bambooForestLocation];
    [bambooForest setTitle:@"Bamboo Forest"];
    [bambooForest setSubtitle:@"Exit to Woodbury"];
    [stillwellMapView addAnnotation:bambooForest];
}

- (void)viewDidUnload
{
    stillwellMapView = nil;
    [super viewDidUnload]; 
    // Release any retained subviews of the main view.
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation: 
(id<MKAnnotation>)annotation    
{
    MKPinAnnotationView * annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    UIButton *entranceButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [entranceButton addTarget:self action:@selector(entranceButtonPressed:) 
forControlEvents:UIControlEventTouchUpInside];
    annView.rightCalloutAccessoryView = entranceButton;
    entranceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    entranceButton.frame = CGRectMake(0, 0, 23, 23);
    entranceButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    entranceButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [UIButton buttonWithType:UIButtonTypeDetailDisclosure];  
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)viewWillAppear:(BOOL)animated 
{  
    //the coordinates in which the map shows once loaded
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = 40.831922;
    zoomLocation.longitude= -73.476353; 
    //the amount of area shown by the map when it loads
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation,  
0.15*METERS_PER_MILE, 0.15*METERS_PER_MILE); 
    MKCoordinateRegion adjustedRegion = [stillwellMapView regionThatFits:viewRegion];                
    [stillwellMapView setRegion:adjustedRegion animated:YES]; 
}
@end

Upvotes: 0

Views: 639

Answers (2)

Prithivi
Prithivi

Reputation: 1

In the mapView:viewForAnnotation: delegate method

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

// add the following line

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
pinView.rightCalloutAccessoryView = rightButton;

and then implement the code to navigate to other view controller in the following delegate method

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

Make sure view controller is embedded inside navigation controller

Upvotes: 0

Apurv
Apurv

Reputation: 17186

Add the event which you have registered on Touch Up Inside with entranceButton.

-(void) entranceButtonPressed:(UIButton *)sender
{
//Write the controller push code here
}

Also, in the code you have reinitialize the entranceButton once it is assigned to rightCalloutAccessoryView.

Upvotes: 1

Related Questions