I am trying to add an overlay circle to my map using MapKit in Swift Xcode. But, the overlay is not showing up

Viewed 62

I have found a bunch of people doing the same thing as I am and theirs works. When this runs, it will print "addregion pressed" but it is not actually drawing the circle

import UIKit
import MapKit

class ViewController: UIViewController {


    @IBOutlet var map: MKMapView!
    let locationManager = CLLocationManager()
    
    override func viewDidLoad() {
            super.viewDidLoad()

            locationManager.delegate = self
            map.delegate = self
            locationManager.requestAlwaysAuthorization()
            locationManager.requestWhenInUseAuthorization()
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.startUpdatingLocation()
    }
    
    @IBAction func addRegion(_ sender: Any) {
        print("addregion pressed")
        guard let longPress = sender as? UILongPressGestureRecognizer else {return}
        let touchLocation = longPress.location(in: map)
        let coordinates = map.convert(touchLocation, toCoordinateFrom: map)
        let region = CLCircularRegion(center: coordinates, radius: 5000, identifier: "geofence")
        map.removeOverlays(map.overlays)
        locationManager.startMonitoring(for: region)
        let circle = MKCircle(center: coordinates, radius: region.radius)
        map.addOverlay(circle)
    }
}


    extension ViewController: CLLocationManagerDelegate {
        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            locationManager.stopUpdatingLocation()
            map.showsUserLocation = true
        }
    }

    extension ViewController: MKMapViewDelegate {
        func map(_ map: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
            guard let circelOverLay = overlay as? MKCircle else {return MKOverlayRenderer()}

            let circleRenderer = MKCircleRenderer(circle: circelOverLay)
            circleRenderer.strokeColor = .blue
            circleRenderer.fillColor = .blue
            circleRenderer.alpha = 0.2
            return circleRenderer
        }
    }
1 Answers

Figured it out. Just needed map.delegate = self

Related