-
-
Notifications
You must be signed in to change notification settings - Fork 937
Expand file tree
/
Copy pathFeatureState.tsx
More file actions
147 lines (130 loc) · 3.59 KB
/
FeatureState.tsx
File metadata and controls
147 lines (130 loc) · 3.59 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
import { type ComponentProps, useCallback, useRef } from 'react';
import { Camera, FillLayer, MapView, ShapeSource } from '@rnmapbox/maps';
import { type ExampleWithMetadata } from '../common/ExampleMetadata';
type CustomProperties = {
normalColor: string;
selectedColor: string;
};
const SOURCE: GeoJSON.FeatureCollection<GeoJSON.Polygon, CustomProperties> = {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
id: 1,
properties: { normalColor: '#0000cc', selectedColor: '#ff0000' },
geometry: {
type: 'Polygon',
coordinates: [
[
[-107.8857421875, 45.197522303056815],
[-106.5234375, 36.58024660149866],
[-120.498046875, 36.10237644873645],
[-120.05859375, 44.918139299585135],
[-107.8857421875, 45.197522303056815],
],
],
},
},
{
type: 'Feature',
id: 2,
properties: { normalColor: '#00cc00', selectedColor: '#ff00ff' },
geometry: {
type: 'Polygon',
coordinates: [
[
[-90.59326171875, 45.25942203635168],
[-87.1875, 40.46366632458768],
[-87.1875, 37.07271048132945],
[-102.81005859375, 36.89719446989035],
[-101.77734375, 44.8714427501659],
[-90.59326171875, 45.25942203635168],
],
],
},
},
],
};
const SHAPES_SOURCE_ID = 'shapes';
const SELECTED_KEY = 'selected';
const styles = {
map: {
flex: 1,
},
shapesFill: {
fillColor: [
'case',
['to-boolean', ['feature-state', SELECTED_KEY]],
['get', 'selectedColor'],
['get', 'normalColor'],
],
},
} as const;
type ShapeSourceProps = ComponentProps<typeof ShapeSource>;
const FeatureState = () => {
const mapRef = useRef<MapView>(null);
const toggleSelected = useCallback(async (featureId: string) => {
const { current: map } = mapRef;
if (!map) return;
console.log(`Toggling selected state for ID '${featureId}'`);
try {
const currentState = await map.getFeatureState(
featureId,
SHAPES_SOURCE_ID,
);
console.log(`Current state: ${JSON.stringify(currentState)}`);
if ('selected' in currentState) {
await map.removeFeatureState(featureId, SELECTED_KEY, SHAPES_SOURCE_ID);
} else {
await map.setFeatureState(
featureId,
{
[SELECTED_KEY]: true,
},
SHAPES_SOURCE_ID,
);
}
} catch (err) {
console.error(
`Failed to toggle feature state for ID '${featureId}': ${
(err as Error).message
}`,
);
}
}, []);
const handlePressShapes: NonNullable<ShapeSourceProps['onPress']> =
useCallback(
(e) => {
const [feature] = e.features;
if (!feature || feature.id === undefined) return;
toggleSelected(feature.id.toString());
},
[toggleSelected],
);
return (
<MapView ref={mapRef} style={styles.map}>
<Camera zoomLevel={2} centerCoordinate={[-101.051593, 41.370337]} />
<ShapeSource
id={SHAPES_SOURCE_ID}
shape={SOURCE}
onPress={handlePressShapes}
>
<FillLayer id="shapesFill" style={styles.shapesFill} />
</ShapeSource>
</MapView>
);
};
export default FeatureState;
const metadata: ExampleWithMetadata['metadata'] = {
title: 'Feature State',
tags: [
'MapView',
'MapView#setFeatureState',
'MapView#getFeatureState',
'MapView#removeFeatureState',
],
docs: `
Demonstrates modifying the feature state.
`,
};
FeatureState.metadata = metadata;