Refresh MKAnnotationView on a MKMapView

前端 未结 7 468
天涯浪人
天涯浪人 2021-01-31 09:27

I\'d like to a-synchronically load images for my custom MKAnnotationView; I\'m already using the EGOImageView-framework (it goes very well with UITableViews), but I fail to make

7条回答
  •  野的像风
    2021-01-31 09:47

    Thank you @Vladimir for the help! Your answer inspired my answer. I made some changes to make it clearer.

    func addAnnotation(coordinate: CLLocationCoordinate2D) {
        let annotation = Annotation()
        annotation.coordinate = coordinate
        annotations.append(annotation)
        mapView.addAnnotation(annotation)
        for index in 0..

    Also the protocol in MKMapview needs to implement:

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        let view = mapView.dequeueReusable(withClass: AnnotationView.self, annotation: annotation)
        if index == 0 {
            view.imageView.image = NSImage(named: "icon_positioning")
        } else {
            view.imageView.image = NSImage(named: "icon_positioning")
        }
        return view
    }
    

    By the way, the AnnotationView initialization here is:

    public extension MKMapView {
        func dequeueReusable(withClass name: T.Type,annotation: MKAnnotation?) -> T {
            if let view = dequeueReusableAnnotationView(withIdentifier: T.identifier()) as? T {
                return view
            }
            let view = T.init(annotation: annotation, reuseIdentifier: T.identifier())
            return view
        }
    }
    

    Thanks again @Vladimir for the answer

提交回复
热议问题