当markerType不同时,我试图更改我的MKCircle fillColor。例如,如果markerType为“机场”,则填充应为红色,如果为“海平面基准”,则填充应为黑色。

// 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并删除可迭代项:

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)

关于swift - Swift-在MKCircle中设置多个fillColor,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52322069/

10-12 19:51