|
| 1 | +--- |
| 2 | +title: Camera Gesture Observer |
| 3 | +tags: [CameraGestureObserver#onMapSteady, CameraGestureObserver#quietPeriodMs, CameraGestureObserver#maxIntervalMs] |
| 4 | +custom_props: |
| 5 | + example_rel_path: Map/CameraGestureObserver.tsx |
| 6 | +custom_edit_url: https://github.com/rnmapbox/maps/tree/master/example/src/examples/Map/CameraGestureObserver.tsx |
| 7 | +--- |
| 8 | + |
| 9 | +Demonstrates how to detect when the map becomes steady after user gestures (pan, zoom, rotate). The CameraGestureObserver component fires the onMapSteady event after a configurable quiet period, providing information about the last gesture type and idle duration. |
| 10 | + |
| 11 | + |
| 12 | +```jsx |
| 13 | +import { useCallback, useState } from 'react'; |
| 14 | +import { View, StyleSheet, Text } from 'react-native'; |
| 15 | +import { MapView, Camera, CameraGestureObserver, type OnMapSteadyEvent } from '@rnmapbox/maps'; |
| 16 | + |
| 17 | +import { ExampleWithMetadata } from '../common/ExampleMetadata'; // exclude-from-doc |
| 18 | + |
| 19 | +const styles = StyleSheet.create({ |
| 20 | + container: { |
| 21 | + flex: 1, |
| 22 | + }, |
| 23 | + map: { |
| 24 | + flex: 1, |
| 25 | + }, |
| 26 | + statusBar: { |
| 27 | + position: 'absolute', |
| 28 | + top: 50, |
| 29 | + left: 20, |
| 30 | + right: 20, |
| 31 | + backgroundColor: 'rgba(255, 255, 255, 0.95)', |
| 32 | + padding: 16, |
| 33 | + borderRadius: 8, |
| 34 | + shadowColor: '#000', |
| 35 | + shadowOffset: { width: 0, height: 2 }, |
| 36 | + shadowOpacity: 0.25, |
| 37 | + shadowRadius: 3.84, |
| 38 | + elevation: 5, |
| 39 | + }, |
| 40 | + title: { |
| 41 | + fontSize: 16, |
| 42 | + fontWeight: 'bold', |
| 43 | + marginBottom: 8, |
| 44 | + }, |
| 45 | + statusText: { |
| 46 | + fontSize: 14, |
| 47 | + lineHeight: 20, |
| 48 | + }, |
| 49 | + hint: { |
| 50 | + fontSize: 12, |
| 51 | + color: '#666', |
| 52 | + marginTop: 8, |
| 53 | + fontStyle: 'italic', |
| 54 | + }, |
| 55 | +}); |
| 56 | + |
| 57 | +const CameraGestureObserverExample = () => { |
| 58 | + const [status, setStatus] = useState('Waiting for interaction...'); |
| 59 | + |
| 60 | + const onMapSteady = useCallback(({ nativeEvent } : { nativeEvent: OnMapSteadyEvent }) => { |
| 61 | + const { reason, idleDurationMs, lastGestureType, timestamp } = nativeEvent; |
| 62 | + |
| 63 | + let message = `✓ Map is steady!\n\nReason: ${reason}`; |
| 64 | + |
| 65 | + if (reason === 'steady' && idleDurationMs !== undefined) { |
| 66 | + message += `\nIdle duration: ${Math.round(idleDurationMs)}ms`; |
| 67 | + } |
| 68 | + |
| 69 | + if (lastGestureType) { |
| 70 | + message += `\nLast gesture: ${lastGestureType}`; |
| 71 | + } |
| 72 | + |
| 73 | + message += `\nTime: ${new Date(timestamp).toLocaleTimeString()}`; |
| 74 | + |
| 75 | + console.log('[CameraGestureObserver]', nativeEvent); |
| 76 | + setStatus(message); |
| 77 | + }, []); |
| 78 | + |
| 79 | + return ( |
| 80 | + <View style={styles.container}> |
| 81 | + <MapView style={styles.map}> |
| 82 | + <Camera |
| 83 | + defaultSettings={{ |
| 84 | + centerCoordinate: [-74.006, 40.7128], |
| 85 | + zoomLevel: 12, |
| 86 | + }} |
| 87 | + /> |
| 88 | + <CameraGestureObserver |
| 89 | + quietPeriodMs={200} |
| 90 | + maxIntervalMs={5000} |
| 91 | + onMapSteady={onMapSteady} |
| 92 | + /> |
| 93 | + </MapView> |
| 94 | + <View style={styles.statusBar}> |
| 95 | + <Text style={styles.title}>Map Steady State</Text> |
| 96 | + <Text style={styles.statusText}>{status}</Text> |
| 97 | + <Text style={styles.hint}> |
| 98 | + Pan, zoom, or rotate the map to see the steady state detection |
| 99 | + </Text> |
| 100 | + </View> |
| 101 | + </View> |
| 102 | + ); |
| 103 | +}; |
| 104 | + |
| 105 | +export default CameraGestureObserverExample; |
| 106 | + |
| 107 | + |
| 108 | +``` |
| 109 | + |
| 110 | +} |
| 111 | + |
0 commit comments