0

我正在尝试在 MKPointAnnotations 上添加不同的图像。这是我的代码,当只有一个注释时它工作正常。我面临在不同注释上添加不同图像的问题。

func showDevices(Devs: Array<Devices.Device>){

    if let annotations = self.MainMap?.annotations {
        self.MainMap.removeAnnotations(annotations)
    }

    if let overlays = self.MainMap?.overlays {
        self.MainMap.removeOverlays(overlays)
    }
    if Devs.count > 200{

    }
    else{
        for Dev in Devs {


            let lat: CLLocationDegrees = Dev.lt!
            let lon: CLLocationDegrees = Dev.ln!

            let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)


            let mapAnnotation = MKPointAnnotation()
            mapAnnotation.title = Dev.dn
            mapAnnotation.subtitle = Dev.dn
            mapAnnotation.coordinate = coordinate

            iconImage = Common.getIcon(Dev.icon!, ign: Dev.ign!)

            DispatchQueue.main.async {
                self.MainMap.addAnnotation(mapAnnotation)
            }
        }
        Common.endLoadingInNavigationCtrl(self)
    }
}

这是我的mapView( :viewFor)功能:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation ) -> MKAnnotationView? {
    // Don't want to show a custom image if the annotation is the user's location.
    guard !annotation.isKind(of: MKUserLocation.self) else {
        return nil
    }
    let annotationIdentifier = "AnnotationIdentifier"

    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 var annotationView = annotationView {
            //Configure your annotation view here
            //annotationView.image = UIImage(named: iconImage!)
        if iconImage != nil {
            annotationView = addImageToAnnotation(annotationView, rotate: degrees, imageUrl: iconImage!)
        }

    }

    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        annotationView?.canShowCallout = true
        // Resize image
        let pinImage = UIImage(named: "pin maps.png")
        let size = CGSize(width: 60, height: 90)
        UIGraphicsBeginImageContext(size)
        pinImage!.draw(in: CGRect(x: 0, y: 1, width: size.width, height: size.height))
            //(0, 0, size.width, size.height))
        let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        annotationView?.image = resizedImage

        let rightButton: AnyObject! = UIButton(type: UIButtonType.detailDisclosure)
        annotationView?.rightCalloutAccessoryView = rightButton as? UIView
    }
    else {
        annotationView?.annotation = annotation
    }

    return annotationView

}

将图像添加到引脚的功能:

func addImageToAnnotation( _ annotationView:MKAnnotationView , rotate : CGFloat? , imageUrl : String) -> MKAnnotationView {


        var img = UIImage(named: imageUrl)
        let size = CGSize(width:50 ,height:50);
        UIGraphicsBeginImageContext(size)
        img?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        annotationView.canShowCallout = true
        annotationView.image = rotate != nil ? img?.imageRotatedByDegrees(rotate!, flip: false) : img


    return annotationView
}

我想为地图上的每个注释添加不同的图像,但它会覆盖所有注释上的单个图像。

4

1 回答 1

0

创建一个自定义MKAnnotation类。

class ImageAnnotation : NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?
    var imageUrl: String?

    override init() {
        self.coordinate = CLLocationCoordinate2D()
        self.title = nil
        self.subtitle = nil
        self.imageUrl = nil
    }
}

设置数据并将注释添加到您的 mapView。

    let annotation = ImageAnnotation()
    annotation.coordinate = coordinate1
    annotation.title = "title"
    annotation.subtitle = "subtitle"
    annotation.imageUrl = "https:// ..."
    self.mapView.addAnnotation(annotation)

实现mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation)委托方法。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    guard let annotation = annotation as? ImageAnnotation else {
        return nil
    }

    let reuseId = "Pin"
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
    if pinView == nil {
        pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView?.canShowCallout = true
        let data = NSData(contentsOf: URL(string: annotation.imageUrl!)!)
        pinView?.image = UIImage(data: data! as Data)
    }
    else {
        pinView?.annotation = annotation
    }

    return pinView
}
于 2018-10-22T15:25:30.823 回答