views:

1195

answers:

2

Hey

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  A: 

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;
   }
}
Ryan Ferretti
+2  A: 

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;
        [defaultPinID release];

    }

    return pinAnnotation;
    [pinAnnotation release];
}
Griffo
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
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