I already know how to set up map pins or annotations like discussed here: https://developer.apple.com/forums/thread/651668
But the real question is:
How do I replace the hardcoded location data with passed parameters?
This Code is executed without problems, due to the fact that the coords in locations are hardcoded:
import MapKit
struct Location: Identifiable {
let id = UUID()
let name: String
let latitude: Double
let longitude: Double
var coordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
}
}
struct MapView: View {
let locations = [Location(name: "Turtle Rock", latitude: 34.011_286, longitude: -116.166_868)]
var coordinate: CLLocationCoordinate2D
var lat: Double
var long: Double
@State private var region = MKCoordinateRegion()
var body: some View {
Map(coordinateRegion: $region,showsUserLocation: true, annotationItems: locations){ loco in
MapPin(coordinate: loco.coordinate)
}
.onAppear{
setRegion(coordinate)
} .edgesIgnoringSafeArea(.all)
}
private func setRegion(_ coordinate: CLLocationCoordinate2D){
region = MKCoordinateRegion(
center: coordinate,
span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)
)
}
}
struct MapView_Previews: PreviewProvider {
static var previews: some View {
MapView(coordinate: CLLocationCoordinate2D(latitude: 34.011_286, longitude: -116.166_868),lat: 34.011_286, long: -116.166_868)
}
}
But as soon as I try to use lat(itude) or long(itude) or even coordinate from the passed parameters, I have to use lazy vars and therefore cannot access that mutable getters... any advice?
let locations = [Location(name: "Turtle Rock", latitude: lat, longitude: long)]
//this Throws: Cannot use instance member 'lat' within property initializer; property initializers run before 'self' is available.
//Using lazy var results in: Cannot use mutating getter on immutable value: 'self' is immutable