当markerType不同时,我试图改变我的MKCircle
fillColor
。例如,如果markerType
是“airport”,那么填充应该是红色,如果是“Sea Plane Base”,则填充应该是黑色。
// MARK: Radius overlay
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKCircle {
let circle = MKCircleRenderer(overlay: overlay)
for markerType in airports {
if markerType.markerType == "airport" {
circle.strokeColor = UIColor.red
circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
circle.lineWidth = 1
} else {
circle.strokeColor = UIColor.black
circle.fillColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1)
circle.lineWidth = 1
}
}
return circle
} else {
return MKPolylineRenderer()
}
}
我正在使用此功能来显示半径。
func mainAirportRadius(radius: CLLocationDistance) {
//MARK: Airport location
for coordinate in airports {
let center = coordinate.coordinate
let circle = MKCircle(center: center, radius: radius)
mapView.add(circle)
}
}
然后我用viewDidLoad
方法调用它
mainAirportRadius(radius: 8046.72)
这是我对您的问题的解决方案的一阶近似。
第一个问题是airports
似乎是一个全局列表或其他全局可迭代的。在任何对mapView
的调用中,它将生成circle
并迭代airports
,为circle
中的每个元素更改airports
属性一次。即,每次调用mapView
时,您都会看到完全相同的行为。
你应该做的是首先更新你的函数定义以包含markerType
并删除iterable:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay, markerType: String) -> MKOverlayRenderer {
if overlay is MKCircle {
let circle = MKCircleRenderer(overlay: overlay)
if markerType == "airport" {
circle.strokeColor = UIColor.red
circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
circle.lineWidth = 1
} else {
circle.strokeColor = UIColor.black
circle.fillColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1)
circle.lineWidth = 1
}
return circle
} else {
return MKPolylineRenderer()
}
}
然后,在打电话给mapView
时,传递那个机场的markerType:
mapView.add(circle,coordinate.markerType)