gpt4 book ai didi

ios - 点击时更改 map 叠加层的笔触颜色

转载 作者:行者123 更新时间:2023-11-29 11:47:03 27 4
gpt4 key购买 nike

我有一个 map View ,并且正在生成一堆 mkcircle 叠加层。它们具有描边宽度和填充颜色。

我还设置了一个点击手势,用于确定点击是否在其中一个 mkcircles 上。我现在想做的是更改描边宽度和填充颜色,以便用户知道点击了哪个 mkcircle,然后在任何其他点击时将其改回。

我的代码如下。

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let circleRenderer = MKCircleRenderer(overlay: overlay)
circleRenderer.fillColor = UIColor.blue.withAlphaComponent(0.1)
circleRenderer.strokeColor = UIColor.blue
circleRenderer.lineWidth = 2
return circleRenderer
}

func handleMapTap(_ gestureReconizer: UITapGestureRecognizer) {
let tapPoint = gestureReconizer.location(in: mapView)
let tapCoordinate = mapView.convert(tapPoint, toCoordinateFrom: mapView)
let point = MKMapPointForCoordinate(tapCoordinate)
if mapView.overlays.count > 0 {
for overlay: MKOverlay in polygonArray {
if (overlay is MKCircle) {
let circle = overlay
let circleRenderer = (mapView.renderer(for: circle) as! MKCircleRenderer)
let datapoint = circleRenderer.point(for: point)
circleRenderer.invalidatePath()

if circleRenderer.path.contains(datapoint) {

let circleIndex = polygonArray.index{$0 === circle}!
print(circleIndex)
}
}
}
}
}

我已经做了一些搜索,但我还没有找到解决方案。我能够获得点击圆的 circleIndex。

感谢任何指导。

最佳答案

以防万一其他人在这里遇到这个问题,这是最终实际上非常简单的答案。

func handleMapTap(_ gestureReconizer: UITapGestureRecognizer) {
let tapPoint = gestureReconizer.location(in: mapView)
let tapCoordinate = mapView.convert(tapPoint, toCoordinateFrom: mapView)
let point = MKMapPointForCoordinate(tapCoordinate)
if mapView.overlays.count > 0 {
for overlay: MKOverlay in polygonArray {
if (overlay is MKCircle) {
let circle = overlay
let circleRenderer = (mapView.renderer(for: circle) as! MKCircleRenderer)
let datapoint = circleRenderer.point(for: point)
circleRenderer.invalidatePath()

circleRenderer.fillColor = UIColor.blue.withAlphaComponent(0.1)
circleRenderer.strokeColor = UIColor.blue

if circleRenderer.path.contains(datapoint) {

circleRenderer.fillColor = UIColor.black
circleRenderer.strokeColor = UIColor.black
let circleIndex = polygonArray.index{$0 === circle}!
print(circleIndex)
}
}
}
}
}

关于ios - 点击时更改 map 叠加层的笔触颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43176482/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com