Repository navigation
fix(android): apply instant camera updates with setCamera - #4332
Conversation
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.
|
Thanks @RobJacobson, this is the right fix. I reproduced #4273 on I pushed one small commit on top (272b7c0). Interrupt reproducerimport 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; |
|
(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. |
Description
Fixes #4273
On a cold style cache, an instant camera update was sent through
flyTowith 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.setInitialCameraalready asks for duration 0 and modenone. This change makes that case, and any other instant update (duration == 0,move, ornone), callMapboxMap.setCameraonce and return. iOS already usessetCamerafor modenone. 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
setInitialCamerauntil the style loads still callsflyTo. 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. CallingsetCameraonly insidesetInitialCamerafixes the opening camera and leaves every other instant update on the brokenflyTo. Doing it inCameraUpdateItem.runcovers the opening camera and those other instant updates in the function that chooses the animation.Checklist
CONTRIBUTING.mdyarn generatein the root folder/exampleapp./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
defaultSettingsfor Chicago ([-87.622088, 41.878781], zoom 10) settles there.onMapIdlewas 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 firstonCameraChangedis 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:emptyStyleis a Mapbox style document with a background layer and no camera. Clear the cache with a fresh install, oradb shell run-as <package> rm -rf files/.mapbox cache/.mapbox, then readonMapIdle.