|
| 1 | +import geoViewport from '@mapbox/geo-viewport'; |
| 2 | +import Mapbox, { |
| 3 | + Camera, |
| 4 | + MapView, |
| 5 | + offlineManagerLegacy, |
| 6 | + StyleURL, |
| 7 | +} from '@rnmapbox/maps'; |
| 8 | +import React, { useRef, useState } from 'react'; |
| 9 | +import { Button, Dimensions, TextInput } from 'react-native'; |
| 10 | + |
| 11 | +import { ExampleWithMetadata } from '../common/ExampleMetadata'; // exclude-from-doc |
| 12 | + |
| 13 | +const CENTER_COORD: [number, number] = [-73.970895, 40.723279]; |
| 14 | +const MAPBOX_VECTOR_TILE_SIZE = 512; |
| 15 | +console.log('=> Mapbox[0]:', Mapbox); |
| 16 | +console.log('=> Mapbox.StyleURL[1]:', Mapbox.StyleURL); |
| 17 | +console.log('=> StyleURL[2]:', StyleURL); |
| 18 | +const STYLE_URL = Mapbox.StyleURL.Satellite; |
| 19 | + |
| 20 | +const OfflineLegacyExample = () => { |
| 21 | + const [packName, setPackName] = useState('pack-1'); |
| 22 | + const [showEditTitle, setShowEditTitle] = useState(false); |
| 23 | + const [tileLimit, setTileLimit] = useState(6000); |
| 24 | + const [timeout, setTimeout] = useState(60); |
| 25 | + |
| 26 | + const map = useRef(); |
| 27 | + |
| 28 | + return ( |
| 29 | + <> |
| 30 | + <Button |
| 31 | + title={`Pack name: ${packName}`} |
| 32 | + onPress={() => { |
| 33 | + setShowEditTitle(!showEditTitle); |
| 34 | + }} |
| 35 | + /> |
| 36 | + {showEditTitle && ( |
| 37 | + <TextInput |
| 38 | + value={packName} |
| 39 | + autoFocus={true} |
| 40 | + onChangeText={(text) => setPackName(text)} |
| 41 | + onBlur={() => setShowEditTitle(false)} |
| 42 | + /> |
| 43 | + )} |
| 44 | + <Button |
| 45 | + title={`Toggle tile limit (${tileLimit})`} |
| 46 | + onPress={() => { |
| 47 | + setTileLimit(tileLimit === 6000 ? 10 : 6000); |
| 48 | + offlineManagerLegacy.setTileCountLimit( |
| 49 | + tileLimit === 6000 ? 10 : 6000, |
| 50 | + ); |
| 51 | + }} |
| 52 | + /> |
| 53 | + <Button |
| 54 | + title={`Toggle timeout (${timeout})`} |
| 55 | + onPress={() => { |
| 56 | + setTimeout(timeout === 60 ? 10 : 60); |
| 57 | + offlineManagerLegacy.setTimeout(timeout === 60 ? 10 : 60); |
| 58 | + }} |
| 59 | + /> |
| 60 | + <Button |
| 61 | + title="Get all packs" |
| 62 | + onPress={async () => { |
| 63 | + const packs = await offlineManagerLegacy.getPacks(); |
| 64 | + console.log('=> packs:', packs); |
| 65 | + packs.forEach((pack) => { |
| 66 | + console.log( |
| 67 | + 'pack:', |
| 68 | + pack, |
| 69 | + 'name:', |
| 70 | + pack.name, |
| 71 | + 'bounds:', |
| 72 | + pack?.bounds, |
| 73 | + 'metadata', |
| 74 | + pack?.metadata, |
| 75 | + ); |
| 76 | + }); |
| 77 | + }} |
| 78 | + /> |
| 79 | + <Button |
| 80 | + title="Get pack" |
| 81 | + onPress={async () => { |
| 82 | + const pack = await offlineManagerLegacy.getPack(packName); |
| 83 | + if (pack) { |
| 84 | + console.log( |
| 85 | + 'pack:', |
| 86 | + pack, |
| 87 | + 'name:', |
| 88 | + pack.name, |
| 89 | + 'bounds:', |
| 90 | + pack?.bounds, |
| 91 | + 'metadata', |
| 92 | + pack?.metadata, |
| 93 | + ); |
| 94 | + |
| 95 | + console.log('=> status', await pack?.status()); |
| 96 | + } |
| 97 | + }} |
| 98 | + /> |
| 99 | + <Button |
| 100 | + title="Pause pack" |
| 101 | + onPress={async () => { |
| 102 | + const pack = await offlineManagerLegacy.getPack(packName); |
| 103 | + if (pack) { |
| 104 | + await pack.pause(); |
| 105 | + } |
| 106 | + }} |
| 107 | + /> |
| 108 | + <Button |
| 109 | + title="Resume pack" |
| 110 | + onPress={async () => { |
| 111 | + const pack = await offlineManagerLegacy.getPack(packName); |
| 112 | + if (pack) { |
| 113 | + await pack.resume(); |
| 114 | + } |
| 115 | + }} |
| 116 | + /> |
| 117 | + <Button |
| 118 | + title="Remove packs" |
| 119 | + onPress={async () => { |
| 120 | + const result = await offlineManagerLegacy.resetDatabase(); |
| 121 | + console.log('Reset DB done:', result); |
| 122 | + }} |
| 123 | + /> |
| 124 | + <Button |
| 125 | + title="Create Pack" |
| 126 | + onPress={async () => { |
| 127 | + if (map.current) { |
| 128 | + const { width, height } = Dimensions.get('window'); |
| 129 | + const mapCenter = await map.current.getCenter(); |
| 130 | + |
| 131 | + const bounds: [number, number, number, number] = geoViewport.bounds( |
| 132 | + mapCenter, |
| 133 | + 13, |
| 134 | + [width, height], |
| 135 | + MAPBOX_VECTOR_TILE_SIZE, |
| 136 | + ); |
| 137 | + |
| 138 | + const options = { |
| 139 | + name: packName, |
| 140 | + styleURL: STYLE_URL, |
| 141 | + bounds: [ |
| 142 | + [bounds[0], bounds[1]], |
| 143 | + [bounds[2], bounds[3]], |
| 144 | + ] as [[number, number], [number, number]], |
| 145 | + minZoom: 10, |
| 146 | + maxZoom: 14, |
| 147 | + metadata: { |
| 148 | + whatIsThat: 'foo', |
| 149 | + }, |
| 150 | + }; |
| 151 | + |
| 152 | + const pack = await offlineManagerLegacy.createPack( |
| 153 | + options, |
| 154 | + (region, status) => |
| 155 | + console.log('=> progress callback region:', 'status: ', status), |
| 156 | + (region, error) => |
| 157 | + console.error('=> error callback region:', 'error:', error), |
| 158 | + ); |
| 159 | + |
| 160 | + console.log('Create pack', pack); |
| 161 | + } |
| 162 | + }} |
| 163 | + /> |
| 164 | + <MapView ref={map} style={{ flex: 1 }} styleURL={STYLE_URL}> |
| 165 | + <Camera zoomLevel={10} centerCoordinate={CENTER_COORD} /> |
| 166 | + </MapView> |
| 167 | + </> |
| 168 | + ); |
| 169 | +}; |
| 170 | + |
| 171 | +export default OfflineLegacyExample; |
| 172 | + |
| 173 | +/* end-example-doc */ |
| 174 | + |
| 175 | +const metadata: ExampleWithMetadata['metadata'] = { |
| 176 | + title: 'Offline Legacy Example', |
| 177 | + tags: [ |
| 178 | + 'offlineManagerLegacy#createPack', |
| 179 | + 'offlineManagerLegacy#getPack', |
| 180 | + 'offlineManagerLegacy#getPacks', |
| 181 | + ], |
| 182 | + docs: ` |
| 183 | +Demonstates basic use of offlineManagerLegacy api. |
| 184 | +`, |
| 185 | +}; |
| 186 | +OfflineLegacyExample.metadata = metadata; |
0 commit comments