-
-
Notifications
You must be signed in to change notification settings - Fork 937
Expand file tree
/
Copy pathShowPointAnnotation.tsx
More file actions
executable file
·213 lines (194 loc) · 5.31 KB
/
ShowPointAnnotation.tsx
File metadata and controls
executable file
·213 lines (194 loc) · 5.31 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { useRef, useState } from 'react';
import { View, Text, StyleSheet, Image } from 'react-native';
import {
Callout,
Camera,
FillLayer,
MapView,
PointAnnotation,
ShapeSource,
getAnnotationsLayerID,
} from '@rnmapbox/maps';
import { type Point, type Position } from 'geojson';
import { Button } from '@rneui/base';
import Bubble from '../common/Bubble';
import { type ExampleWithMetadata } from '../common/ExampleMetadata'; // exclude-from-doc
const ANNOTATION_SIZE = 40;
const styles = {
annotationContainer: {
alignItems: 'center',
backgroundColor: 'white',
borderColor: 'rgba(0, 0, 0, 0.45)',
borderRadius: ANNOTATION_SIZE / 2,
borderWidth: StyleSheet.hairlineWidth,
height: ANNOTATION_SIZE,
justifyContent: 'center',
overflow: 'hidden',
width: ANNOTATION_SIZE,
},
matchParent: {
flex: 1,
},
} as const;
type AnnotationWithRemoteImageProps = {
id: string;
title: string;
coordinate: Position;
};
const AnnotationWithRemoteImage = ({
id,
coordinate,
title,
}: AnnotationWithRemoteImageProps) => {
const pointAnnotation = useRef<PointAnnotation>(null);
return (
<PointAnnotation
id={id}
coordinate={coordinate}
title={title}
draggable
onSelected={(feature) =>
console.log('onSelected:', feature.id, feature.geometry.coordinates)
}
onDrag={(feature) =>
console.log('onDrag:', feature.id, feature.geometry.coordinates)
}
onDragStart={(feature) =>
console.log('onDragStart:', feature.id, feature.geometry.coordinates)
}
onDragEnd={(feature) =>
console.log('onDragEnd:', feature.id, feature.geometry.coordinates)
}
ref={pointAnnotation}
>
<View style={styles.annotationContainer}>
<Image
source={{ uri: 'https://reactnative.dev/img/tiny_logo.png' }}
style={{ width: ANNOTATION_SIZE, height: ANNOTATION_SIZE }}
onLoad={() => pointAnnotation.current?.refresh()}
// Prevent rendering bitmap at unknown animation state
fadeDuration={0}
/>
</View>
<Callout title="This is a sample loading a remote image" />
</PointAnnotation>
);
};
const ShowPointAnnotation = () => {
const [coordinates, setCoordinates] = useState([
[-73.99155, 40.73581],
[-73.99155, 40.73681],
]);
const [layerRendering, setLayerRendering] = useState<'below' | 'above'>(
'below',
);
const renderAnnotations = () => {
const items = [];
for (let i = 0; i < coordinates.length; i++) {
const coordinate = coordinates[i]!;
const title = `Lon: ${coordinate[0]} Lat: ${coordinate[1]}`;
const id = `pointAnnotation${i}`;
if (i % 2 === 1) {
items.push(
null,
<AnnotationWithRemoteImage
key={id}
id={id}
coordinate={coordinate}
title={title}
/>,
);
} else {
items.push(
null,
<PointAnnotation
key={id}
id={id}
coordinate={coordinate}
title={title}
>
<View style={styles.annotationContainer} />
<Callout title="This is an empty example" />
</PointAnnotation>,
);
}
}
return items;
};
return (
<>
<MapView
onPress={(feature) => {
setCoordinates((prevState) => [
...prevState,
(feature.geometry as Point).coordinates,
]);
}}
style={styles.matchParent}
deselectAnnotationOnTap={true}
>
<Camera
defaultSettings={{ centerCoordinate: coordinates[0], zoomLevel: 16 }}
/>
{renderAnnotations()}
<ShapeSource
id="polygon"
shape={{
coordinates: [
[
[-73.98813787946587, 40.73199795542578],
[-73.98313197853199, 40.7388685230859],
[-73.98962548210226, 40.74155214586244],
[-73.9945841575561, 40.73468185536569],
[-73.98813787946587, 40.73199795542578],
],
],
type: 'Polygon',
}}
>
<FillLayer
id="polygon"
{...{
[layerRendering + 'LayerID']:
getAnnotationsLayerID('PointAnnotations'),
}}
style={{
fillColor: 'rgba(255, 0, 0, 0.5)',
fillOutlineColor: 'red',
}}
/>
</ShapeSource>
</MapView>
<Bubble>
<Text style={{ marginBottom: 10 }}>
Click to add a point annotation
</Text>
<Button
onPress={() =>
setLayerRendering(
(prevState) =>
(({ above: 'below', below: 'above' }) as const)[prevState],
)
}
>
Render Polygon {{ above: 'below', below: 'above' }[layerRendering]}
</Button>
</Bubble>
</>
);
};
export default ShowPointAnnotation;
/* end-example-doc */
const metadata: ExampleWithMetadata['metadata'] = {
title: 'Show Point Annotations',
tags: [
'PointAnnotation',
'MapView#deselectAnnotationOnTap',
'PointAnnotation#refresh',
'getAnnotationsLayerID',
],
docs: `
Shows Point annotation with images
`,
};
ShowPointAnnotation.metadata = metadata;