mapbox / mapbox-maps-ios

Interactive, thoroughly customizable maps for iOS powered by vector tiles and Metal
https://www.mapbox.com/mapbox-mobile-sdk
Other
450 stars 148 forks source link

Jerky Movement of ViewAnnotation #2163

Open swasta opened 2 months ago

swasta commented 2 months ago

Environment

Observed behavior and steps to reproduce

When adding a ViewAnnotation to the map and updating its coordinates to the camera center upon camera changes, the annotation moves jerkily, especially noticeable when dragging the map quickly. This issue occurs in Maps SDK Version 11.3.0.

  1. Add a ViewAnnotation on the map with an initial coordinate.
  2. Subscribe to the cameraChanged event.
  3. In the cameraChanged event, update the annotation's coordinate to the camera center.
  4. Drag the map quickly.
  5. Observe that the annotation's movement is jerky.
let annotation = ViewAnnotation(
    coordinate: .init(latitude: 40.7128, longitude: -74.0060),
    view: SimplePointerView()
)

mapView.viewAnnotations.add(annotation)

mapView.mapboxMap.onCameraChanged
    .observe { cameraChange in
        annotation.annotatedFeature.geometry = .point(.init(cameraChange.cameraState.center))
    }
    .store(in: &cancellables)

Expected behavior

The annotation is expected to move smoothly across the map in response to camera changes, similar to the behavior observed in Maps SDK Version 10.17.0.

Notes / preliminary analysis

In our actual project, we're not just sticking the annotation's view to the camera's center. I'm doing this in the issue to demonstrate the problem in the simplest way. Actually, we're using our own calculated coordinates to update the annotations' positions and observing this stuttering movement. With this upgrade from v10 to v11 throwing us this curveball, we're kind of thinking about ditching moving annotations through Mapbox and maybe just sticking UIViews on top of the mapView instead. Not exactly thrilled about having to possibly go this route because of the update. It’s extra work we weren’t planning on.

Additional links and references

Video demonstrating jerky movement in v11.3.0: Link to video

Video showing smooth movement in v10.17.0: Link to video

persidskiy commented 2 months ago

Hi @swasta , can you try the setting the presentsWithTransaction = true? Usually, it helps with view annotations issues

https://docs.mapbox.com/ios/maps/api/11.3.0/documentation/mapboxmaps/mapview/presentswithtransaction

swasta commented 2 months ago

can you try the setting the presentsWithTransaction = true? Usually, it helps with view annotations issues

Unfortunately, setting this property doesn't seem to change anything

persidskiy commented 2 months ago

Actually, we're using our own calculated coordinates to update the annotations' positions and observing this stuttering movement.

@swasta Do I understand correctly that in onCameraChanged, you calculate the geo coordinates of the annotation and then set them as the annotation's geometry?

Do you need to use view annotations then? You probably can just use point(for: coordinate) to position the views

swasta commented 2 months ago

@persidskiy Yes, that’s exactly right! Imagine we have one ViewAnnotation positioned in the middle of the screen. When the user drags the map causing the annotation to move outside the visible viewport, we want to maintain a pointer—a UIView—close to the edge of the screen where the annotation exited. This visual cue informs users that they’ve panned the annotation off-screen and indicates which direction they should drag the map to bring the annotation back into view. To accomplish this, we calculate the pointer's coordinate so it’s adjacent to the appropriate screen boundary. With each movement of the camera, we recalculate this coordinate and adjust the pointer's position accordingly. This mechanism worked flawlessly in SDK version 10.17.0, but we've noticed that the movement has lost its smoothness after upgrading to version 11.3.0.

Do you need to use view annotations then?

You're probably right; we might not necessarily need to use them. However, as mentioned, this functionality was working perfectly until we decided to upgrade to version 11.