Reputation: 29
I have a map and I have added 12 Annotations to it perfectly, what I would like is so that when someone taps on these Annotations it would have an info button to the right of the annotation which when tapped would show directions to the annotation in the maps application. I've written the function to open the maps application with the desired coordinates I'm just not sure how one would go adding the info button to the annotation and making it execute the function when tapped.
Edit: I require this to be done in Swift.
Upvotes: 2
Views: 3730
Reputation: 1614
For Swift 2 I did it this way:
let buttonType = UIButtonType.InfoDark
barAnnotView?.rightCalloutAccessoryView = UIButton(type: buttonType)
Upvotes: 2
Reputation: 543
you can use ViewForAnnotation Method of mapview as below
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"loc"];
annotationView.canShowCallout = YES;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
}
also you can add method for call accessory view
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
[self performSegueWithIdentifier:@"DetailsIphone" sender:view];
}
This way you can navigate to direction on info button.
Updated for Swift code
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!,
calloutAccessoryControlTapped control: UIControl!) {
if control == view.rightCalloutAccessoryView {
println("Disclosure Pressed! \(view.annotation.subtitle)"
}
}
you can use this for swift language.
Please add this code for viewforAnnotation :
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if !(annotation is CustomPointAnnotation) {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView.canShowCallout = true
anView.rightCalloutAccessoryView = UIButton.buttonWithType(.InfoDark) as UIButton
}
else {
anView.annotation = annotation
}
let cpa = annotation as CustomPointAnnotation
anView.image = UIImage(named:cpa.imageName)
return anView
}
make sure that you have added the "MKMapViewDelegate"
Upvotes: 7