更改水龙头地图叠加的笔划颜色

问题描述:

我有一个地图视图,并且生成一堆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) 
       } 
      } 
     } 
    } 
} 

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

任何指导表示赞赏。

+0

在handleMapTap中,是否无法更改circleRenderer的strokeColor和lineWidth? –

+0

是的。我刚刚发现并解决了这个问题的答案。 – puks1978

只是为了防止其他人遇到这个问题,这个答案其实很简单。

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) 
       } 
      } 
     } 
    } 
}