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

Hello All,

This is my first question here, so please be lenient ;) I'm trying to add add circular (MKCircle) annotations for all of these model objects in my code instead of adding every of them with coordinations. Here is my code:




    let locationManager = CLLocationManager()

    

    struct Szczyt {

      let name: String

      let opis: String

      let lattitude: CLLocationDegrees

      let longtitude: CLLocationDegrees

        var coordinate: CLLocationCoordinate2D {

            .init(latitude: lattitude, longitude: longtitude)
// here Im trying to create var for this property to set it for all objects
        }

    }

@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 circles = szczyty.map {

        MKCircle(center: $0.coordinate, radius: 100)
//here I got - Cannot use instance member 'szczyty' within property initializer; property initializers run before 'self' is available
    }

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)]
    

Can someone can give me a tip or advice how to solve this? Once again - Im trying to set MKCircle for all of the objects in szczyty. Thank you in advance for your help.

In the next steps I would like to detect if user is in those circles by setting 'distance(from:)' method to determinate user location between circle and user location. Than user should have an option to check in in specified area. Also if you would have any tips and advices what to use to do it, I'd be very thankful.

Paweł

Answered by OOPer in 676312022

I guess all the properties and methods you have shown belongs to a class -- some UIViewController. You should better include the header of the class.

And the definition of szczyty is not consistent with the struct definition Szczyt. I assume the struct definition is right and the initial value of szczyty needs to be modified.


As shown in the error message, you cannot use an expression containing instance properties or methods as an initial value of an instance property, in Swift.

One possible way is to set the value of circles in viewDidLoad():

    private(set) var circles: [MKCircle]!
        
    override func viewDidLoad() {
        super.viewDidLoad()
        
        circles = szczyty.map {
            MKCircle(center: $0.coordinate, radius: 100)
        }
        //...
    }
Accepted Answer

I guess all the properties and methods you have shown belongs to a class -- some UIViewController. You should better include the header of the class.

And the definition of szczyty is not consistent with the struct definition Szczyt. I assume the struct definition is right and the initial value of szczyty needs to be modified.


As shown in the error message, you cannot use an expression containing instance properties or methods as an initial value of an instance property, in Swift.

One possible way is to set the value of circles in viewDidLoad():

    private(set) var circles: [MKCircle]!
        
    override func viewDidLoad() {
        super.viewDidLoad()
        
        circles = szczyty.map {
            MKCircle(center: $0.coordinate, radius: 100)
        }
        //...
    }

Ok, so I've got

private(set) var circles: [MKCircle]!
override func viewDidLoad() {

        super.viewDidLoad()

        checkLocationServices()

        findSzczyty(szczyty)

        mapView.delegate = self

        circles = szczyty.map {

                    MKCircle(center: $0.coordinate, radius: 200)

                }

        }
...
func findSzczyty(_ szczyty: [Szczyt]) {

      for szczyt in szczyty {

        let annotations = MKPointAnnotation()

        annotations.title = szczyt.name

        annotations.subtitle = szczyt.describtion

        annotations.coordinate = CLLocationCoordinate2D(latitude:

          szczyt.lattitude, longitude: szczyt.longtitude)

        mapView.addAnnotation(annotations)

        mapView.addOverlay(circles as! MKOverlay) // when I try to add it on the map I get an error when building - Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

      }

    }

@OOPer maybe you have any tips and advices what to use in my next steps? I dont mean to write me a code, but some good advices. Thanks.

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