如何让Pin更改它的图像?

时间:2016-03-29 13:07:21

标签: swift mapkit

我有问题,我的注释仍然是一个针!我想要按钮和图片,显示有关用户的信息并且它有效。但它并没有改变地图上引脚的外观! 这是我的annotationView:

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

    // 2
    if annotation.isKindOfClass(pinAnnotation.self) {
        // 3
        var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

        if annotationView == nil {


            //4
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = true


            // 5
            let btn = UIButton(type: .InfoDark)
            annotationView!.rightCalloutAccessoryView = btn



            resultImgArray[0].getDataInBackgroundWithBlock {
                (imageData: NSData?, error: NSError?) -> Void in

                if error == nil {

                    let imageView = UIImageView(frame:CGRectMake(0,0,45,45))
                    imageView.image = UIImage(data: imageData!)
                    annotationView!.leftCalloutAccessoryView = imageView

                }


            }


        } else {
            // 6
            annotationView!.annotation = annotation
        }

        return annotationView
    }

    return nil

}

这是我的pinAnnotationView.swift作为MKAnnotationView:

import UIKit
import MapKit
import CoreLocation

class pinAnnotationView: MKAnnotationView {

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

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {

        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)

        var frame = self.frame
        frame.size = CGSizeMake(80, 80)
        self.frame = frame
        self.backgroundColor = UIColor.clearColor()
        self.centerOffset = CGPointMake(-5, -5)
    }

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


    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code

        UIImage(named: "carlogo.png")?.drawInRect(CGRectMake(30, 30, 30, 30))

    }
}

这是图像
enter image description here

1 个答案:

答案 0 :(得分:0)

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation.isMember(of: MKUserLocation.self) {
            return nil
        }
        let identifier = "someID"
        var dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
        if dequeuedAnnotationView == nil{
            dequeuedAnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            configureDetailView(annotationView: dequeuedAnnotationView!)

        }
        return dequeuedAnnotationView
    }    

func configureDetailView(annotationView: MKAnnotationView) {
            var frame = self.frame
            frame.size = CGSizeMake(80, 80)
            self.frame = frame
            self.backgroundColor = UIColor.clearColor()
            self.centerOffset = CGPointMake(-5, -5)
            annotationView.image = UIImage(named:"yourImage")       
    }

在这种情况下使用annotationView.detailCalloutAccessoryView =“yourView”将是框架。

我希望能为你效力