0

I am trying to create a custom annotation callout under swift 4. I have spend last a few days google around, the examples I could find were either swift 2 or 3, and I could not understand some of the parts.

I have created a xib view file and its controller:

import UIKit
import MapKit

class ForHireAnnotationCallOut: UIView {

@IBOutlet weak var coImage: UIImageView!
@IBOutlet weak var coName: UILabel!
@IBOutlet weak var coServiceType: UILabel!
@IBOutlet weak var coExp: UILabel!
@IBOutlet weak var coHourlyRate: UILabel!
@IBAction func ocDetailsBtn(_ sender: Any) {

}
var coAnnotation = MKPointAnnotation()
internal var coData: User?{
    didSet{
        coName.text = coData?.name
        coServiceType.text = coData?.serviceType
        coExp.text = (coData?.exp.replacingOccurrences(of: "Years", with: "Yrs"))! + " Exp."
        coHourlyRate.text = coData?.hourlyRate

        self.layoutIfNeeded()
    }
}


internal var preferredContentSize:CGSize{

        return CGSize(width: 0, height: 0)

}

override init(frame: CGRect) {
    super.init(frame: frame)
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override func awakeFromNib() {
    super.awakeFromNib()
}

}

User struct contains:

public struct User: Equatable {
var uid: String
var name: String
var phone: String
var serviceType: String
var exp: String
var hourlyRate: String
var pictureUrl: String
}

Custom annotation class:

public class myAnnotation: NSObject, MKAnnotation {
public var coordinate: CLLocationCoordinate2D
public var title: String?
public var subtitle: String?
var userKey: String?
public var exp: String?
public var hourlyRate: String?

override init() {
    self.coordinate = CLLocationCoordinate2D()
    self.title = nil
    self.subtitle = nil
    self.userKey = nil
    self.exp = nil
    self.hourlyRate = nil
}

}

My understanding is, in order to have custom call out, you will need to use mapView viewFor annotation function to use the xib view.

I found some example from another thread by @Babac and modified to something like this in swift 4:

func mapView(_ mapView: MKMapView, viewFor annotation: myAnnotation) -> MKAnnotationView? {
// Don't want to show a custom image if the annotation is the user's location.
guard !(annotation is MKUserLocation) else {
    return nil
}

// Better to make this class property 
let annotationIdentifier = "AnnotationIdentifier" //<---not very sure why we need this

var annotationView: MKAnnotationView?
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) {
    annotationView = dequeuedAnnotationView
    annotationView?.annotation = annotation
}
else {
    let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
    av.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    annotationView = av
}

if let annotationView = annotationView {
    // Configure your annotation view here
    annotationView.canShowCallout = true
    annotationView.image = UIImage(named: "yourImage")
}

return annotationView
}

Other than annotationIdentifier, I do not understand where I should include my xib view and its controller in this func for the call out. Also how do I pass the values to the xib view controller?

Thanks

solohan83
  • 21
  • 4

0 Answers0