Skip to content

Commit bf4184c

Browse files
authored
fix(ios): remove main.sync from HybridMapView and async camera APIs (#45)
* fix(ios): remove main.sync from HybridMapView and async camera APIs * refactor(ios): introduce MapViewState for improved state management * refactor: simplify MapView and MapScene components by removing forwardRef and updating prop types * fix: handle potential errors in camera animations and retrieval
1 parent c2e6bf7 commit bf4184c

7 files changed

Lines changed: 387 additions & 458 deletions

File tree

example/App.tsx

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {
2-
forwardRef,
32
memo,
43
useCallback,
54
useEffect,
65
useMemo,
76
useRef,
87
useState,
98
type ReactNode,
9+
type Ref,
1010
} from 'react';
1111
import { StatusBar } from 'expo-status-bar';
1212
import {
@@ -485,6 +485,7 @@ const ScenarioDock = memo(function ScenarioDock({
485485
});
486486

487487
type MapSceneProps = {
488+
ref?: Ref<MapViewRef>;
488489
scenario: MapScenario;
489490
provider: SupportedExampleProvider;
490491
mapType: MapType;
@@ -500,25 +501,22 @@ type MapSceneProps = {
500501
onLongPress: (coordinate: Coordinate) => void;
501502
};
502503

503-
const MapScene = memo(
504-
forwardRef<MapViewRef, MapSceneProps>(function MapScene(
505-
{
506-
scenario,
507-
provider,
508-
mapType,
509-
mapPadding,
510-
animationOption,
511-
onMapReady,
512-
onClusterPress,
513-
onMarkerPress,
514-
onMarkerDragEnd,
515-
onOverlayPress,
516-
onPress,
517-
onPoiPress,
518-
onLongPress,
519-
},
520-
ref,
521-
) {
504+
const MapScene = memo(function MapScene({
505+
ref,
506+
scenario,
507+
provider,
508+
mapType,
509+
mapPadding,
510+
animationOption,
511+
onMapReady,
512+
onClusterPress,
513+
onMarkerPress,
514+
onMarkerDragEnd,
515+
onOverlayPress,
516+
onPress,
517+
onPoiPress,
518+
onLongPress,
519+
}: MapSceneProps) {
522520
const commonMapProps = {
523521
style: styles.map,
524522
mapType,
@@ -569,8 +567,7 @@ const MapScene = memo(
569567
provider="google"
570568
/>
571569
);
572-
}),
573-
);
570+
});
574571

575572
type StatusHeaderProps = {
576573
status: string;
@@ -671,29 +668,35 @@ export default function App() {
671668
);
672669

673670
const handleAnimateCamera = useCallback(() => {
674-
mapRef.current?.animateCamera(
675-
{
676-
center: {
677-
latitude: scenario.region.latitude,
678-
longitude: scenario.region.longitude,
671+
mapRef.current
672+
?.animateCamera(
673+
{
674+
center: {
675+
latitude: scenario.region.latitude,
676+
longitude: scenario.region.longitude,
677+
},
678+
zoom: 13,
679+
heading: 0,
680+
pitch: 0,
679681
},
680-
zoom: 13,
681-
heading: 0,
682-
pitch: 0,
683-
},
684-
1,
685-
);
682+
1,
683+
)
684+
?.catch(() => {});
686685
}, [scenario]);
687686

688687
const handleGetCamera = useCallback(async () => {
689-
const camera = await mapRef.current?.getCamera();
690-
if (camera == null) {
691-
return;
692-
}
688+
try {
689+
const camera = await mapRef.current?.getCamera();
690+
if (camera == null) {
691+
return;
692+
}
693693

694-
setStatus(
695-
`zoom ${camera.zoom?.toFixed(1) ?? '?'} · ${camera.center.latitude.toFixed(4)}, ${camera.center.longitude.toFixed(4)}`,
696-
);
694+
setStatus(
695+
`zoom ${camera.zoom?.toFixed(1) ?? '?'} · ${camera.center.latitude.toFixed(4)}, ${camera.center.longitude.toFixed(4)}`,
696+
);
697+
} catch {
698+
// Map unmounted or native call failed — ignore.
699+
}
697700
}, []);
698701

699702
const cycleMapType = useCallback(() => {
@@ -788,11 +791,13 @@ export default function App() {
788791
scenario.advanced?.fitToCoordinatesOnReady &&
789792
scenario.markers != null
790793
) {
791-
mapRef.current?.fitToCoordinates(
792-
scenario.markers.map((marker) => marker.coordinate),
793-
mapPadding,
794-
true,
795-
);
794+
mapRef.current
795+
?.fitToCoordinates(
796+
scenario.markers.map((marker) => marker.coordinate),
797+
mapPadding,
798+
true,
799+
)
800+
?.catch(() => {});
796801
}
797802
}, [scenario, mapPadding]);
798803

package/android/src/main/java/com/margelo/nitro/nitromaps/HybridMapView.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,14 @@ class HybridMapView(private val context: ThemedReactContext) :
271271

272272
override fun fetchCamera(): Promise<Camera> = currentAdapter().fetchCamera()
273273

274-
override fun applyCamera(camera: Camera) {
274+
override fun applyCamera(camera: Camera): Promise<Unit> {
275275
currentAdapter().applyCamera(camera)
276+
return Promise.resolved(Unit)
276277
}
277278

278-
override fun animateCamera(camera: Camera, duration: Double?) {
279+
override fun animateCamera(camera: Camera, duration: Double?): Promise<Unit> {
279280
currentAdapter().animateCamera(camera, duration)
281+
return Promise.resolved(Unit)
280282
}
281283

282284
override fun getVisibleRegion(): Promise<VisibleRegion> = currentAdapter().getVisibleRegion()
@@ -285,8 +287,9 @@ class HybridMapView(private val context: ThemedReactContext) :
285287
coordinates: Array<Coordinate>,
286288
padding: EdgePadding?,
287289
animated: Boolean?,
288-
) {
290+
): Promise<Unit> {
289291
currentAdapter().fitToCoordinates(coordinates, padding, animated)
292+
return Promise.resolved(Unit)
290293
}
291294

292295
override fun prepareForRecycle() {

0 commit comments

Comments
 (0)