Adding MKCircle to the annotations on the map - Problem with setting CLLocationCoordinate2D

357 views Asked by At

Im trying to add MKCircle to the map annotation. Later it is going to be used to detect user in this area and check in (if you have any tips how to start with this later, I'd be grateful). For now here is my code, but i get an error:

Cannot convert value of type 'CLLocationCoordinate2D.Type' to expected argument type 'CLLocationCoordinate2D'

Here is my code, do I need to add sth else?:

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate{

    let locationManager = CLLocationManager()
    
    struct Szczyt {
      var name: String
      var describtion: String
      var lattitude: CLLocationDegrees
      var longtitude: CLLocationDegrees
    }
    
    @IBOutlet weak var mapView: MKMapView!
    @IBAction func mapTypeSegmentSelected(_ sender: UISegmentedControl) {
            switch sender.selectedSegmentIndex {
            case 0:
                mapView.mapType = .standard
            case 1:
                mapView.mapType = .satellite
            default:
                mapView.mapType = .hybrid
            }
        }
  **let circle = MKCircle(center: CLLocationCoordinate2D, radius: 100)** //Cannot convert value of type 'CLLocationCoordinate2D.Type' to expected argument type 'CLLocationCoordinate2D'

    let szczyty = [Szczyt(name: "one", describtion: "describtion one", lattitude: 50.333061725039226, longtitude: 16.708595782487315),
                   Szczyt(name: "Two", describtion: "Describtion two", lattitude: 50.444874478583854, longtitude: 20.896341184611302),
                   Szczyt(name: "Three", describtion: "Describiton three", lattitude: 50.555134079897516, longtitude: 15.884675411850157)]
    
 
    
    override func viewDidLoad() {
        super.viewDidLoad()
        checkLocationServices()
        findSthOnTheMap(szczyty)
        mapView.delegate = self
        
        }
        func checkLocationServices() {
          if CLLocationManager.locationServicesEnabled() {
            checkLocationAuthorization()
          } else {
          }
        }
        func checkLocationAuthorization() {
          switch CLLocationManager.authorizationStatus() {
          case .authorizedWhenInUse:
            mapView.showsUserLocation = true
           case .denied: 
           break
          case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
            mapView.showsUserLocation = true
          case .restricted: 
           break
          case .authorizedAlways:
           break

          }
    }
    func findSthOnTheMap(_ szczyty: [Szczyt]) {
      for szczyt in szczyty {
        let annotations = MKPointAnnotation()
        annotations.title = szczyt.name
        annotations.subtitle = szczyt.opis
        annotations.coordinate = CLLocationCoordinate2D(latitude:
          szczyt.lattitude, longitude: szczyt.longtitude)
        mapView.addAnnotation(annotations)
        mapView.addOverlay(circle)
      }
    }
    
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard !(annotation is MKUserLocation) else { return nil }
        let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "MyMarker")
        switch annotation.title!! {
            case "one":
                annotationView.markerTintColor = UIColor(red: 0.86, green: 0.99, blue: 0.79, alpha: 1.00)
                annotationView.glyphImage = UIImage(named: "example")
            case "two":
                annotationView.markerTintColor = UIColor(red: 0.80, green: 0.98, blue: 0.73, alpha: 1.00)
                annotationView.glyphImage = UIImage(named: "example")
            case "three":
                annotationView.markerTintColor = UIColor(red: 0.73, green: 0.98, blue: 0.68, alpha: 1.00)
                annotationView.glyphImage = UIImage(named: "example")
            default:
                annotationView.markerTintColor = UIColor.green
                annotationView.glyphImage = UIImage(named: "example")
        }
        return annotationView
    }
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let circleRenderer = MKCircleRenderer(overlay: overlay)
        circleRenderer.strokeColor = UIColor.red
        circleRenderer.lineWidth = 1.0
        return circleRenderer
    }

}
1

There are 1 answers

8
Shadowrun On BEST ANSWER

This expression:

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

Is like saying 3 + Int

You need to provide a value of type CLLocationCoordinate2D

You provided the type itself as an argument to MKCircle's center parameter

Make an instance like CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0) to provide an actual coordinate where you want the circle to be centered

Update: if you want to add circular annotations for all of these model objects, then perhaps something like this is what you want:

Note some typos:

struct Szczyt {
    let name: String
    let describtion: String   // description
    let lattitude: CLLocationDegrees // latitude
    let longtitude: CLLocationDegrees // longitude
}

Add a computed property in an extension on our model that lets us easily get its CLLocationCoordinate2D

extension Szczyt {
    var coordinate: CLLocationCoordinate2D {
        .init(latitude: lattitude, longitude: longtitude)
    }
}

let szczyty = [Szczyt(name: "one", describtion: "describtion one", lattitude: 50.333061725039226, longtitude: 16.708595782487315),
               Szczyt(name: "Two", describtion: "Describtion two", lattitude: 50.444874478583854, longtitude: 20.896341184611302),
               Szczyt(name: "Three", describtion: "Describiton three", lattitude: 50.555134079897516, longtitude: 15.884675411850157)]

Make an array of MKCircle objects from all of the model objects

let circles = szczyty.map {
    MKCircle(center: $0.coordinate, radius: 100)
}