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--
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...)
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.
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With