How do I fix this Error implementing map in SwiftUI

I am trying to create my first app, it is an app that allows me to follow the progress of my enduro (dirt bike) sessions, both in race and training mode, it happens that one of the basic features that my app must have is a tracking system on a map so that it saves my route. I am trying to implement the map in my app but I find several errors, I want to apologize if the format or anything i’m writing here is incorrect, this is my first time asking here.
I’m using Xcode 15.4 and iOS 17.5,
Thank you so much for your time.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import SwiftUI
import MapKit
struct IdentifiableCoordinate: Identifiable {
let id = UUID()
let coordinate: CLLocationCoordinate2D
}
struct SecondView: View {
@StateObject private var locationManager = LocationManager()
var body: some View {
ZStack {
Map(coordinateRegion: $locationManager.region, showsUserLocation: true, annotationItems: locationManager.trackingLocations) { location in ( here is the error message: Initializer 'init(coordinateRegion:interactionModes:showsUserLocation:userTrackingMode:annotationItems:annotationContent:)' requires that 'Annotation<Text, some View>' conform to 'MapAnnotationProtocol'
Annotation(location.coordinate, coordinate: <#CLLocationCoordinate2D#>) { ( here is the second error message: Initializer 'init(_:coordinate:anchor:content:)' requires that 'CLLocationCoordinate2D' conform to 'StringProtocol'
Circle()
.strokeBorder(Color.red, lineWidth: 2)
.frame(width: 30, height: 30)
}
}
.ignoresSafeArea()
.onAppear {
locationManager.checkIfLocationServicesIsEnabled()
}
VStack {
Text("ENDURapp")
.font(.largeTitle)
.foregroundColor(.white)
.padding(.top, 50)
Spacer()
}
}
}
}
class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
private let locationManager = CLLocationManager()
@Published var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194), // Coordenada inicial
span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
)
@Published var trackingLocations: [IdentifiableCoordinate] = []
override init() {
super.init()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
func checkIfLocationServicesIsEnabled() {
if CLLocationManager.locationServicesEnabled() {
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
} else {
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let latestLocation = locations.last else { return }
region = MKCoordinateRegion(
center: latestLocation.coordinate,
span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
)
trackingLocations.append(IdentifiableCoordinate(coordinate: latestLocation.coordinate))
}
}
struct SecondView_Previews: PreviewProvider {
static var previews: some View {
SecondView()
}
}
</code>
<code>import SwiftUI import MapKit struct IdentifiableCoordinate: Identifiable { let id = UUID() let coordinate: CLLocationCoordinate2D } struct SecondView: View { @StateObject private var locationManager = LocationManager() var body: some View { ZStack { Map(coordinateRegion: $locationManager.region, showsUserLocation: true, annotationItems: locationManager.trackingLocations) { location in ( here is the error message: Initializer 'init(coordinateRegion:interactionModes:showsUserLocation:userTrackingMode:annotationItems:annotationContent:)' requires that 'Annotation<Text, some View>' conform to 'MapAnnotationProtocol' Annotation(location.coordinate, coordinate: <#CLLocationCoordinate2D#>) { ( here is the second error message: Initializer 'init(_:coordinate:anchor:content:)' requires that 'CLLocationCoordinate2D' conform to 'StringProtocol' Circle() .strokeBorder(Color.red, lineWidth: 2) .frame(width: 30, height: 30) } } .ignoresSafeArea() .onAppear { locationManager.checkIfLocationServicesIsEnabled() } VStack { Text("ENDURapp") .font(.largeTitle) .foregroundColor(.white) .padding(.top, 50) Spacer() } } } } class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate { private let locationManager = CLLocationManager() @Published var region = MKCoordinateRegion( center: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194), // Coordenada inicial span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01) ) @Published var trackingLocations: [IdentifiableCoordinate] = [] override init() { super.init() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest } func checkIfLocationServicesIsEnabled() { if CLLocationManager.locationServicesEnabled() { locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() } else { } } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard let latestLocation = locations.last else { return } region = MKCoordinateRegion( center: latestLocation.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01) ) trackingLocations.append(IdentifiableCoordinate(coordinate: latestLocation.coordinate)) } } struct SecondView_Previews: PreviewProvider { static var previews: some View { SecondView() } } </code>
import SwiftUI
import MapKit

struct IdentifiableCoordinate: Identifiable {
    let id = UUID()
    let coordinate: CLLocationCoordinate2D
}

struct SecondView: View {
    @StateObject private var locationManager = LocationManager()

    var body: some View {
        ZStack {
           
            Map(coordinateRegion: $locationManager.region, showsUserLocation: true, annotationItems: locationManager.trackingLocations) { location in ( here is the error message: Initializer 'init(coordinateRegion:interactionModes:showsUserLocation:userTrackingMode:annotationItems:annotationContent:)' requires that 'Annotation<Text, some View>' conform to 'MapAnnotationProtocol'
                
                Annotation(location.coordinate, coordinate: <#CLLocationCoordinate2D#>) { ( here is the second error message: Initializer 'init(_:coordinate:anchor:content:)' requires that 'CLLocationCoordinate2D' conform to 'StringProtocol'
                    Circle()
                        .strokeBorder(Color.red, lineWidth: 2)
                        .frame(width: 30, height: 30)
                }
            }
            .ignoresSafeArea()
            .onAppear {
                locationManager.checkIfLocationServicesIsEnabled()
            }
            
            VStack {
                Text("ENDURapp")
                    .font(.largeTitle)
                    .foregroundColor(.white)
                    .padding(.top, 50)
                
                Spacer()
            }
        }
    }
}

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
    private let locationManager = CLLocationManager()
    
    @Published var region = MKCoordinateRegion(
        center: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194), // Coordenada inicial
        span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
    )
    
    @Published var trackingLocations: [IdentifiableCoordinate] = []
    
    override init() {
        super.init()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
    }
    
    func checkIfLocationServicesIsEnabled() {
        if CLLocationManager.locationServicesEnabled() {
            locationManager.requestWhenInUseAuthorization()
            locationManager.startUpdatingLocation()
        } else {
            
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let latestLocation = locations.last else { return }
        region = MKCoordinateRegion(
            center: latestLocation.coordinate,
            span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
        )
        trackingLocations.append(IdentifiableCoordinate(coordinate: latestLocation.coordinate))
    }
}

struct SecondView_Previews: PreviewProvider {
    static var previews: some View {
        SecondView()
    }
}

I change as someone suggest the following: don’t use a ForEach use annotationItems with Map but that didn’t work either.

The errors you are seeing are related to using incompatible APIs. Try replacing Annotation with MapAnnotation and the code will compile. If you have a target lower than iOS 17, you have to use the older deprecated types.

For an example on how to use the iOS 17.0+ Annotation type, the tutorial Integrating MapKit with SwiftUI provides a pretty good introduction.

There are several other issues in the code you shared. A non-exhaustive list:

  • The LocationManager should be held as a @State private var locationManager = LocationManager() rather than a state object
  • Instead of using CLLocationManager.locationServicesEnabled(), check the status such as
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if locationManager.authorizationStatus == .notDetermined {
locationManager.requestWhenInUseAuthorization()
}
</code>
<code>if locationManager.authorizationStatus == .notDetermined { locationManager.requestWhenInUseAuthorization() } </code>
if locationManager.authorizationStatus == .notDetermined {
    locationManager.requestWhenInUseAuthorization()
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật