Hallo Zusammen,
ich hab wieder mal eine Frage zu meinem Projekt. Nachdem ihr mir das letzte mal gut geholfen habt, hoffe ich das ihr mir hier auch gut helfen könnt.
Ich möchte GeoCaches auf einer Karte anzeigen lassen, dazu habe ich bisher meine Caches über eine API heruntergeladen und in mein CoreData framework abgespeichert. Jedoch liegen die da nicht als MKAnnotation Object vor. so das ich das nachträglich noch zusammenbauen muss.
Hier stelle ich mir nun zwei fragen:
1. Ist es möglich das Annotation Objekt vom Typ MKAnnotation in der Datenbank abzulegen, so das ich es später nur noch abrufen muss?
2. Wie hole ich mir die Annotations, aus der Datenbank, bevor ich mir meine Karte anzeigen lasse, ansonsten ist mein annotationsArray leer?
Hat jemand eine Idee?
Alles anzeigen
ich hab wieder mal eine Frage zu meinem Projekt. Nachdem ihr mir das letzte mal gut geholfen habt, hoffe ich das ihr mir hier auch gut helfen könnt.
Ich möchte GeoCaches auf einer Karte anzeigen lassen, dazu habe ich bisher meine Caches über eine API heruntergeladen und in mein CoreData framework abgespeichert. Jedoch liegen die da nicht als MKAnnotation Object vor. so das ich das nachträglich noch zusammenbauen muss.
Hier stelle ich mir nun zwei fragen:
1. Ist es möglich das Annotation Objekt vom Typ MKAnnotation in der Datenbank abzulegen, so das ich es später nur noch abrufen muss?
2. Wie hole ich mir die Annotations, aus der Datenbank, bevor ich mir meine Karte anzeigen lasse, ansonsten ist mein annotationsArray leer?
Hat jemand eine Idee?
Quellcode
- import SwiftUI
- import MapKit
- import Combine
- struct MapView: View {
- @Environment(\.managedObjectContext) var viewContext
- @FetchRequest(sortDescriptors: []) var caches: FetchedResults<OpenCache>
- @ObservedObject private var locationManager = LocationManager()
- @State private var region = MKCoordinateRegion.defaultRegion
- @State private var cancellable: AnyCancellable?
- @State private var trackingMode: MKUserTrackingMode = .none
- @State private var mapType: MKMapType = .standard
- @State var hasLoadedData = false
- @State private var annotationsArray: [Annotation] = []
- // Extrakte Methode zum erzeugen der einzelnen CoreData Einträge
- fileprivate func createDataEntries(_ response: (JSONCacheModel)) {
- let lat = response.location?.split(separator: "|")
- let vC = PersistenceController.shared.container.viewContext
- let newCacheModel = OpenCache(context: vC)
- newCacheModel.id = UUID()
- newCacheModel.code = response.code
- newCacheModel.name = response.name
- newCacheModel.status = response.status
- newCacheModel.type = response.type
- newCacheModel.latitude = Double(lat![0])!
- newCacheModel.longitude = Double(lat![1])!
- try? viewContext.save()
- }
- var body: some View {
- VStack {
- // Anzeigen der Karte, sobald der Standort gefunden worden ist
- if locationManager.location != nil {
- MapModel(
- region: $region,
- mapType: mapType,
- userTrackingMode: $trackingMode,
- showsUserLocation: true,
- annotation: annotationsArray
- ).edgesIgnoringSafeArea([.top]).onAppear{
- if !hasLoadedData {
- // Abrufen der Caches
- NetworkManager.shared.getAllCaches { (results) in
- switch results {
- case .success(let response):
- response.results.forEach { cache in
- NetworkManager.shared.getOneCache(cacheCode: cache) { (result) in
- switch result {
- case .success(let response):
- // CoreData einträge erzeugen
- createDataEntries(response)
- break
- case .failure(let error):
- print(error.rawValue)
- break
- }
- }
- }
- break
- case .failure(let error):
- print(error)
- }
- }
- }
- }
- } else {
- Text("Location user location ...")
- }
- Picker("", selection: $mapType) {
- Text("Standard").tag(MKMapType.standard)
- Text("Satellite").tag(MKMapType.satellite)
- Text("Hybrid").tag(MKMapType.hybrid)
- }
- .pickerStyle(SegmentedPickerStyle())
- .opacity(0.5)
- Spacer()
- }
- .onAppear{
- setCurrentLocation()
- }
- }
- private func setCurrentLocation() {
- cancellable = locationManager.$location.sink { location in
- region = MKCoordinateRegion(center: location?.coordinate ?? CLLocationCoordinate2D(), latitudinalMeters: 100,longitudinalMeters: 100)
- }
- }
- }