'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.

1 Like

This is not a Swift change, but rather an iOS SDK change. The 2023 MapKit WWDC session and its links may be helpful. You might also post your question on the Apple developer forums.

They changed the Map to the "Camera" paradigm.

var cameraPosition: MapCameraPosition {
MapCameraPosition.region(some MKCoordinateRegion)
}

Map(position: .constant(cameraPosition), bounds: nil, interactionModes: .all, scope: nil)

It's a wholesale change to MapKit, but it looks to be way more powerful and needed for visionOS.

Good luck on your project!

Thanks a lot this method works with me, also there is a similar method

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) 

    }
}
4 Likes