swift4 - 倒圆图填写swift 4
问题描述
我正在尝试在地图上画一个圆圈,当用户选择选项 1 时,圆圈填充蓝色,当用户选择选项 2 时,整个地图将填充蓝色,而圆圈区域是无色的。这怎么可能?
`func addRadiusOverlay(forGeotification geotification: Geotification) {
mapView?.addOverlay(MKCircle(center: geotification.coordinate, radius: 300))
}`
`func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKCircle {
let circleRenderer = MKCircleRenderer(overlay: overlay)
circleRenderer.lineWidth = 5.0
circleRenderer.strokeColor = UIColor(red: 0/255, green: 122/255, blue: 255/255, alpha: 1.0)
circleRenderer.fillColor = circleRenderer.strokeColor!.withAlphaComponent(0.1)
return circleRenderer
}
return MKOverlayRenderer(overlay: overlay)
}`
解决方案
在 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)
]
并遵循辅助方法,由我的旧答案提供
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 选择产生
option-1 应该做相反的事情:)
推荐阅读
- reactjs - 无法对 Firestore 集合使用 onSnapshot 方法
- python-3.x - 优化深度优先搜索python
- sql - SQL - 按日期分组防止重复
- csv - 行:-1,列:-1 System.StringException:无效 id:'1294888839' Apex
- c++ - 如何在 C++ 中将 2D char 数组转换为 1D char 数组
- c# - 改装说它找不到身份验证方法但它大摇大摆地工作
- html - 我必须在 datalist 上单击 3 次才能选择项目
- reactjs - 将文件从 Axios 发布到 Django Rest Framework 时如何解决授权问题?
- java - 有没有办法可以限制 SpringBoot 方法标头中的请求参数数量?
- mysql - 为什么在用 `type=ref` 的扫描替换全表扫描后这个 MySQL 查询变慢了?