2017-04-03 4 views
0

私はマップビューを持っており、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を取得できました。

ご了承ください。

+0

handleMapTapでは、circleRendererのstrokeColorとlineWidthを変更できませんか? –

+0

はい。私はちょうどそれを見つけて、答えの質問についてでした。 – puks1978

答えて

0

他の誰かがここに来るのは、実際にはかなり簡単だった答えです。

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) 
       } 
      } 
     } 
    } 
} 
関連する問題