Skip to content

fix(android): apply instant camera updates with setCamera - #4332

Merged
mfazekas merged 2 commits into
rnmapbox:mainfrom
RobJacobson:fix/android-instant-camera
Oct 11, 2026
Merged

mfazekas merged 2 commits into
rnmapbox:mainfrom
RobJacobson:fix/android-instant-camera

Conversation

@RobJacobson

@RobJacobson RobJacobson commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #4273

On a cold style cache, an instant camera update was sent through flyTo with duration 0. That animation writes the center and the zoom as separate updates. The center is applied while the zoom is still 0, and Mapbox’s height constraint then keeps the latitude at 0. The map opens at the requested longitude and the wrong latitude.

setInitialCamera already asks for duration 0 and mode none. This change makes that case, and any other instant update (duration == 0, move, or none), call MapboxMap.setCamera once and return. iOS already uses setCamera for mode none. Returning also stops duration 0 from starting the flight a second time. Flights and eases with a positive duration are unchanged.

Two fixes are already on the issue. Deferring setInitialCamera until the style loads still calls flyTo. That passes when a normal style has already moved the zoom off 0, and it still loses the latitude for an empty style that has no camera of its own. Calling setCamera only inside setInitialCamera fixes the opening camera and leaves every other instant update on the broken flyTo. Doing it in CameraUpdateItem.run covers the opening camera and those other instant updates in the function that chooses the animation.

Checklist

  • I've read CONTRIBUTING.md
  • I updated the doc/other generated code with running yarn generate in the root folder
  • I have tested the new feature on /example app.
    • In V11 mode/ios
    • In New Architecture mode/ios
    • In V11 mode/android
    • In New Architecture mode/android
  • I added/updated a sample - if a new feature was implemented (/example)

No public API or generated code changed. The example instrumentation used for the runs below was removed before this branch.

Screenshot OR Video

After the fix, a cold start with defaultSettings for Chicago ([-87.622088, 41.878781], zoom 10) settles there. onMapIdle was that camera on 4/4 cold starts with the default Mapbox style, and on 4/4 cold starts with an inline style that has no center or zoom. The first onCameraChanged is still the SDK default at [0, 0]; the settled camera is the one that matters. The unpatched failure is the one already measured on this issue.

Component to reproduce the issue you're fixing

The bounds component in the issue reaches this same run(). The runs above used a center:

<MapView style={{ flex: 1 }} styleJSON={emptyStyle}>
  <Camera
    defaultSettings={{
      centerCoordinate: [-87.622088, 41.878781],
      zoomLevel: 10,
    }}
  />
</MapView>

emptyStyle is a Mapbox style document with a background layer and no camera. Clear the cache with a fresh install, or adb shell run-as <package> rm -rf files/.mapbox cache/.mapbox, then read onMapIdle.

A zero-duration flyTo writes center and zoom separately, so a cold start keeps latitude at 0. Instant updates now use one setCamera.
setCamera doesn't stop an in-progress flyTo/easeTo, so an instant update
(duration 0, none, moveTo) sent during a flight was overridden as the flight
continued. The previous zero-duration flyTo cancelled it implicitly.
@mfazekas

Copy link
Copy Markdown
Contributor

Thanks @RobJacobson, this is the right fix. I reproduced #4273 on main with the issue's component and a cold cache (6/6 at latitude 0), and with your change it's 6/6 correct. Logging confirmed your explanation: the zero-duration flyTo writes the center at the initial zoom 0.71, where the height constraint only allows latitude 0, then the zoom.

I pushed one small commit on top (272b7c0). setCamera doesn't stop a camera animation that's already running, while the old zero-duration flyTo cancelled it, so an instant update sent during a flight lost to the flight. With a 6 s flyTo to Paris interrupted after 1 s by an instant update to Tokyo, main ends in Tokyo, this PR ended in Paris for none, moveTo and flyTo with duration 0. The commit calls cancelAllAnimators() before the setCamera in jumpTo; with it both checks pass (cold start 6/6, interrupt 3/3).

Interrupt reproducer
import React, { useRef, useState } from 'react';
import { Button, Text, View } from 'react-native';
import { Camera, MapView } from '@rnmapbox/maps';

const PARIS = [2.3522, 48.8566];
const TOKYO = [139.6917, 35.6895];
const wait = (ms) => new Promise((r) => setTimeout(r, ms));

const BugReportExample = () => {
  const map = useRef(null);
  const camera = useRef(null);
  const [status, setStatus] = useState('idle');

  const run = async (mode) => {
    camera.current.setCamera({ centerCoordinate: [-74, 40.7], zoomLevel: 3, animationDuration: 0 });
    await wait(1500);
    camera.current.setCamera({ centerCoordinate: PARIS, zoomLevel: 5, animationMode: 'flyTo', animationDuration: 6000 });
    await wait(1000);
    camera.current.setCamera({ centerCoordinate: TOKYO, zoomLevel: 5, animationMode: mode, animationDuration: 0 });
    await wait(7000);
    const c = await map.current.getCenter();
    const ok = Math.abs(c[0] - TOKYO[0]) < 1 && Math.abs(c[1] - TOKYO[1]) < 1;
    setStatus(`${mode}: ${ok ? 'PASS' : 'FAIL'} center=[${c[0].toFixed(2)}, ${c[1].toFixed(2)}]`);
  };

  return (
    <View style={{ flex: 1 }}>
      <View style={{ flexDirection: 'row' }}>
        <Button title="none" onPress={() => run('none')} />
        <Button title="moveTo" onPress={() => run('moveTo')} />
        <Button title="flyTo 0" onPress={() => run('flyTo')} />
      </View>
      <Text>{status}</Text>
      <MapView ref={map} style={{ flex: 1 }}>
        <Camera ref={camera} />
      </MapView>
    </View>
  );
};

export default BugReportExample;

@mfazekas
mfazekas merged commit 355de12 into rnmapbox:main Oct 11, 2026
9 checks passed
@RobJacobson

Copy link
Copy Markdown
Contributor Author

(Blushing.) Thanks. This is my first OSS commit. My tombstone will read: "Fixed a glitch with rnmapbox. He died happy."

Even weirder, I don't know a damn thing about native development using Kotlin. To give credit where credit is due, thank Grok 4.7 in Cursor. However, I'm happy that I helped to make it happen. Learning Kotlin properly is next on my to-do list.

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Android: initial camera from Camera defaultSettings lands at latitude 0 on a cold style cache

2 participants