Big Green Alligator
Big Green Alligator

Reputation: 1185

How to add right callout accessory to custom annotation in swift?

I've got the following custom annotation class:

import UIKit
import MapKit

class LocationMapAnnotation: NSObject, MKAnnotation {
    var title: String?
    var coordinate: CLLocationCoordinate2D
    var location: Location

    init(title: String, coordinate: CLLocationCoordinate2D, location: Location) {
        self.title = title
        self.coordinate = coordinate
        self.location = location
    }
}

I am loading the annotations into a map view like this:

for i in 0..<allLocations.count{
            //Add an annotation
            let l: Location = self.allLocations[i] as! Location
            let coordinates = CLLocationCoordinate2DMake(l.latitude as Double, l.longitude as Double)
            let annotation = LocationAnnotation(title: l.name, coordinate: coordinates, location: l)
            mapView.addAnnotation(annotation)
        }

How can I add an (i) info button to the right of the annotation in a map view. Also is there an easy way to animate it?

Thanks.

Upvotes: 0

Views: 2684

Answers (1)

Wolverine
Wolverine

Reputation: 4329

Try this :

here "Sample" class that implemented the MKAnnotation protocol. You need to adjust this as per your annotation type.

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "Sample"

if annotation.isKindOfClass(Sample.self) {
    if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) {

 // Reuse Annotationview

        annotationView.annotation = annotation
        return annotationView
    } else {

 // Create Annotation

        let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:identifier)
        annotationView.enabled = true
        annotationView.canShowCallout = true

 // Here I create the button and add in accessoryView

        let btn = UIButton(type: .DetailDisclosure)
        annotationView.rightCalloutAccessoryView = btn
        return annotationView
    }
}
return nil
}

Upvotes: 2

Related Questions