倒圆图填充swift 4

时间:2019-03-28 09:59:16

标签: swift4 mapkit mkoverlay

我正在尝试在地图上绘制一个圆,当用户选择选项1时,圆圈用蓝色填充,当用户选择选项2时,整个地图将用蓝色填充,而圆圈区域为无色。那怎么可能?

Storage::delete('public/images/0HHJ5DygYUXuLC5dgu14KJTPvCEaxhISV8tpb4Pq.png');

1 个答案:

答案 0 :(得分:1)

对于Option-2,要绘制一个具有填充的外部透明孔的圆,请使用MKPolygon.polygonWithPoints:count:interiorPolygons:和internalPolygons参数作为圆MKPolygon,如下所示:

MKPolygon(coordinates: WORLD_COORDINATES, count: WORLD_COORDINATES.count, interiorPolygons: circlePolygon)

使用以下方法生成多边形

func setupRadiusOverlay(forGeotification geotification: Geotification) {
    let c = makeCircleCoordinates(geotification.coordinate, radius: RADIUS)
    self.option1polygon = MKPolygon(coordinates: c, count: c.count, interiorPolygons: nil)
    self.option2polygon = MKPolygon(coordinates: WORLD_COORDINATES, count: WORLD_COORDINATES.count, interiorPolygons: option1polygon)
}

使用以下方法添加多边形

func addRadiusOverlay(isOption2Selected: Bool) {
    guard let mapView = mapView else { return }

    let overlay = isOption2Selected ? self.option2polygon : self.option1polygon
    if mapView.overlays.index(where: { $0 === overlay }) == nil {
        mapView.removeOverlays(mapView.overlays.filter{ $0 is MKPolygon })
        mapView.addOverlay(overlay)
    }
}

更改委托方法mapView(_:rendererFor:)

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    guard overlay is MKPolygon else {
        return MKOverlayRenderer(overlay: overlay)
    }

    let color = UIColor(red: 0/255, green: 122/255, blue: 255/255, alpha: 1.0)
    let renderer = MKPolygonRenderer(overlay: overlay)
    renderer.lineWidth = 5.0
    renderer.strokeColor = color
    renderer.fillColor = color.withAlphaComponent(0.1)
    return renderer
}

以下将是世界坐标

let WORLD_COORDINATES = [
    CLLocationCoordinate2D(latitude: 90, longitude: 0),
    CLLocationCoordinate2D(latitude: 90, longitude: 180),
    CLLocationCoordinate2D(latitude:-90, longitude: 180),
    CLLocationCoordinate2D(latitude:-90, longitude: 0),
    CLLocationCoordinate2D(latitude:-90, longitude:-180),
    CLLocationCoordinate2D(latitude: 90, longitude:-180)
]

并根据我的old answer

的帮助方法进行操作
func makeCircleCoordinates(_ coordinate: CLLocationCoordinate2D, radius: Double, tolerance: Double = 3.0) -> [CLLocationCoordinate2D] {
    let latRadian = coordinate.latitude * .pi / 180
    let lngRadian = coordinate.longitude * .pi / 180
    let distance = (radius / 1000) / 6371 // kms
    return stride(from: 0.0, to: 360.0, by: tolerance).map {
        let bearing = $0 * .pi / 180

        let lat2 = asin(sin(latRadian) * cos(distance) + cos(latRadian) * sin(distance) * cos(bearing))
        var lon2 = lngRadian + atan2(sin(bearing) * sin(distance) * cos(latRadian),cos(distance) - sin(latRadian) * sin(lat2))
        lon2 = fmod(lon2 + 3 * .pi, 2 * .pi) - .pi  // normalise to -180..+180º
        return CLLocationCoordinate2D(latitude: lat2 * (180.0 / .pi), longitude: lon2 * (180.0 / .pi))
    }
}

option-2选择产生

enter image description here

option-1应该做反比:)