Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Disclosure Button to MKPointAnnotation

Tags:

ios

To create a map annotation in a storyboard iOS project, I used:

    CLLocationCoordinate2D annotationCoord3;

        annotationCoord3.latitude = 34.233129;
        annotationCoord3.longitude = -118.998644;

        MKPointAnnotation *annotationPoint3 = [[MKPointAnnotation alloc] init];
        annotationPoint3.coordinate = annotationCoord3;
        annotationPoint3.title = @"Another Spot";
        annotationPoint3.subtitle = @"More than a Fluke";
        [_mapView addAnnotation:annotationPoint3];

It works great but I'd like to add a disclosure button so I can push seque to a new view controller and display image. Is this possible?

Thx in advance,

--bd--

like image 904
user1443032 Avatar asked Jun 07 '12 19:06

user1443032


2 Answers

declare your class to be a MKMapViewDelegate. Then, add

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


    MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"String"];
    if(!annotationView) {   
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"String"];
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    }

    annotationView.enabled = YES;
    annotationView.canShowCallout = YES;

    return annotationView;
}

Then you add:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    // Go to edit view
    ViewController *detailViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
   [self.navigationController pushViewController:detailViewController animated:YES];

}

... ViewController can be anything you defined (I use nib-files...)

like image 119
Axel Avatar answered Nov 16 '22 01:11

Axel


Axel's answer is correct (I just upvoted): you must implement the MKMapViewDelegate and assign an instance of it to the Delegate property of the MKMapView. For those using MonoTouch, here's the port:

class MapDelegate : MKMapViewDelegate
{
    public override MKAnnotationView GetViewForAnnotation (MKMapView mapView, NSObject annotation)
    {
        MKAnnotationView annotationView = mapView.DequeueReusableAnnotation ("String");
        if (annotationView == null) {
            annotationView = new MKAnnotationView(annotation, "String");
            annotationView.RightCalloutAccessoryView = new UIButton(UIButtonType.DetailDisclosure);
        }
        annotationView.Enabled = true;
        annotationView.CanShowCallout = true;
        return annotationView;
    }

    public override void CalloutAccessoryControlTapped (MKMapView mapView, MKAnnotationView view, UIControl control)
    {
        // Push new controller to root navigation controller.
    }
}
like image 43
Diego Avatar answered Nov 16 '22 03:11

Diego