I am currently encountering two deprecated errors in my code. Could someone please identify the issues with the code?
Errors:
'init(coordinateRegion:interactionModes:showsUserLocation:userTrackingMode:annotationItems:annotationContent:)' was deprecated in iOS 17.0: Use Map initializers that take a MapContentBuilder instead.
'MapAnnotation' was deprecated in iOS 17.0: Use Annotation along with Map initializers that take a MapContentBuilder instead.
Code:
// MARK: - Stores Map (Dynamic)
struct StoresMapView: View { @State private var storeLocations: [StoreLocation] = [] @State private var region = MKCoordinateRegion( center: CLLocationCoordinate2D(latitude: -31.95, longitude: 115.86), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5) )
var body: some View {
Map(coordinateRegion: $region, interactionModes: .all, annotationItems: storeLocations) { store in
MapAnnotation(coordinate: CLLocationCoordinate2D(latitude: store.latitude, longitude: store.longitude)) {
VStack(spacing: 4) {
Image(systemName: "leaf.circle.fill")
.font(.title)
.foregroundColor(.green)
Text(store.name)
.font(.caption)
.fixedSize()
}
}
}
.onAppear(perform: loadStoreData)
.navigationTitle("Store Locator")
}
private func loadStoreData() {
guard let url = URL(string: "https://example.com/cop092/StoreLocations.json") else { return }
URLSession.shared.dataTask(with: url) { data, _, _ in
if let data = data, let decoded = try? JSONDecoder().decode([StoreLocation].self, from: data) {
DispatchQueue.main.async {
self.storeLocations = decoded
if let first = decoded.first {
self.region.center = CLLocationCoordinate2D(latitude: first.latitude, longitude: first.longitude)
}
}
}
}.resume()
}
}
MapKit's expansion of its SwiftUI APIs means that the deprecated APIs in your code are no longer the best way to do things. Everything you need to know about how to use the replacements for these deprecated APIs is in Meet MapKit for SwiftUI.
— Ed Ford, DTS Engineer