Skip to content

Commit e452ac2

Browse files
committed
feat: add support for offline downloading of tilesets
1 parent 96552bc commit e452ac2

File tree

10 files changed

+502
-462
lines changed

10 files changed

+502
-462
lines changed

android/src/main/java/com/rnmapbox/rnmbx/modules/RNMBXOfflineModule.kt

+15-14
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class TileRegionPack(var name: String, var state: TileRegionPackState = TileRegi
5151

5252
// stored in metadata for resume functionality
5353
var styleURI: String? = null
54+
var tilesets: List<String> = emptyList()
5455
var bounds: Geometry? = null
5556
var zoomRange: ZoomRange? = null
5657

@@ -79,17 +80,20 @@ class TileRegionPack(var name: String, var state: TileRegionPackState = TileRegi
7980
name: String,
8081
state: TileRegionPackState = TileRegionPackState.UNKNOWN,
8182
styleURI: String,
83+
tilesets: List<String>,
8284
bounds: Geometry,
8385
zoomRange: ZoomRange,
8486
metadata: JSONObject
85-
) : this(name= name, state= state,progress= null, metadata= metadata) {
87+
) : this(name= name, state= state, progress= null, metadata= metadata) {
8688
val rnmeta = JSONObject()
8789
rnmeta.put("styleURI", styleURI)
8890
this.styleURI = styleURI
8991
rnmeta.put("bounds", bounds.toJSONObject())
9092
this.bounds = bounds
9193
rnmeta.put("zoomRange", JSONArray(arrayOf(zoomRange.minZoom, zoomRange.maxZoom)))
9294
this.zoomRange = zoomRange
95+
rnmeta.put("tilesets", JSONArray(tilesets))
96+
this.tilesets = tilesets
9397
this.metadata.put(RNMapboxInfoMetadataKey, rnmeta);
9498
}
9599
}
@@ -143,9 +147,11 @@ class RNMBXOfflineModule(private val mReactContext: ReactApplicationContext) :
143147
val boundsFC = FeatureCollection.fromJson(boundsStr)
144148
val bounds = convertPointPairToBounds(boundsFC)
145149

