This code to add a circle is working properly but I am unable to make the code work to remove the circle when I move the pin. I tried adding a remove function but that didn't work. Any suggestion where I am going wrong would be very much appreciated.
@IBAction func addAircraft(_ sender: UILongPressGestureRecognizer) {
let location = sender.location(in: self.mapView)
let locCoord = self.mapView.convert(location, toCoordinateFrom: self.mapView)
let annotation = MKPointAnnotation()
annotation.coordinate = locCoord
annotation.title = "Aircraft"
// addCircle(locCoord: locCoord)
self.mapView.removeAnnotations(mapView.annotations)
self.mapView.addAnnotation(annotation)
// self.mapView.remove(circle)
}
// MARK: Circle and Polygon overlays.
func addCircle(locCoord: CLLocationCoordinate2D) {
self.mapView.delegate = self
let circle = MKCircle(center: locCoord, radius: 1000 as CLLocationDistance)
self.mapView.add(circle)
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKCircle {
let circle = MKCircleRenderer(overlay: overlay)
circle.strokeColor = UIColor.red
circle.lineWidth = 1
return circle
}else {
return MKOverlayRenderer()
}
}