New guy here, hacking away on the SwiftUI tutorial (https://developer.apple.com/tutorials/swiftui/building-lists-and-navigation ):
In MapView.swift it would be useful to add a pin/marker. Thus adding:
struct Place: Identifiable {
let id = UUID()
let name: String
let coordinate: CLLocationCoordinate2D
}
var annotations = [
Place(name: "Xyz", coordinate: CLLocationCoordinate2D(latitude: 34.011286, longitude: -116.166868))
]
and modifying the View to:
var body: some View {
Map(coordinateRegion: $region, annotationItems: annotations) {
MapPin(coordinate: $0.coordinate)
}
.onAppear {
setRegion(coordinate)
}
}
puts a pin on the Turtle Rock map using the hard-coded latitude and longitude.
However, changing (latitude: 34.011286, longitude: -116.166868) to (latitude: coordinate.latitude, longitude: coordinate.longitude) results in error "Cannot use instance member 'coordinate' within property initializer; property initializers run before 'self' is available."
What modifications to the project are required to access the latitude and longitude for use in the annotation?
5
0
2.5k