0

I'm building a navigation app and I want the map to follow the user's location and heading with a specific pitch (e.g. 60 degrees...) , similar to how Apple Maps looks during navigation, or Google.

The problem I have...

.userLocation(followsHeading:fallback:) has no parameters for pitch or distance. When you switch to it, the map follows the user correctly but always resets to a flat 0° pitch regardless of what was set before.

It would be awesome to have something like this, I have submitted a feedback to Apple

camera = .userLocation(
    followsHeading: true,
    distance: 800,
    pitch: 60,
    fallback: .automatic
)

The workaround is as follows...

Set the desired camera first, wait for the animation to complete, then switch to .userLocation which inherits the pitch and distance from whatever the map's current state is at that moment (crazy)...

camera = .camera(MapCamera(
    centerCoordinate: location.coordinate,
    distance: 800,
    heading: location.course < 0 ? 0 : location.course,
    pitch: 60
))

Task {
    try? await Task.sleep(for: .seconds(5))
    camera = .userLocation(followsHeading: true, fallback: .automatic)
}

Seeding the camera with the desired pitch and distance, then waiting 5 seconds before handing off to .userLocation. This works because .userLocation inherits the pitch from whatever the map's current camera state is at the moment of the switch. The delay gives the animation time to fully complete so the inherited values are correct

This is fragile...Is there a reliable, non-hacky way to set the initial pitch and distance when switching to .userLocation tracking programmatically? Or is this a MapKit limitation with no clean solution?

Docs: https://developer.apple.com/documentation/mapkit/mapcameraposition/userlocation(followsheading:fallback:)

This pitch is kind of important and I don't know of any reliable way to achieve it. It's more of a safety feature, This navigation app is built specifically for truck drivers, and the angled view gives them a much better perspective of what's coming up ahead so they can position in the correct lane in time. The app is about 90% done and this is the last major thing I need to sort out

enter image description here

2
  • I don't think you have to wait 5 seconds for the animation to finish. Whenever the location changes, do something like: cameraPosition = .camera(MapCamera(centerCoordinate: location.coordinate, distance: 800, heading: location.course < 0 ? 0 : location.course, pitch: 60)) where @State private var cameraPosition: MapCameraPosition = .camera(MapCamera(centerCoordinate: .init(latitude: 0, longitude: 0), distance: 800, heading: 0, pitch: 60)) and Map(position: $cameraPosition) {...}. Commented 5 hours ago
  • Tried this variant before but it's not reliable. It works fine when walking but when driving it's laggy....not even close to .userLocation, which uses the device's gyroscope and accelerometer to interpolate between GPS ticks (and whatever Apple uses behind the scenes that is not exposing)... The idea was to use .userLocation somehow with a custom pitch... thank you anyway :) Commented 4 hours ago

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.