可以帮助我使用附加参数 Color 制作自定义 MKPolyline
吗?
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
}
}
初始化
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
类上。
感谢您的帮助。
最佳答案
它可以比我想象的更简单:
类
import Foundation
import MapKit
class CustomPolyline : MKPolyline {
var color: String?
}
初始化
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;
}
关于ios - 如何使用附加参数在 SWIFT 中制作自定义 MKPolyline - 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26010977/