Map(coordinateRegion: , showsUserLocation: ) deprecated in iOS 17.0 – Swiftui

by
Ali Hasan
ios17 mapkit swiftui-animation

Quick Fix: Rather than using the deprecated Map(coordinateRegion: , showsUserLocation: ), use the Map(position:.., bounds: .., interactionModes:.., scope:.., content:) initializer with a MapCameraPosition binding and a UserAnnotation to display the user’s location.

The Problem:

The Map(coordinateRegion: , showsUserLocation: ) initializer is deprecated in iOS 17.0. How can I replace it with a new initializer that takes a MapContentBuilder?

The Solutions:

Solution 1: Use `init(position:bounds:interactionModes:scope:content:)` Initializer

Use the init(position:bounds:interactionModes:scope:content:) initializer instead. This takes a binding of MapCameraPosition, so you should change the type in the view model accordingly.

Map(position: $viewModel.region) {
    UserAnnotation()
}

To show the user’s position, use a UserAnnotation.

Solution 2: Use map initializer that take a MapContentBuilder instead.

Here’s how you can replace the deprecated call to Map(coordinateRegion: showsUserLocation:) with a map initializer that takes a MapContentBuilder instead:

First, define a @Published variable to hold the map’s camera position. You can use the MapCameraPosition.region() initializer to create a camera position from a MKCoordinateRegion.

@Published var position = MapCameraPosition.region(MKCoordinateRegion(center: MapDetails.startingLocation, span: MapDetails.defaultSpan))

Then, use the Map(position:) initializer to create a map that uses the specified camera position.

Map(position: $viewModel.position)

This will create a map that shows the specified region and allows the user to pan and zoom the map.

Q&A

How can one replace Map(coordinateRegion: $viewModel.region, showsUserLocation: true)?

Use the init(position:bounds:interactionModes:scope:content:) initialiser instead.

What can be added to show user’s position?

Use a UserAnnotation to show the user’s position.

What other ways are available?

You can use a map initializer that takes a MapContentBuilder instead.

Video Explanation:

The following video, titled "init(coordinateRegion:interactionModes:showsUserLocation ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... deprecated in iOS 17.0: Use Map initializers that take a MapContentBuilder instead. SwiftUI Camp https://rebeloper.com/swiftui-camp In ...