-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnhancedMapComponent.js
More file actions
170 lines (151 loc) · 4.5 KB
/
EnhancedMapComponent.js
File metadata and controls
170 lines (151 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import React, { useRef, useEffect, useImperativeHandle, forwardRef } from 'react';
import { View, StyleSheet, UIManager, findNodeHandle, Alert } from 'react-native';
import { requireNativeComponent } from 'react-native';
import EventEmitterModule from './EventEmitterModule'; // Assuming this module is properly imported
const RNMapView = requireNativeComponent('RNMapView');
/**
* EnhancedMapComponent - A React Native component that wraps a native map view
*
* @param {Object} props - Component props
* @param {Object} props.style - Additional styles to apply to the map view
* @param {Function} props.onImagePress - Callback when an image on the map is pressed
* @param {boolean} props.showImageActions - Whether to show default image actions on press (move/delete)
* @param {number} props.imageDiameter - Default diameter for images placed on the map
* @param {React.RefObject} ref - Reference to access map methods imperatively
*/
const EnhancedMapComponent = forwardRef((props, ref) => {
const {
style,
onImagePress,
showImageActions = true,
imageDiameter,
...restProps
} = props;
const mapRef = useRef(null);
useEffect(() => {
// Set initial image diameter if provided
if (imageDiameter && mapRef.current) {
setImageDiameter(imageDiameter);
}
}, [imageDiameter]);
useEffect(() => {
// Listen for image press events from the native map
const eventListener = EventEmitterModule.addListener('MyCustomEvent', (event) => {
console.log('Map image clicked', event.imageId);
// Call custom handler if provided
if (onImagePress) {
onImagePress(event.imageId, event);
}
// Show default actions if enabled
if (showImageActions) {
Alert.alert(
'Image Options',
`Selected image: ${event.imageId}`,
[
{
text: 'Move',
onPress: () => {
updateImageLocation(event.imageId, 100, 100);
},
style: 'default'
},
{
text: 'Delete',
onPress: () => {
removeImageOnMap(event.imageId);
},
style: 'destructive'
},
{
text: 'Cancel',
style: 'cancel'
}
]
);
}
});
return () => {
eventListener.remove();
};
}, [onImagePress, showImageActions]);
// Map manipulation methods
const appendPoint = (longitude, latitude) => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(mapRef.current),
UIManager.RNMapView.Commands.appendPoint,
[longitude, latitude]
);
};
const clearMap = () => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(mapRef.current),
UIManager.RNMapView.Commands.clearMap,
[]
);
};
const setColor = (r, g, b) => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(mapRef.current),
UIManager.RNMapView.Commands.setColor,
[r, g, b]
);
};
const addImageOnMap = (url, id, longitude, latitude) => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(mapRef.current),
UIManager.RNMapView.Commands.addImageOnMap,
[url, id, longitude, latitude]
);
};
const updateImageLocation = (id, longitude, latitude) => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(mapRef.current),
UIManager.RNMapView.Commands.updateImageLocation,
[id, longitude, latitude]
);
};
const removeImageOnMap = (id) => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(mapRef.current),
UIManager.RNMapView.Commands.removeImageOnMap,
[id]
);
};
const setImageDiameter = (diameter) => {
UIManager.dispatchViewManagerCommand(
findNodeHandle(mapRef.current),
UIManager.RNMapView.Commands.setImageDiameter,
[diameter]
);
};
// Expose methods to parent component through ref
useImperativeHandle(ref, () => ({
appendPoint,
clearMap,
setColor,
addImageOnMap,
updateImageLocation,
removeImageOnMap,
setImageDiameter,
// Access to the native map view reference
getNativeMapRef: () => mapRef.current
}));
return (
<View style={[styles.container, style]}>
<RNMapView
ref={mapRef}
style={styles.map}
{...restProps}
/>
</View>
);
});
const styles = StyleSheet.create({
container: {
flex: 1,
},
map: {
flex: 1,
},
});
export default EnhancedMapComponent;