How to use Map initializers Xcode15

Hello, I'm trying to learn swift and making the Landmarks tutorial I found a problem with the map view. The error read as follow: "'init(coordinateRegion:interactionModes:showsUserLocation:userTrackingMode:)' was deprecated in iOS 17.0: Use Map initializers that take a MapContentBuilder instead."

My code is:

import MapKit


struct MapView: View {
    @State private var region = MKCoordinateRegion(
        center: CLLocationCoordinate2D(latitude: 34.011_286, longitude: -116.166_868),
        span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)
    )


// The error happens here!
    var body: some View {
        Map(coordinateRegion: $region) 

    }
}

#Preview {
    MapView()
}

Any suggestions about hot to solve this will be appreciate it.

Thanks,

BR

Skalex

  • did you find a solution to this? I'm having the same problem.

Add a Comment

Replies

You can try init(position: Binding<MapCameraPosition>) initializer of Map struct, API Doc

import MapKit


struct MapView: View {
    @State private var cameraPosition = MapCameraPosition.region(MKCoordinateRegion(
        center: CLLocationCoordinate2D(latitude: 34.011_286, longitude: -116.166_868),
        span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)
    ))


// The error happens here!
    var body: some View {
        Map(position: $cameraPosition) 

    }
}

#Preview {
    MapView()
}