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ł
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)
}
//...
}