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

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()
}

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

wuminqi,

I commented above and here is my example I played with. my view model appears to have no issues, but can not use it in a view to generate a Map(position: argument ) such as Map(position: vm.mapRegion)

struct MapView: View {
    // load LocationViewModel Environment
    @Environment(LocationsViewModel.self) private var vm
    //@State private var cameraPos = vm.mapRegion
    // Cannot use instance member 'vm' within property initializer; property initializers run before 'self' is available
    
    //@State private var cameraPosition = MapCameraPosition.region(vm.mapRegion)
    // Cannot use instance member 'vm' within property initializer; property initializers run before 'self' is available
    
    @State private var cameraPosition = MapCameraPosition.region(MKCoordinateRegion(
        center: CLLocationCoordinate2D(latitude: 34.011286, longitude: -116.166868),
        span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)
    ))
    
    var body: some View {
        Text(vm.mapLocation.name)
            .padding(5)
        //Text("center: \(vm.mapRegion.center.latitude), span: \( span: vm.mapRegion.mapSpan.)")
        Text("MapLocation: \(vm.mapLocation.region)")
            .padding(5)
        
        Text("Region.center: \(vm.mapLocation.region.center)")
            .padding(5)
        
        Text("Region.span: \(vm.mapLocation.region.span)")
            .padding(5)
        
        Text("Region: \(vm.mapRegion)")
        
        // Instance method 'appendInterpolation' requires that 'CLLocationCoordinate2D' conform to '_FormatSpecifiable'
        Spacer()
        // Map(position: MapCameraPosition.region(MKCoordinateRegion(center:vm.mapLocation.region.center, span: vm.mapLocation.region.span
        // Cannot convert value of type 'MapCameraPosition' to expected argument type 'Binding<MapCameraPosition>')))
        // Map(position: MapCameraPosition.region(MKCoordinateRegion(center: vm.mapRegion.center, span: vm.mapSpan)))
        // Cannot convert value of type 'MapCameraPosition' to expected argument type 'Binding<MapCameraPosition>'
        
        //Map(position: MapCameraPosition.region(vm.mapRegion.))
        //Cannot convert value of type 'MapCameraPosition' to expected argument type 'Binding<MapCameraPosition>'
        
        Map(position: $cameraPosition)
    }
}
code-block
How to use Map initializers Xcode15
 
 
Q