2

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.

user4011770
  • 29
  • 1
  • 4

2 Answers2

7

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"

BKjadav
  • 543
  • 2
  • 16
  • Thanks, but I believe the code above is for Obj-C, I'm looking for swift. – user4011770 Jul 03 '15 at 14:24
  • I've added this code and it works but I can't see any changes or an info button? – user4011770 Jul 03 '15 at 14:30
  • Alright, I'm getting a "Use of Undeclared type 'CustomPointAnnotation'" error now. Just to confirm, my class is: class locations: UIViewController, MKMapViewDelegate { // code here } – user4011770 Jul 03 '15 at 14:39
2

For Swift 2 I did it this way:

let buttonType = UIButtonType.InfoDark
barAnnotView?.rightCalloutAccessoryView = UIButton(type: buttonType)
icebat
  • 4,696
  • 4
  • 22
  • 36
Kegham K.
  • 1,589
  • 22
  • 40