如何使自定义MKPolyline在SWIFT与额外的参数 - 颜色 [英] How to make custom MKPolyline in SWIFT with additional argument - color

查看:288
本文介绍了如何使自定义MKPolyline在SWIFT与额外的参数 - 颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何帮助我用自定义 MKPolyline 和附加参数Color?

Can any help me with making custom MKPolyline with additional argument Color?

CustomPolyline.swift

import Foundation
import MapKit
class CustomPolyline : MKPolyline {
    let coordinates: UnsafeMutablePointer<CLLocationCoordinate2D>
    let count : Int = 0
    let color : String = "ff0000"
    init(coordinates: UnsafeMutablePointer<CLLocationCoordinate2D>, count: Int, color: String) {

        self.coordinates = coordinates
        self.count = count
        self.color = color
    }
}

Init

Polyline = CustomPolyline(coordinates: &Path, count: Path.count, color: "ff0000")
self.mapView.addOverlay(Polyline)

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {      
        if (overlay is CustomPolyline) {
            var pr = MKPolylineRenderer(overlay: overlay);
            pr.strokeColor = UIColor.colorWithRGBHex(0xff0000).colorWithAlphaComponent(0.5);
            pr.lineWidth = 10;
            return pr;
        }

        return nil
    }



解决方案不工作,我不能弄清楚为什么。折线根本不可见。我是SWIFT的初学者,所以我认为这个问题是我的 CustomPolyline 类。
感谢您的帮助。

My solution doesn't work and I can't figure it out why. Polylines isn't visible at all. I'm a beginner in SWIFT so I think that problem is with my CustomPolyline class. Thanks for help.

推荐答案

可以做的比我更简单:

import Foundation
import MapKit

class CustomPolyline : MKPolyline {

    var color: String?
}

Init

cPolyline = CustomPolyline(coordinates: &Path, count: Path.count)
cPolyline.color = "#ff0000"
self.mapView.addOverlay(cPolyline)

func mapView(mapView: MKMapView!, rendererForOverlay overlay: CustomPolyline!) -> MKOverlayRenderer! {      

            var pr = MKPolylineRenderer(overlay: overlay);
            pr.strokeColor = UIColor(rgba: overlay.color);
            pr.lineWidth = 10;
            return pr;

    }

这篇关于如何使自定义MKPolyline在SWIFT与额外的参数 - 颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