Reputation: 3697
I have a map view and I am generating a bunch of mkcircle overlays. These have a stroke width and fill color.
I also have a tap gesture set up that determines if the tap is on one of the mkcircles. What I would like to do now is change the stroke width and fill color so the user knows which mkcircle was tapped and then change it back on any other tap.
The code I have is below.
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)
}
}
}
}
}
I have done some searching but I have not been able to find a solution yet. I am able to get the circleIndex of the tapped circle.
Any guidance is appreciated.
Upvotes: 2
Views: 1238
Reputation: 3697
Just in case anyone else comes across this here is the answer which was actually pretty easy in the end.
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)
}
}
}
}
}
Upvotes: 2