I have a fairy simple SwiftUI code block showing a map with a pin. This works so far.
I'd like to add a new pin by a list of draggable objects. Now, I am bit stuck at the following problems:
- Where to add a list of draggable items to create the pin?
- How to select the item and translate it to a marker object?
- How to determine the final location in MapKit where the pin is dragged?
I think it is solvable and I can do it in LeafletJS, but SwiftUI makes my head ache.
import MapKit
import CoreLocation
struct Marker: Identifiable {
let id = UUID()
var location: MapMarker
}
struct ContentView: View {
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 47.8681, longitude: 8.205), span: MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.50))
let markers = [Marker(location: MapMarker(coordinate: CLLocationCoordinate2D(latitude: 47.8681, longitude: 8.205), tint: .blue))]
var body: some View {
Map(coordinateRegion: $region, showsUserLocation: true, userTrackingMode: .constant(.follow),annotationItems: markers){ marker in
marker.location
}
.frame(width: 1200, height: 600)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}