iosswiftmkmapviewmkcircle

Swift MKCircle Overlay


I am trying to draw a MKCircle around a map annotation. I think the code is right so far but not sure why it isn't working. I believe I have all the code needed for it to work.

func getPlaces(){
    let uid = Auth.auth().currentUser?.uid
    Database.database().reference().child("Businesses").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
        // print("\(snap.value)")

        if let locationDict = snapshot.value as? [String:AnyObject]{

            let lat = Double(locationDict["businessLatitude"] as! String)
            let long = Double(locationDict["businessLongitude"] as! String)
            let center = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
            let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

            let radius = 100.0

            self.mapView!.setRegion(region, animated: true)

            let circle = MKCircle(center: center, radius: radius)

            let annotation = MKPointAnnotation()
            annotation.coordinate = region.center
            self.mapView.addAnnotation(annotation)
            self.mapView.add(circle)
        }
    })
}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    let circleRenderer = MKCircleRenderer(overlay: overlay)
    circleRenderer.strokeColor = UIColor.red
    circleRenderer.lineWidth = 1.0
    return circleRenderer
}

Solution

  • Did you set the mapview delegate?

    self.mapView.delegate = self
    

    Don't forget MKMapViewDelegate protocol.

    class ViewController: UIViewController, MKMapViewDelegate  {
        ...
    }