'init(coordinateRegion:i.... was deprecated in iOS 17.0

I'm following the official SwiftUI tutorial, using the latest Xcode (Version 15.0 beta 2, 15A5161b) so it's using iOS 17.0 beta. This is my code:

import SwiftUI
import MapKit
struct MapView: View {
    @State private var region = MKCoordinateRegion(
        center: CLLocationCoordinate2D(latitude: 34.011_284, longitude: -116.166_860),
        span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)
    )
    var body: some View {
        Map(coordinateRegion: $region)
    }
}
struct MapView_Previews: PreviewProvider {
    static var previews: some View {
        MapView()
    }
}

and this is the error I got for line 11 (Map(coordinateRegion: $region)):

'init(coordinateRegion:interactionModes:showsUserLocation:userTrackingMode:)' was deprecated in iOS 17.0: Use Map initializers that take a MapContentBuilder instead. I have no idea as to how to do this. Any help is appreciated.

Those tutorials were built for Xcode 13.1 (iOS 15) and so aren't updated to use new APIs introduced with later releases, including the new SwiftUI MapKit updates.

If you want to proceed with the new APIs, I'd recommend watching these session videos from WWDC23:

and having a look at the documentation to see what's changed.



Now to convert your "old" code to use the new MapKit APIs. As there is no direct conversion, this was the best I could come up with.

struct MapView: View {
    @State private var region = MKCoordinateRegion()

    let initialPosition: MapCameraPosition = {
        let center = CLLocationCoordinate2D(latitude: 34.011_284, longitude: -116.166_860)
        let span = MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)
        let region = MKCoordinateRegion(center: center, span: span)
        return .region(region)
    }()

    var body: some View {
        Map(initialPosition: initialPosition)
            .onMapCameraChange(frequency: .continuous) { context in
                region = context.region
            }
    }
}

Hi !

First I'm new in SwiftUI and Xcode so I don't know if I my code is very clean but I got the same problem and I find something.

import SwiftUI
import MapKit

struct MapView: View {
    
    var landmark: Landmark
    
    @State private var position: MapCameraPosition = .automatic
    
    var body: some View {
        Map(position: $position){
            Marker(landmark.name, coordinate: landmark.locationCoordinate)
        }
        .onAppear {
            let region = MKCoordinateRegion(
                center: CLLocationCoordinate2D(
                    latitude: landmark.locationCoordinate.latitude,
                    longitude: landmark.locationCoordinate.longitude),
                span: MKCoordinateSpan(
                    latitudeDelta: 1,
                    longitudeDelta: 1)
            )
            position = .region(region)
        }
    }
}

struct MapView_Previews: PreviewProvider {
    static var landmarks = ModelData().landmarks
    
    static var previews: some View {
        MapView(landmark: landmarks[0])
    }
}

By using this code in the MapView File, you can change de latitudeDelta and the longitudeDelta that make the zoom in(lower value) or Zoom out (higher value).

Have a nice day !

struct MapView: View {

var coordinate: CLLocationCoordinate2D

var body: some View {
    Map(initialPosition: initialPosition)
}


var initialPosition: MapCameraPosition {
       let span = MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)
       let region = MKCoordinateRegion(center: coordinate, span: span)
       return .region(region)
   }

}

struct MapView_Previews: PreviewProvider { static var previews: some View { MapView(coordinate: CLLocationCoordinate2D(latitude: 34.011_286, longitude: -116.166_868)) } }

'init(coordinateRegion:i.... was deprecated in iOS 17.0
 
 
Q