Swift - 在MKCircle中设置多个fillColor

问题描述 投票:0回答:1

当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)
swift mapkit fill radius mkcircle
1个回答
0
投票

这是我对您的问题的解决方案的一阶近似。

第一个问题是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)
© www.soinside.com 2019 - 2024. All rights reserved.