From 31bdad0c4986c84a990f73e1a0dd6fbf5b7b6be2 Mon Sep 17 00:00:00 2001 From: Tenshi Date: Thu, 4 Jul 2024 16:55:44 +0900 Subject: [PATCH] [FIX] Update fitBounds function to accept an array for padding & Fix Android Release Mode Crash (#297) * [FIX] type assertion * [FIX] javadoc * [FIX] type assertion * Update Camera.md * Update docs.json * Update Camera.tsx * [FIX] camera docs * [FIX] fitbound padding & animationDuration type * [FIX] updateMarkers --- .../annotation/MarkerViewManager.java | 8 +- docs/Camera.md | 2 +- docs/docs.json | 6 +- docs/offlineManager.md | 246 ++++++++++++++++++ javascript/components/Camera.tsx | 6 +- 5 files changed, 258 insertions(+), 10 deletions(-) create mode 100644 docs/offlineManager.md diff --git a/android/rctmln/src/main/java/com/maplibre/rctmln/components/annotation/MarkerViewManager.java b/android/rctmln/src/main/java/com/maplibre/rctmln/components/annotation/MarkerViewManager.java index 8f541cc1e..25b46bfaf 100644 --- a/android/rctmln/src/main/java/com/maplibre/rctmln/components/annotation/MarkerViewManager.java +++ b/android/rctmln/src/main/java/com/maplibre/rctmln/components/annotation/MarkerViewManager.java @@ -54,9 +54,11 @@ public void restoreViews() { public void updateMarkers(){ - try { - for( int i = 0; i < markers.size(); i++ ){ - markerUpdate.invoke(markers.get(i)); + try { + if (markerUpdate != null) { + for( int i = 0; i < markers.size(); i++ ){ + markerUpdate.invoke(markers.get(i)); + } } } catch (IllegalArgumentException e) { System.out.println(e.toString()); } diff --git a/docs/Camera.md b/docs/Camera.md index 06cb2ed0e..561f095ca 100644 --- a/docs/Camera.md +++ b/docs/Camera.md @@ -36,7 +36,7 @@ Map camera transitions to fit provided bounds | ---- | :--: | :------: | :----------: | | `northEastCoordinates` | `Array` | `Yes` | North east coordinate of bound | | `southWestCoordinates` | `Array` | `Yes` | South west coordinate of bound | -| `padding` | `Number` | `No` | Camera padding for bound | +| `padding` | `n/a` | `No` | Padding for the bounds | | `animationDuration` | `Number` | `No` | Duration of camera animation | diff --git a/docs/docs.json b/docs/docs.json index 8a9f0dacf..e604f88ea 100644 --- a/docs/docs.json +++ b/docs/docs.json @@ -268,7 +268,7 @@ "methods": [ { "name": "fitBounds", - "docblock": "Map camera transitions to fit provided bounds\n\n@example\nthis.camera.fitBounds([lng, lat], [lng, lat])\nthis.camera.fitBounds([lng, lat], [lng, lat], 20, 1000) // padding for all sides\nthis.camera.fitBounds([lng, lat], [lng, lat], [verticalPadding, horizontalPadding], 1000)\nthis.camera.fitBounds([lng, lat], [lng, lat], [top, right, bottom, left], 1000)\n\n@param {Array} northEastCoordinates - North east coordinate of bound\n@param {Array} southWestCoordinates - South west coordinate of bound\n@param {Number=} padding - Camera padding for bound\n@param {Number=} animationDuration - Duration of camera animation\n@return {void}", + "docblock": "Map camera transitions to fit provided bounds\n\n@example\nthis.camera.fitBounds([lng, lat], [lng, lat])\nthis.camera.fitBounds([lng, lat], [lng, lat], 20, 1000) // padding for all sides\nthis.camera.fitBounds([lng, lat], [lng, lat], [verticalPadding, horizontalPadding], 1000)\nthis.camera.fitBounds([lng, lat], [lng, lat], [top, right, bottom, left], 1000)\n\n@param {Array} northEastCoordinates - North east coordinate of bound\n@param {Array} southWestCoordinates - South west coordinate of bound\n@param {Number|Array=} padding - Padding for the bounds\n@param {Number=} animationDuration - Duration of camera animation\n@return {void}", "modifiers": [], "params": [ { @@ -289,9 +289,9 @@ }, { "name": "padding", - "description": "Camera padding for bound", + "description": "Padding for the bounds", "type": { - "name": "Number" + "name": null }, "optional": true }, diff --git a/docs/offlineManager.md b/docs/offlineManager.md new file mode 100644 index 000000000..59e119f3a --- /dev/null +++ b/docs/offlineManager.md @@ -0,0 +1,246 @@ + +## +### OfflineManager implements a singleton (shared object) that manages offline packs.
All of this class’s instance methods are asynchronous, reflecting the fact that offline resources are stored in a database.
The shared object maintains a canonical collection of offline packs. + + +### methods +#### createPack(options[, progressListener][, errorListener]) + +Creates and registers an offline pack that downloads the resources needed to use the given region offline. + +##### arguments +| Name | Type | Required | Description | +| ---- | :--: | :------: | :----------: | +| `options` | `OfflineCreatePackOptions` | `Yes` | Create options for a offline pack that specifices zoom levels, style url, and the region to download. | +| `progressListener` | `Callback` | `No` | Callback that listens for status events while downloading the offline resource. | +| `errorListener` | `Callback` | `No` | Callback that listens for status events while downloading the offline resource. | + + + +```javascript +const progressListener = (offlineRegion, status) => console.log(offlineRegion, status); +const errorListener = (offlineRegion, err) => console.log(offlineRegion, err); + +await MapLibreGL.offlineManager.createPack({ + name: 'offlinePack', + styleURL: 'mapbox://...', + minZoom: 14, + maxZoom: 20, + bounds: [[neLng, neLat], [swLng, swLat]] +}, progressListener, errorListener) +``` + + +#### invalidatePack(name) + +Invalidates the specified offline pack. This method checks that the tiles in the specified offline pack match those from the server. Local tiles that do not match the latest version on the server are updated.This is more efficient than deleting the offline pack and downloading it again. If the data stored locally matches that on the server, new data will not be downloaded. + +##### arguments +| Name | Type | Required | Description | +| ---- | :--: | :------: | :----------: | +| `name` | `String` | `Yes` | Name of the offline pack. | + + + +```javascript +await MapLibreGL.offlineManager.invalidatePack('packName') +``` + + +#### deletePack(name) + +Unregisters the given offline pack and allows resources that are no longer required by any remaining packs to be potentially freed. + +##### arguments +| Name | Type | Required | Description | +| ---- | :--: | :------: | :----------: | +| `name` | `String` | `Yes` | Name of the offline pack. | + + + +```javascript +await MapLibreGL.offlineManager.deletePack('packName') +``` + + +#### invalidateAmbientCache() + +Forces a revalidation of the tiles in the ambient cache and downloads a fresh version of the tiles from the tile server.
This is the recommend method for clearing the cache.
This is the most efficient method because tiles in the ambient cache are re-downloaded to remove outdated data from a device.
It does not erase resources from the ambient cache or delete the database, which can be computationally expensive operations that may carry unintended side effects. + +##### arguments +| Name | Type | Required | Description | +| ---- | :--: | :------: | :----------: | + + + + +```javascript +await MapLibreGL.offlineManager.invalidateAmbientCache(); +``` + + +#### clearAmbientCache() + +Erases resources from the ambient cache.
This method clears the cache and decreases the amount of space that map resources take up on the device. + +##### arguments +| Name | Type | Required | Description | +| ---- | :--: | :------: | :----------: | + + + + +```javascript +await MapLibreGL.offlineManager.clearAmbientCache(); +``` + + +#### setMaximumAmbientCacheSize(size) + +Sets the maximum size of the ambient cache in bytes. Disables the ambient cache if set to 0.
This method may be computationally expensive because it will erase resources from the ambient cache if its size is decreased. + +##### arguments +| Name | Type | Required | Description | +| ---- | :--: | :------: | :----------: | +| `size` | `Number` | `Yes` | Size of ambient cache. | + + + +```javascript +await MapLibreGL.offlineManager.setMaximumAmbientCacheSize(5000000); +``` + + +#### resetDatabase() + +Deletes the existing database, which includes both the ambient cache and offline packs, then reinitializes it. + +##### arguments +| Name | Type | Required | Description | +| ---- | :--: | :------: | :----------: | + + + + +```javascript +await MapLibreGL.offlineManager.resetDatabase(); +``` + + +#### getPacks() + +Retrieves all the current offline packs that are stored in the database. + +##### arguments +| Name | Type | Required | Description | +| ---- | :--: | :------: | :----------: | + + + + +```javascript +const offlinePacks = await MapLibreGL.offlineManager.getPacks(); +``` + + +#### getPack(name) + +Retrieves an offline pack that is stored in the database by name. + +##### arguments +| Name | Type | Required | Description | +| ---- | :--: | :------: | :----------: | +| `name` | `String` | `Yes` | Name of the offline pack. | + + + +```javascript +const offlinePack = await MapLibreGL.offlineManager.getPack(); +``` + + +#### mergeOfflineRegions(path) + +Sideloads offline db + +##### arguments +| Name | Type | Required | Description | +| ---- | :--: | :------: | :----------: | +| `path` | `String` | `Yes` | Path to offline tile db on file system. | + + + +```javascript +await MapLibreGL.offlineManager.mergeOfflineRegions(path); +``` + + +#### setTileCountLimit(limit) + +Sets the maximum number of tiles that may be downloaded and stored on the current device.
Consult the Terms of Service for your map tile host before changing this value. + +##### arguments +| Name | Type | Required | Description | +| ---- | :--: | :------: | :----------: | +| `limit` | `Number` | `Yes` | Map tile limit count. | + + + +```javascript +MapLibreGL.offlineManager.setTileCountLimit(1000); +``` + + +#### setProgressEventThrottle(throttleValue) + +Sets the period at which download status events will be sent over the React Native bridge.
The default is 500ms. + +##### arguments +| Name | Type | Required | Description | +| ---- | :--: | :------: | :----------: | +| `throttleValue` | `Number` | `Yes` | event throttle value in ms. | + + + +```javascript +MapLibreGL.offlineManager.setProgressEventThrottle(500); +``` + + +#### subscribe(packName, progressListener, errorListener) + +Subscribe to download status/error events for the requested offline pack.
Note that createPack calls this internally if listeners are provided. + +##### arguments +| Name | Type | Required | Description | +| ---- | :--: | :------: | :----------: | +| `packName` | `String` | `Yes` | Name of the offline pack. | +| `progressListener` | `Callback` | `Yes` | Callback that listens for status events while downloading the offline resource. | +| `errorListener` | `Callback` | `Yes` | Callback that listens for status events while downloading the offline resource. | + + + +```javascript +const progressListener = (offlinePack, status) => console.log(offlinePack, status) +const errorListener = (offlinePack, err) => console.log(offlinePack, err) +MapLibreGL.offlineManager.subscribe('packName', progressListener, errorListener) +``` + + +#### unsubscribe(packName) + +Unsubscribes any listeners associated with the offline pack.
It's a good idea to call this on componentWillUnmount. + +##### arguments +| Name | Type | Required | Description | +| ---- | :--: | :------: | :----------: | +| `packName` | `String` | `Yes` | Name of the offline pack. | + + + +```javascript +MapLibreGL.offlineManager.unsubscribe('packName') +``` + + + diff --git a/javascript/components/Camera.tsx b/javascript/components/Camera.tsx index 4d327e928..5f90a9a17 100644 --- a/javascript/components/Camera.tsx +++ b/javascript/components/Camera.tsx @@ -427,15 +427,15 @@ class Camera extends React.Component { * * @param {Array} northEastCoordinates - North east coordinate of bound * @param {Array} southWestCoordinates - South west coordinate of bound - * @param {Number=} padding - Camera padding for bound + * @param {Number|Array=} padding - Padding for the bounds * @param {Number=} animationDuration - Duration of camera animation * @return {void} */ fitBounds( northEastCoordinates: number[], southWestCoordinates: number[], - padding = 0, - animationDuration = 0.0, + padding: number | number[] | null | undefined = 0, + animationDuration: number | null | undefined = 0.0, ): void { const pad = { paddingLeft: 0,