How to create MKCircle in Swift?

Iv searched all around to get a good explanation of how to make MKCircle annotation for MapView using Swift 2.0, but I cannot find an adequate explanation. Can someone post sample code showing how to create MKCircle annotation? Here is the code that I use to draw a map and get the coordinate.

let address = self.location

let geocoder = CLGeocoder()

    geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
        if((error) != nil){
            print("Error", error)
        }
        if let placemark = placemarks?.first {
            let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate

            self.locationCoordinates = coordinates
            let span = MKCoordinateSpanMake(0.005, 0.005)
            let region = MKCoordinateRegion(center: self.locationCoordinates, span: span)
            self.CIMap.setRegion(region, animated: true)

            let annotation = MKPointAnnotation()
            annotation.coordinate = self.locationCoordinates
            self.CIMap.addAnnotation(annotation)

            self.CIMap.layer.cornerRadius = 10.0

            self.CIMap.addOverlay(MKCircle(centerCoordinate: self.locationCoordinates, radius: 1000))
        }
    })
+4
source share
3 answers

It will show a step-by-step approach on how to create a circular overlay on a map using swift 3 with xcode 8.3.3

, () , mapView. , , Long Press mapView libary, , addRegion() .

  • CLLocationManager, . viewDidLoad .

        import UIKit  
        import MapKit
    
        class ViewController: UIViewController {
    
    
            @IBOutlet var mapView: MKMapView!
            let locationManager = CLLocationManager()
    
        override func viewDidLoad() {  
                super.viewDidLoad()
    
                locationManager.delegate = self
                locationManager.requestAlwaysAuthorization()
                locationManager.requestWhenInUseAuthorization()
                locationManager.desiredAccuracy = kCLLocationAccuracyBest
                locationManager.startUpdatingLocation()
    
            }
    
  • , adRegion().

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

, , . mapviewdelegate.

        func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {}
  1. , , . CLLocationManagerDelegate MKMapViewDelegate.

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

locationManager.stopUpdatingLocation() , .

        extension ViewController: MKMapViewDelegate {
            func mapView(_ mapView: 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
            }
        }

.

.

import UIKit
import MapKit

class ViewController: UIViewController {


    @IBOutlet var mapView: MKMapView!
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        locationManager.requestWhenInUseAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()

    }

    // MARK: Long Press Gesture Recognizer Action Method

    @IBAction func addRegion(_ sender: Any) {
        print("addregion pressed")
        guard let longPress = sender as? UILongPressGestureRecognizer else {return}

        let touchLocation = longPress.location(in: mapView)
        let coordinates = mapView.convert(touchLocation, toCoordinateFrom: mapView)
        let region = CLCircularRegion(center: coordinates, radius: 5000, identifier: "geofence")
        mapView.removeOverlays(mapView.overlays)
        locationManager.startMonitoring(for: region)
        let circle = MKCircle(center: coordinates, radius: region.radius)
        mapView.add(circle)

    }

}

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

extension ViewController: MKMapViewDelegate {
    func mapView(_ mapView: 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
    }
}
+2

MKMapViewDelegate defenition.

 mapView.delegate = self 

viewDidLoad.

mapView.addOverlay(MKCircle(centerCoordinate: CLLocationCoordinate2D, radius: CLLocationDistance))

mapView rendererForOverlay mapViews

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
    if let overlay = overlay as? MKCircle {
        let circleRenderer = MKCircleRenderer(circle: overlay)
        circleRenderer.fillColor = UIColor.blueColor()
        return circleRenderer
    }
}

, MapKit

+7

An overlay is just a collection of numbers. The thing that is visible on the map is an overlay visualizer. You must implement mapView:rendererForOverlay:to ensure overlay rendering; otherwise you will not see anything.

+1
source

Source: https://habr.com/ru/post/1612853/


All Articles