150+
val tilesets = options.getArray("tilesets")?.toArrayList()?.map { it as String } ?: emptyList()
146151
val actPack = TileRegionPack(
147152
name = id,
148153
styleURI = options.getString("styleURL")!!,
154+
tilesets = tilesets,
149155
bounds = bounds,
150156
zoomRange = ZoomRange(
151157
minZoom = options.getInt("minZoom").toByte(),
@@ -280,30 +286,25 @@ class RNMBXOfflineModule(private val mReactContext: ReactApplicationContext) :
280286
?: return Result.failure(IllegalArgumentException("startLoading failed as there is no styleURI in pack"))
281287
val metadata = pack.metadata
282288
?: return Result.failure(IllegalArgumentException("startLoading failed as there is no metadata in pack"))
283-
284289
val stylePackOptions = StylePackLoadOptions.Builder()
285290
.glyphsRasterizationMode(GlyphsRasterizationMode.IDEOGRAPHS_RASTERIZED_LOCALLY)
286291
.metadata(metadata.toMapboxValue())
287292
.build()
288-
289-
val descriptorOptions = TilesetDescriptorOptions.Builder()
290-
.styleURI(styleURI)
291-
.minZoom(zoomRange.minZoom)
292-
.maxZoom(zoomRange.maxZoom)
293-
.stylePackOptions(stylePackOptions)
294-
.pixelRatio(2.0f)
295-
.build()
296-
val tilesetDescriptor = offlineManager.createTilesetDescriptor(descriptorOptions)
297-
293+
val descriptors = getTilesetDescriptors(
294+
offlineManager = offlineManager,
295+
styleURI = styleURI,
296+
minZoom = zoomRange.minZoom,
297+
maxZoom = zoomRange.maxZoom,
298+
stylePackOptions = stylePackOptions,
299+
tilesets = pack.tilesets)
298300
val loadOptions = TileRegionLoadOptions.Builder()
299301
.geometry(bounds)
300-
.descriptors(arrayListOf(tilesetDescriptor))
302+
.descriptors(descriptors)
301303
.metadata(metadata.toMapboxValue())
302304
.acceptExpired(true)
303305
.networkRestriction(NetworkRestriction.NONE)
304306
.averageBytesPerSecond(null)
305307
.build()
306-
307308
var lastProgress: TileRegionLoadProgress? = null
308309
val task = this.tileStore.loadTileRegion(
309310
id, loadOptions,

android/src/main/mapbox-v11-compat/v10/com/rnmapbox/rnmbx/v11compat/OfflineManager.kt

+26
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import com.mapbox.common.toValue
66
import com.mapbox.maps.OfflineManager
77
import com.mapbox.maps.OfflineRegionManager
88
import com.mapbox.maps.ResourceOptions
9+
import com.mapbox.maps.StylePackLoadOptions
10+
import com.mapbox.maps.TilesetDescriptorOptions
11+
import com.mapbox.maps.TilesetDescriptorOptionsForTilesets
12+
import com.mapbox.common.TilesetDescriptor
913

1014
fun getOfflineRegionManager(getAccessToken: () -> String): OfflineRegionManager {
1115
return OfflineRegionManager(ResourceOptions.Builder().accessToken(getAccessToken()).build())
@@ -20,6 +24,28 @@ fun getOfflineManager(tileStore: TileStore, getAccessToken: () -> String): Offli
2024
)
2125
}
2226

27+
fun getTilesetDescriptors(offlineManager: OfflineManager, styleURI: String, minZoom: Byte, maxZoom: Byte, stylePackOptions: StylePackLoadOptions, tilesets: List<String>): ArrayList<TilesetDescriptor>{
28+
val descriptorOptions = TilesetDescriptorOptions.Builder()
29+
.styleURI(styleURI)
30+
.minZoom(minZoom)
31+
.maxZoom(maxZoom)
32+
.stylePackOptions(stylePackOptions)
33+
.pixelRatio(2.0f)
34+
.build()
35+
val descriptor = offlineManager.createTilesetDescriptor(descriptorOptions)
36+
val tilesetDescriptorOptions = TilesetDescriptorOptionsForTilesets.Builder()
37+
.tilesets(tilesets)
38+
.minZoom(minZoom)
39+
.maxZoom(maxZoom)
40+
.build()
41+
val tilesetDescriptor = offlineManager.createTilesetDescriptor(tilesetDescriptorOptions)
42+
val descriptors = arrayListOf(descriptor)
43+
if (tilesets.isNotEmpty()) {
44+
descriptors.add(tilesetDescriptor)
45+
}
46+
return descriptors
47+
}
48+
2349
fun TileStore.setAccessToken(token: String) {
2450
this.setOption(TileStoreOptions.MAPBOX_ACCESS_TOKEN, token.toValue());
2551
}

android/src/main/mapbox-v11-compat/v11/com/rnmapbox/rnmbx/v11compat/OfflineManager.kt

+32
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,45 @@ import com.mapbox.common.MapboxOptions
44
import com.mapbox.common.TileStore
55
import com.mapbox.maps.OfflineManager
66
import com.mapbox.maps.OfflineRegionManager
7+
import com.mapbox.maps.StylePackLoadOptions
8+
import com.mapbox.maps.TilesetDescriptorOptions
9+
import com.mapbox.common.TilesetDescriptor
710

811
fun getOfflineRegionManager(getAccessToken: () -> String): OfflineRegionManager {
912
return OfflineRegionManager()
1013
}
14+
1115
fun getOfflineManager(tileStore: TileStore, getAccessToken: () -> String): OfflineManager {
1216
return OfflineManager()
1317
}
18+
19+
fun getTilesetDescriptors(offlineManager: OfflineManager, styleURI: String, minZoom: Byte, maxZoom: Byte, stylePackOptions: StylePackLoadOptions, tilesets: List<String>): ArrayList<TilesetDescriptor>{
20+
if (tilesets.isNotEmpty()) {
21+
val descriptorOptions = TilesetDescriptorOptions.Builder()
22+
.styleURI(styleURI)
23+
.minZoom(minZoom)
24+
.maxZoom(maxZoom)
25+
// TODO: When tilesets is passed in the mappack doesn't save -- not sure why
26+
// .tilesets(tilesets)
27+
.stylePackOptions(stylePackOptions)
28+
.pixelRatio(2.0f)
29+
.build()
30+
val descriptor = offlineManager.createTilesetDescriptor(descriptorOptions)
31+
val descriptors = arrayListOf(descriptor)
32+
return descriptors
33+
}
34+
val descriptorOptions = TilesetDescriptorOptions.Builder()
35+
.styleURI(styleURI)
36+
.minZoom(minZoom)
37+
.maxZoom(maxZoom)
38+
.stylePackOptions(stylePackOptions)
39+
.pixelRatio(2.0f)
40+
.build()
41+
val descriptor = offlineManager.createTilesetDescriptor(descriptorOptions)
42+
val descriptors = arrayListOf(descriptor)
43+
return descriptors
44+
}
45+
1446
fun TileStore.setAccessToken(token: String) {
1547
MapboxOptions.accessToken = token
1648
}

example/android/gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ org.gradle.jvmargs=-Xmx2560m -XX:MaxMetaspaceSize=1024m
2222
# https://developer.android.com/topic/libraries/support-library/androidx-rn
2323
android.useAndroidX=true
2424

25-
RNMBX11=false
25+
RNMBX11=true
2626

2727
# Use this property to specify which architecture you want to build.
2828
# You can also override it from the CLI using

0 commit comments

Comments
 (0)