Swift中的自定义AnnotationView

时间:2017-09-20 09:51:55

标签: ios swift mapkit mkannotation

我正在尝试制作自定义AnnotationView。实际上我做到了,但我不知道如何自定义每个地址的坐标,名称和其他自定义标签。这是自定义AnnotationView函数。

func createAnnotationView(annotation: MKAnnotation,name: String,piece: String,distance: String)-> MKAnnotationView{
    let annotationView = MKAnnotationView()
    annotationView.frame = CGRect(x: 0, y: 0, width: 150, height: 45)
    annotationView.backgroundColor = UIColor.white
    annotationView.layer.cornerRadius = 7
    annotationView.layer.shadowColor = UIColor.lightGray.cgColor
    annotationView.layer.shadowRadius = 3
    annotationView.layer.shadowOffset = CGSize.zero
    annotationView.layer.shadowOpacity = 1.0
    let orangeView = UIView()
    orangeView.frame = CGRect(x: 0, y: 0, width: 45, height: 45)
    orangeView.layer.cornerRadius = 7
    orangeView.backgroundColor = UIColor.init(red: 245/255, green: 166/255, blue: 35/255, alpha: 0.85)
    let imageView = UIImageView()
    imageView.frame = CGRect(x: 10, y: 10, width: 25, height: 25)
    imageView.image = UIImage(named: "shopStore")
    let storeName = UILabel()
    storeName.frame = CGRect(x: 50, y: 5, width: 95, height: 20)
    storeName.font = UIFont(name: "AvenirNext-DemiBold", size: 10)
    storeName.textColor = UIColor().rgb(red: 27.0, green: 90.0, blue: 122.0)
    storeName.text = name
    let pieceLabel = UILabel()
    pieceLabel.frame = CGRect(x: 50, y: 20, width: 50, height: 20)
    pieceLabel.font = UIFont(name: "AvenirNext-Medium", size: 8)
    pieceLabel.textColor = UIColor().rgb(red: 27.0, green: 90.0, blue: 122.0)
    pieceLabel.text = "\(piece) Ürün"
    let distanceLabel = UILabel()
    distanceLabel.frame = CGRect(x: 110, y: 20, width:25, height: 20)
    distanceLabel.font = UIFont(name: "AvenirNext-Medium", size: 8)
    distanceLabel.textColor = UIColor().rgb(red: 27.0, green: 90.0, blue: 122.0)
    distanceLabel.text = "\(distance)"

    annotationView.addSubview(distanceLabel)
    annotationView.addSubview(pieceLabel)
    annotationView.addSubview(storeName)
    orangeView.addSubview(imageView)
    annotationView.addSubview(orangeView)
    annotationView.annotation = annotation
    mapView.addAnnotation(annotationView.annotation!)
    return annotationView
}

我在这里调用我的功能;

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if !(annotation is MKUserLocation){
            return createAnnotationView(name: "Name of address", piece: "3", distance: "1000m")

        }else{
            return nil
        }
    }

基本上我想添加名称坐标和距离参数的新引脚。我该怎么办?

谢谢。

0 个答案:

没有答案