0

Any ideas of how to replace this call? It was blindingly useful and has now been deprecated in iOS 17.0.

Map(coordinateRegion: $viewModel.region, showsUserLocation: true)

This was being fed via:

enum MapDetails {
     static let startingLocation = CLLocationCoordinate2D(latitude: GlobalVariables.latitude, longitude: GlobalVariables.longitude)
     static let defaultSpan      = MKCoordinateSpan(latitudeDelta: GlobalVariables.span, longitudeDelta: GlobalVariables.span)
}

final class LocationViewModel: NSObject, ObservableObject, CLLocationManagerDelegate {

var locationManager:  CLLocationManager?
@Published var region = MKCoordinateRegion(center: MapDetails.startingLocation, span: MapDetails.defaultSpan)

...

The yellow warning is:

'init(coordinateRegion:interactionModes:showsUserLocation:userTrackingMode:)' was deprecated in iOS 17.0: Use Map initializers that take a MapContentBuilder instead.

Is there an obvious replacement that I have overlooked?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Edward Hasted
  • 3,201
  • 8
  • 30
  • 48
  • The error message clearly tells you want to do. How about you "use `Map` initializers that take a `MapContentBuilder` instead"? – Sweeper Aug 09 '23 at 06:57
  • Apparently the new @MapContentBuilder macro is covered some in the [WWDC 2023 Meet MapKit for SwiftUI video](https://developer.apple.com/videos/play/wwdc2023/10043/) – HangarRash Aug 09 '23 at 06:57

1 Answers1

1

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

Note that you can create a MapCameraPosition from a MKCoordinateRegion by using MapCameraPosition.region(_:).

To show the user's position, use a UserAnnotation

Map(position: $viewModel.region) {
    UserAnnotation()
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • Hi - has the MapCameraPosition replaced the UserLocation? So will @State var myLocation: MapCameraPosition = .automatic give me my current location? Is there an example that I can work through that gives the user location? All that I have found use explicit locations. And haven't been able to work it backwards from the WWDC video that was my first port of call. Your notes are highly condensed and I am sure spot on. – Edward Hasted Aug 10 '23 at 10:10
  • The WWDC overview doesn't show you how to programatically get the users, location, it just adds MapUserLocationButton() to the Map(). Is there a way to programatically press this? – Edward Hasted Aug 10 '23 at 10:57
  • @EdwardHasted Oh you want to *move* the map to the user's location? I think `.automatic` should work then. You can also use `.userLocation(fallback: .automatic)` or similar. Of course, make sure you request the location permissions first. – Sweeper Aug 10 '23 at 11:05
  • @EdwardHasted You seem confused. Are you aware that I'm talking about the `MapCameraPosition` value of `$viewModel.region`? Set that initially to those values. If we are still not on the same page, I suggest that you post a new question with a [mcve]. – Sweeper Aug 10 '23 at 11:22
  • From App Developer the approach appears to be add @State private var position: MapCameraPosition = .userLocation(followsHeading: true, fallback: .automatic) and then Map(position: $position) { UserAnnotation() } but this generates the "The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions" message. – Edward Hasted Aug 10 '23 at 11:33
  • @EdwardHasted Are you using the latest Xcode beta? I don't think that is supposed to happen. – Sweeper Aug 10 '23 at 11:43