7

Anyone know if there's anyway to get a button onto an annotation?

I'd like the location to be selectable - so you can say.. select the location and get all the events at that location by clicking on the button.

is this possible?

w://

2 Answers2

13

Here's the code I used for my annotation, it includes a button on the right side of the bubble. You can set an IBAction to push a new view onto the stack to display whatever you want

- (MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
    MKPinAnnotationView *pinAnnotation = nil;
    if(annotation != mapView.userLocation) 
    {
        static NSString *defaultPinID = @"myPin";
        pinAnnotation = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinAnnotation == nil )
            pinAnnotation = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];

        pinAnnotation.canShowCallout = YES;

        //instatiate a detail-disclosure button and set it to appear on right side of annotation
        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        pinAnnotation.rightCalloutAccessoryView = infoButton;

    }

    return pinAnnotation;
}
conorgriffin
  • 4,282
  • 7
  • 51
  • 88
  • 4
    In addition to this, you'll want to implement the - mapView:annotationView:calloutAccessoryControlTapped: delegate method to handle taps on the disclosure button you have in the annotation callout. – Brad Larson Mar 03 '10 at 02:19
  • 2
    Why are you releasing `defaultPinID`? You don't have to release it. The release of `pinAnnotation` in the end is for nothing, because it never gets called. – testing Oct 05 '10 at 10:38
  • 1
    just curious - this is tagged monotouch so why not use a C# example? – Jack Marchetti Oct 28 '11 at 22:30
5

I just helped someone else with this in objective c, but I'm sure the concept is the same with mono. You need to create a custom MKAnnotationView object and override the GetViewForAnnotation (viewForAnnotation in obj-c) method of your MKMapViewDelegate class... check out the other question.

When you create your custom MKAnnotationView object it is basically a UIView made for map annotations... you can just add your button and other info to the view and it will show up when the user hits the annotation.

Here some rough code for the delegate method:

public override MKAnnotationView GetViewForAnnotation(
                                         MKMapView mapView,NSObject annotation) {
      var annotationId = "location";
      var annotationView = mapView.DequeueReusableAnnotation(annotationId);
      if (annotationView == null) {
         // create new annotation
         annotationView = new CustomAnnotationView(annotation, annotationId);
      }
      else {
         annotationView.annotation = annotation;
      }
      annotation.CanShowCallout = true;
      // setup other info for view
      // ..........

      return annotationView;
   }
}
Community
  • 1
  • 1
Ryan Ferretti
  • 2,891
  • 2
  • 27
  • 37