How to set image on MKAnnotation in MKMapView

前端 未结 5 772
盖世英雄少女心
盖世英雄少女心 2021-01-06 23:53

I am developing an app for chating, I have to display all friends on a Map with their image. Please provide guidance to implement it.

I have used following code...

5条回答
  •  不思量自难忘°
    2021-01-07 00:09

    A swift4 version of Nitesh Borad Answer:

    func mapView(_ map: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        let annotationViewReuseIdentifier = "annotationViewReuseIdentifier"
    
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationViewReuseIdentifier) as? MKAnnotationView
    
        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationViewReuseIdentifier)
        }
    
        // here you can assign your friend's image    
        annotationView?.image = UIImage(named: "friend_image.png")
        annotationView?.annotation = annotation
    
        // add below line of code to enable selection on annotation view
        annotationView?.canShowCallout = true
    
        return annotationView
    }
    

提交回复
热议问题