这是我绘制第一行的代码, 对于另一种颜色的另一种颜色我该怎么办?????
func mapView(mapView : MKMapView! , rendererForOverlay overlay: MKOverlay!) ->MKOverlayRenderer! {
if overlay is MKPolyline {
var polyLineRenderer = MKPolylineRenderer(overlay: overlay)
polyLineRenderer.strokeColor = UIColor.blackColor()
polyLineRenderer.lineWidth = 10
return polyLineRenderer
}
return nil
}
答案 0 :(得分:5)
最后我找到了方法:
在课程的顶部,我把
var toGo : MKPolyline?
var toCome : MKPolyline?
然后在视图中加载:
var polyLineGoes = MKPolyline(coordinates: &coordinateGoes, count: coordinateGoes.count)
toGo = polyLineGoes
mapView.addOverlay(polyLineGoes)
var polyLineComes = MKPolyline(coordinates: &coordinateComes, count: coordinateComes.count)
toCome = polyLineComes
mapView.addOverlay(polyLineComes)
在课程结束时:
func mapView(mapView : MKMapView! , rendererForOverlay overlay: MKOverlay!) ->MKOverlayRenderer! {
if overlay is MKPolyline {
if ( toGo != nil) && (toCome != nil ) {
if overlay as? MKPolyline == toGo {
var polyLineRenderer = MKPolylineRenderer(overlay: overlay)
polyLineRenderer.strokeColor = UIColor.redColor()
polyLineRenderer.lineWidth = 3
return polyLineRenderer
} else if overlay as? MKPolyline == toCome {
print(overlay.description)
var polyLineRenderer = MKPolylineRenderer(overlay: overlay)
polyLineRenderer.strokeColor = UIColor.blueColor()
polyLineRenderer.lineWidth = 3
return polyLineRenderer
}
}
}
return nil
}
答案 1 :(得分:1)
我遇到了同样的问题,但我找到了解决问题的另一种方法。
在mapView中代码为:
func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer!{
if overlay is MKPolyline {
var polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = variables.lineColor
polylineRenderer.lineWidth = 4
return polylineRenderer
}
return nil
}
我只有一个名为variables的结构,它包含变量lineColor。另外,我有另一个CONSTANT结构,它有一些CONSTANT,使mapView类中的代码更具可读性。这两个结构中的代码将是:
struct CONSTANT {
static let greenColor = UIColor.greenColor().colorWithAlphaComponent(0.5)
static let blueColor = UIColor.blueColor().colorWithAlphaComponent(0.5)
static let redColor = UIColor.redColor().colorWithAlphaComponent(0.5)
}
struct variables {
// let the default lineColor be green
static var lineColor = CONSTANT.greenColor
}
有了这些,我只需将我的variables.lineColor更改为CONSTANT结构中的颜色。例如,
lineColor = CONSTANT.greenColor
mapView.addOverlay(myPolyLine, level: MKOverlayLevel.AboveRoads)
lineColor = CONSTANT.blueColor
mapView.addOverlay(myPolyLine, level: MKOverlayLevel.AboveRoads)
lineColor = CONSTANT.redColor
mapView.addOverlay(myPolyLine, level: MKOverlayLevel.AboveRoads)
首先发布在这里:D
答案 2 :(得分:0)
我找到了另一种方法。
首先,我们需要扩展MKPolyline
:
extension MKPolyline {
struct ColorHolder {
static var _color: UIColor?
}
var color: UIColor? {
get {
return ColorHolder._color
}
set(newValue) {
ColorHolder._color = newValue
}
}
}
在viewDidLoad
中,我们现在可以为每条折线指定一种颜色:
var polyline1 = MKPolyline(coordinates: coordinates1, count: coordinates1.count)
polyline1.color = UIColor(.red)
mapView.addOverlay(polyline1)
var polyline2 = MKPolyline(coordinates: coordinates2, count: coordinates2.count)
polyline2.color = UIColor(.green)
mapView.addOverlay(polyline2)
最后,我们有了 mapView
函数:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if let overlay_ = overlay as? MKPolyline {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = overlay_.color
return renderer
} else {
return MKOverlayRenderer()
}
}