-
-
Notifications
You must be signed in to change notification settings - Fork 937
Expand file tree
/
Copy pathOfflineExample.tsx
More file actions
147 lines (138 loc) · 3.86 KB
/
OfflineExample.tsx
File metadata and controls
147 lines (138 loc) · 3.86 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 { useState } from 'react';
import geoViewport from '@mapbox/geo-viewport';
import Mapbox, {
Camera,
MapView,
offlineManager,
StyleURL,
} from '@rnmapbox/maps';
import { Button, Dimensions, TextInput } from 'react-native';
import { type ExampleWithMetadata } from '../common/ExampleMetadata'; // exclude-from-doc
const CENTER_COORD: [number, number] = [-73.970895, 40.723279];
const MAPBOX_VECTOR_TILE_SIZE = 512;
console.log('=> Mapbox[0]:', Mapbox);
console.log('=> Mapbox.StyleURL[1]:', Mapbox.StyleURL);
console.log('=> StyleURL[2]:', StyleURL);
const STYLE_URL = Mapbox.StyleURL.Satellite;
const OfflineExample = () => {
const [packName, setPackName] = useState('pack-1');
const [showEditTitle, setShowEditTitle] = useState(false);
return (
<>
<Button
title={`Pack name: ${packName}`}
onPress={() => {
setShowEditTitle(!showEditTitle);
}}
/>
{showEditTitle && (
<TextInput
value={packName}
autoFocus={true}
onChangeText={(text) => setPackName(text)}
onBlur={() => setShowEditTitle(false)}
/>
)}
<Button
title="Get all packs"
onPress={async () => {
const packs = await offlineManager.getPacks();
console.log('=> packs:', packs);
packs.forEach((pack) => {
console.log(
'pack:',
pack,
'name:',
pack.name,
'bounds:',
pack?.bounds,
'metadata',
pack?.metadata,
);
});
}}
/>
<Button
title="Get pack"
onPress={async () => {
const pack = await offlineManager.getPack(packName);
if (pack) {
console.log(
'pack:',
pack,
'name:',
pack.name,
'bounds:',
pack?.bounds,
'metadata',
pack?.metadata,
);
console.log('=> status', await pack?.status());
}
}}
/>
<Button
title="Resume pack"
onPress={async () => {
const pack = await offlineManager.getPack(packName);
if (pack) {
await pack.resume();
}
}}
/>
<Button
title="Remove packs"
onPress={async () => {
const result = await offlineManager.resetDatabase();
console.log('Reset DB done:', result);
}}
/>
<Button
title="Create Pack"
onPress={() => {
const { width, height } = Dimensions.get('window');
const bounds: [number, number, number, number] = geoViewport.bounds(
CENTER_COORD,
12,
[width, height],
MAPBOX_VECTOR_TILE_SIZE,
);
const options = {
name: packName,
styleURL: STYLE_URL,
tilesets: [],
bounds: [
[bounds[0], bounds[1]],
[bounds[2], bounds[3]],
] as [[number, number], [number, number]],
minZoom: 10,
maxZoom: 20,
metadata: {
whatIsThat: 'foo',
},
};
offlineManager.createPack(options, (_region, status) =>
console.log('=> progress callback region:', 'status: ', status),
);
}}
/>
<MapView style={{ flex: 1 }} styleURL={STYLE_URL}>
<Camera zoomLevel={10} centerCoordinate={CENTER_COORD} />
</MapView>
</>
);
};
export default OfflineExample;
/* end-example-doc */
const metadata: ExampleWithMetadata['metadata'] = {
title: 'Offline Example',
tags: [
'offlineManager#createPack',
'offlineManager#getPack',
'offlineManager#getPacks',
],
docs: `
Demonstrates basic use of offlineManager api.
`,
};
OfflineExample.metadata = metadata;