任何人都可以帮我定制吗MKPolyline
带有附加参数 Color?
自定义多段线.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
班级。
感谢帮助。
它可以比我想象的更简单:
Class
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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)