@mapsindoors/react-native-maps-indoors-mapbox 2.9.1 → 2.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/Package.swift +87 -0
  2. package/README.md +82 -21
  3. package/android/build.gradle +2 -14
  4. package/android/src/main/java/com/mapsindoorsrn/MapboxMapView.java +4 -0
  5. package/android/src/main/java/com/mapsindoorsrn/MapsIndoorsPackage.java +10 -1
  6. package/android/src/main/java/com/mapsindoorsrn/MapsIndoorsViewManager.java +28 -2
  7. package/android/src/main/java/com/mapsindoorsrn/core/DirectionsRendererModule.java +10 -2
  8. package/android/src/main/java/com/mapsindoorsrn/core/DirectionsServiceModule.java +1 -0
  9. package/android/src/main/java/com/mapsindoorsrn/core/MPDisplayRuleModule.java +24 -0
  10. package/android/src/main/java/com/mapsindoorsrn/core/MapControlModule.java +335 -99
  11. package/android/src/main/java/com/mapsindoorsrn/core/MapsIndoorsModule.java +17 -1
  12. package/android/src/main/java/com/mapsindoorsrn/core/RouteStore.java +59 -0
  13. package/app.plugin.js +8 -86
  14. package/ios/core/DirectionsRendererBridge.m +1 -0
  15. package/ios/core/DirectionsRendererModule.swift +6 -3
  16. package/ios/core/DirectionsServiceModule.swift +7 -1
  17. package/ios/core/MPDisplayRuleBridge.m +9 -0
  18. package/ios/core/MPDisplayRuleModule.swift +18 -0
  19. package/ios/core/MapControlModule.swift +8 -4
  20. package/ios/core/MapsIndoorsBridge.m +1 -0
  21. package/ios/core/MapsIndoorsModule.swift +45 -2
  22. package/ios/core/ReactPositionProvider.swift +6 -7
  23. package/ios/core/Sources/RouteStore/RouteStore.swift +62 -0
  24. package/ios/spm-bridges/include/MapsIndoorsBridges.h +1 -0
  25. package/package.json +11 -3
  26. package/react-native-maps-indoors-mapbox.podspec +8 -11
  27. package/specs/NativeDirectionsRendererModule.ts +1 -1
  28. package/specs/NativeMPDisplayRuleModule.ts +2 -0
  29. package/specs/NativeMapsIndoorsModule.ts +1 -1
  30. package/src/core/MPBuilding.ts +2 -1
  31. package/src/core/MPDirectionsRenderer.ts +7 -1
  32. package/src/core/MPDirectionsService.ts +4 -1
  33. package/src/core/MPDisplayRule.ts +25 -0
  34. package/src/core/MPFloor.ts +25 -0
  35. package/src/core/MPSolutionConfig.ts +42 -0
  36. package/src/core/MapsIndoors.ts +39 -7
  37. package/src/core/RouteHandles.ts +76 -0
@@ -4,7 +4,7 @@ import {TurboModuleRegistry} from 'react-native';
4
4
  export interface Spec extends TurboModule {
5
5
  addListener(eventName: string): void;
6
6
  removeListeners(count: number): void;
7
- loadMapsIndoors(apiKey: string, optionalStrings: Array<string> | null): Promise<null>;
7
+ loadMapsIndoors(apiKey: string, optionalStrings: Array<string> | null, sdkVersion: string | null): Promise<null>;
8
8
  getDefaultVenue(): Promise<string | null>;
9
9
  getVenues(): Promise<string>;
10
10
  getBuildings(): Promise<string>;
@@ -247,7 +247,8 @@ export default class MPBuilding extends MPEntity {
247
247
  "anchor": this.anchor.toJSON(),
248
248
  "buildingInfo": this.buildingInfo,
249
249
  "geometry": this.geometry,
250
- "floors": this._floors,
250
+ // JSON.stringify writes a Map as {}. Keys become strings, as the native SDKs send them.
251
+ "floors": Object.fromEntries(this._floors),
251
252
  "defaultFloor": this.defaultFloor,
252
253
  "address": this.address,
253
254
  "boundingBox": this.bounds.toJSON()
@@ -1,6 +1,7 @@
1
1
  import { EmitterSubscription, NativeEventEmitter, NativeModules } from 'react-native';
2
2
  import { MPCameraViewFitMode, MPDirectionsRendererOptions, MPError, MPRoute, OnLegSelectedListener } from "../../index";
3
3
  import { EventNames } from './EventNames';
4
+ import { nativeRouteId } from './RouteHandles';
4
5
  import { RouteStopIconConfig } from './RouteStopIconConfig';
5
6
 
6
7
  const { DirectionsRenderer } = NativeModules;
@@ -104,6 +105,10 @@ export default class MPDirectionsRenderer {
104
105
  /**
105
106
  * Set a route to be rendered. This also resets the selected leg and step indices to 0.
106
107
  *
108
+ * Directions analytics attribute the route to its origin and destination only when it has the
109
+ * content of a route {@link MPDirectionsService.getRoute} returned since MapsIndoors last loaded,
110
+ * among its ten most recently used. A copy made through JSON qualifies; an edited route does not.
111
+ *
107
112
  * @public
108
113
  * @async
109
114
  * @param {?MPRoute} [route]
@@ -118,7 +123,8 @@ export default class MPDirectionsRenderer {
118
123
  iconsAsString.set(index, encodeStopIcon(icon));
119
124
  });
120
125
  }
121
- await DirectionsRenderer.setRoute(JSON.stringify(route), JSON.stringify(Object.fromEntries(iconsAsString.entries())), legIndex);
126
+ const routeJson = JSON.stringify(route);
127
+ await DirectionsRenderer.setRoute(routeJson, JSON.stringify(Object.fromEntries(iconsAsString.entries())), legIndex, nativeRouteId(routeJson));
122
128
  }
123
129
 
124
130
  /**
@@ -1,5 +1,6 @@
1
1
  import { NativeModules } from 'react-native';
2
2
  import { MPError, MPPoint, MPRoute, MPHighway } from "../../index";
3
+ import { attachNativeRoute } from './RouteHandles';
3
4
 
4
5
  const { DirectionsService } = NativeModules;
5
6
 
@@ -114,7 +115,9 @@ export default class MPDirectionsService {
114
115
  res = await DirectionsService.getRoute(JSON.stringify(origin), JSON.stringify(destination), stopsAsJson, optimze, this.id);
115
116
  }
116
117
  if (res.route !== undefined) {
117
- return Promise.resolve(MPRoute.create(JSON.parse(res.route)));
118
+ const route = MPRoute.create(JSON.parse(res.route));
119
+ attachNativeRoute(route, res.routeId);
120
+ return Promise.resolve(route);
118
121
  } else {
119
122
  return Promise.reject(MPError.create(JSON.parse(res.error)));
120
123
  }
@@ -1103,6 +1103,31 @@ export default class MPDisplayRule {
1103
1103
  });
1104
1104
  }
1105
1105
 
1106
+ /**
1107
+ * Get the factor the icon grows to across the solution's icon zoom range. Defaults to 1.
1108
+ * @returns Promise<number>
1109
+ */
1110
+ public async getIconZoomFactor(): Promise<number> {
1111
+ return DisplayRule.getIconZoomFactor(this.id).catch((err: Error) => {
1112
+ return Promise.reject(MPError.create(JSON.parse(err.message)));
1113
+ });
1114
+ }
1115
+
1116
+ /**
1117
+ * Set the factor the icon grows to across the solution's icon zoom range.
1118
+ * It must be finite and within 0 < factor <= 4; any other value is ignored with a warning.
1119
+ * @returns Promise<void>
1120
+ */
1121
+ public async setIconZoomFactor(factor: number): Promise<void> {
1122
+ if (!Number.isFinite(factor) || factor <= 0 || factor > 4) {
1123
+ console.warn(`MapsIndoors: ignoring iconZoomFactor ${factor}, must be finite and within 0 < factor <= 4`);
1124
+ return;
1125
+ }
1126
+ return DisplayRule.setIconZoomFactor(this.id, factor).catch((err: Error) => {
1127
+ return Promise.reject(MPError.create(JSON.parse(err.message)));
1128
+ });
1129
+ }
1130
+
1106
1131
  /**
1107
1132
  * Get the icon's anchor value.
1108
1133
  * @returns Promise<MPIconAnchor>
@@ -119,6 +119,31 @@ export default class MPFloor extends MPEntity {
119
119
  return this.geometry!.bounds.center;
120
120
  }
121
121
 
122
+ /**
123
+ * Writes the floor in the shape the native SDKs send it.
124
+ *
125
+ * Omits `geometry` when it has no coordinates: iOS sends none for a floor whose backend geometry
126
+ * is a single Polygon (SPEX-1803), and decodes a present `geometry` strictly, so sending an empty
127
+ * one would fail the whole building. `entityBounds` carries the extent instead, as native sends it.
128
+ *
129
+ * @public
130
+ * @returns {object}
131
+ */
132
+ public toJSON(): object {
133
+ const bbox = this.geometry?.bbox;
134
+ const hasBounds = Array.isArray(bbox) && bbox.length === 4 && bbox.every(Number.isFinite);
135
+ return {
136
+ type: this.type,
137
+ id: this.id,
138
+ name: this.name,
139
+ ...(this.geometry?.coordinates ? { geometry: this.geometry } : {}),
140
+ aliases: this.aliases,
141
+ floorIndex: this.floorIndex,
142
+ buildingId: this.buildingId,
143
+ ...(hasBounds ? { entityBounds: this.geometry!.bounds.toJSON() } : {}),
144
+ };
145
+ }
146
+
122
147
  }
123
148
 
124
149
  /**
@@ -26,6 +26,8 @@ export default class MPSolutionConfig {
26
26
  * @param {?MPCollisionHandling} [_mpCollisionHandling]
27
27
  * @param {?boolean} [_isNewSelection]
28
28
  * @param {?number} [_automatedZoomLimit]
29
+ * @param {?number} [_iconScaleZoomFrom]
30
+ * @param {?number} [_iconScaleZoomTo]
29
31
  */
30
32
  private constructor(
31
33
  public readonly id: string,
@@ -35,6 +37,8 @@ export default class MPSolutionConfig {
35
37
  private _mpCollisionHandling?: MPCollisionHandling,
36
38
  private _isNewSelection?: boolean,
37
39
  private _automatedZoomLimit?: number,
40
+ private _iconScaleZoomFrom?: number,
41
+ private _iconScaleZoomTo?: number,
38
42
  ) { }
39
43
 
40
44
 
@@ -55,6 +59,8 @@ export default class MPSolutionConfig {
55
59
  object?.collisionHandling,
56
60
  object?.isNewSelection,
57
61
  object?.automatedZoomLimit,
62
+ object?.iconScaleZoomFrom,
63
+ object?.iconScaleZoomTo,
58
64
  );
59
65
  }
60
66
 
@@ -131,6 +137,28 @@ export default class MPSolutionConfig {
131
137
  public set selectable(selectable: boolean | undefined) {
132
138
  this.locationSettings.selectable = selectable;
133
139
  }
140
+
141
+ /**
142
+ * Get the MapsIndoors zoom where icons start to grow towards their display rule's icon zoom
143
+ * factor, as the solution stores it. Undefined when not set.
144
+ *
145
+ * @public
146
+ * @type {number | undefined}
147
+ */
148
+ public get iconScaleZoomFrom(): number | undefined {
149
+ return this._iconScaleZoomFrom;
150
+ }
151
+
152
+ /**
153
+ * Get the MapsIndoors zoom where icons reach their display rule's icon zoom factor, as the
154
+ * solution stores it. Undefined when not set.
155
+ *
156
+ * @public
157
+ * @type {number | undefined}
158
+ */
159
+ public get iconScaleZoomTo(): number | undefined {
160
+ return this._iconScaleZoomTo;
161
+ }
134
162
  }
135
163
 
136
164
  /**
@@ -185,4 +213,18 @@ export interface MPSolutionConfigParams {
185
213
  * @type {number}
186
214
  */
187
215
  automatedZoomLimit?: number,
216
+
217
+ /**
218
+ * Start of the icon zoom range
219
+ *
220
+ * @type {?number}
221
+ */
222
+ iconScaleZoomFrom?: number,
223
+
224
+ /**
225
+ * End of the icon zoom range
226
+ *
227
+ * @type {?number}
228
+ */
229
+ iconScaleZoomTo?: number,
188
230
  }
@@ -8,9 +8,28 @@ import {
8
8
  import MPBuildingCollection from './MPBuildingCollection';
9
9
  import MPVenueCollection from './MPVenueCollection';
10
10
  import { EventNames } from './EventNames';
11
+ import { forgetNativeRoutes } from './RouteHandles';
11
12
  import { OnBaseMapCacheProgressListener } from './OnBaseMapCacheProgressListener';
12
13
 
13
14
  const { MapsIndoorsModule } = NativeModules;
15
+
16
+ /**
17
+ * The published version of the map package this file is bundled into, read from its own
18
+ * package.json rather than kept as a constant so it cannot drift from the released version.
19
+ *
20
+ * It is sent on {@link MapsIndoors.load} so the native side can name this wrapper as the component
21
+ * behind the MapsIndoors Insights session. The SDK snapshots the component when the session starts,
22
+ * inside the native load, so the version has to travel on the load call itself.
23
+ */
24
+ const sdkVersion: string = (() => {
25
+ try {
26
+ // Resolved relative to this file inside the installed package: <package root>/package.json.
27
+ return require('../../package.json').version ?? 'unknown';
28
+ } catch (e) {
29
+ return 'unknown';
30
+ }
31
+ })();
32
+
14
33
  /**
15
34
  * Main class for accessing data in the MapsIndoors SDK.
16
35
  *
@@ -38,7 +57,9 @@ export default class MapsIndoors {
38
57
  * @returns {Promise<void>} If the load fails for any reason, it will reject with a {@link MPError}.
39
58
  */
40
59
  public static async load(apiKey: String, venues: String[] = null): Promise<void> {
41
- return MapsIndoorsModule.loadMapsIndoors(apiKey, venues).catch((err: Error) =>
60
+ // Native forgets its kept routes on load; keep the ids in step.
61
+ forgetNativeRoutes();
62
+ return MapsIndoorsModule.loadMapsIndoors(apiKey, venues, sdkVersion).catch((err: Error) =>
42
63
  Promise.reject(MPError.parse(err))
43
64
  );
44
65
  }
@@ -267,6 +288,12 @@ export default class MapsIndoors {
267
288
  /**
268
289
  * Run a query on all available locations with a query and filter.
269
290
  *
291
+ * Selecting one of the returned locations afterwards is reported to MapsIndoors Insights as a
292
+ * search-driven selection (`location_searched`) alongside the plain selection event, but only when
293
+ * the {@link query} carries search text: a call without it, such as a filter-only lookup, marks
294
+ * nothing and also clears the marks left by the previous text search. A location the app found by
295
+ * searching its own data outside this call is never reported as search-driven.
296
+ *
270
297
  * @public
271
298
  * @static
272
299
  * @async
@@ -372,14 +399,19 @@ export default class MapsIndoors {
372
399
  if (positonProvider) {
373
400
  const positionUpdateListener = (position: MPPositionResultInterface) => {
374
401
  if (Platform.OS === 'ios') {
402
+ // The coordinate is [lng, lat] only. floorIndex is optional, and iOS decodes the
403
+ // coordinate as numbers, so an undefined floor here becomes null in the JSON and
404
+ // every update is rejected (MS-3929). A missing floorIndex key defaults to 0 natively.
375
405
  let obj = {
376
- coordinate: [position.point.longitude, position.point.latitude, position.floorIndex],
406
+ coordinate: [position.point.longitude, position.point.latitude],
377
407
  floorIndex: position.floorIndex,
378
408
  bearing: position.bearing,
379
409
  accuracy: position.accuracy,
380
410
  providerName: position.positionProvider
381
411
  }
382
- MapsIndoorsModule.onPositionUpdate(JSON.stringify(obj));
412
+ // Nothing awaits this, so a rejected update would otherwise vanish without a trace.
413
+ MapsIndoorsModule.onPositionUpdate(JSON.stringify(obj))
414
+ .catch((err: any) => console.warn('MapsIndoors: position update rejected', err?.message ?? err));
383
415
  }else {
384
416
  MapsIndoorsModule.onPositionUpdate(JSON.stringify(position));
385
417
  }
@@ -435,6 +467,7 @@ export default class MapsIndoors {
435
467
  * @returns {Promise<void>}
436
468
  */
437
469
  public static async destroy(): Promise<void> {
470
+ forgetNativeRoutes();
438
471
  return MapsIndoorsModule.destroy().then(() => { });
439
472
  }
440
473
 
@@ -667,10 +700,9 @@ export default class MapsIndoors {
667
700
  * This runs independently of {@link synchronizeContent} and {@link cacheData}, which cache the
668
701
  * MapsIndoors content itself. Both are needed for a fully offline map.
669
702
  *
670
- * On iOS, a {@link MapView} has to have been mounted at least once first: the iOS SDK registers
671
- * its base-map cache when it builds the map provider, and until then this rejects with
672
- * {@link MPError.baseMapCachingNotSupported} even on Mapbox. Android has no such ordering
673
- * requirement.
703
+ * Works before any {@link MapView} exists, so tiles can be cached ahead of the first map. On
704
+ * iOS, until `MapControl.create` has run this caches the MapsIndoors Mapbox style, so an app
705
+ * rendering its own style should call `MapControl.create` before syncing.
674
706
  *
675
707
  * Run one call at a time. Progress arrives on a single channel carrying only the fraction, with
676
708
  * nothing identifying the call, so two overlapping calls interleave their progress - including
@@ -0,0 +1,76 @@
1
+ import MPRoute from './MPRoute';
2
+
3
+ /**
4
+ * Links each {@link MPRoute} that {@link MPDirectionsService.getRoute} returns to the native route it
5
+ * was encoded from, so {@link MPDirectionsRenderer.setRoute} can render that native instance instead
6
+ * of decoding a copy (SPEX-2782).
7
+ *
8
+ * Only the native instance knows the route's resolved endpoints, which the SDK reports with the
9
+ * directions Insights events. Neither SDK serialises them, so every decoded copy reports none.
10
+ * Rendering one native instance repeatedly is safe: its endpoints are resolved once and read on every
11
+ * event, never consumed.
12
+ *
13
+ * Keyed by the route's JSON rather than by object, so a route that went through JSON on the app's
14
+ * side, such as a state store or navigation params, still finds its native route. An edited route has
15
+ * different JSON and finds none, so it reports no endpoints rather than ones it may no longer have.
16
+ */
17
+
18
+ /** Matches the native stores, which forget the least recently used route past this many. */
19
+ const CAPACITY = 10;
20
+
21
+ /** Native route ids by route JSON, least recently used first. */
22
+ const ids = new Map<string, string>();
23
+
24
+ let warned = false;
25
+
26
+ /** Records the native route behind a route that {@link MPDirectionsService} just created. */
27
+ export function attachNativeRoute(route: MPRoute, id: string | undefined): void {
28
+ if (!id) {
29
+ return;
30
+ }
31
+ const json = JSON.stringify(route);
32
+ ids.delete(json);
33
+ ids.set(json, id);
34
+ if (ids.size > CAPACITY) {
35
+ ids.delete(ids.keys().next().value!);
36
+ }
37
+ }
38
+
39
+ /**
40
+ * The id of the native route to render in place of `json`, or null to decode `json`.
41
+ *
42
+ * @param json The route as JSON, which the caller sends across the bridge anyway.
43
+ */
44
+ export function nativeRouteId(json: string): string | null {
45
+ const id = ids.get(json);
46
+ if (id === undefined) {
47
+ warnOnce();
48
+ return null;
49
+ }
50
+ ids.delete(json);
51
+ ids.set(json, id);
52
+ return id;
53
+ }
54
+
55
+ /**
56
+ * Forgets every route, as the native stores do when MapsIndoors loads or is destroyed: the routes
57
+ * belong to the solution that was loaded when they were requested.
58
+ */
59
+ export function forgetNativeRoutes(): void {
60
+ ids.clear();
61
+ }
62
+
63
+ /**
64
+ * The fallback is legitimate for a route the app built, but it is also how these endpoints went
65
+ * missing unnoticed before, so development builds say so once.
66
+ */
67
+ function warnOnce(): void {
68
+ if (warned || !(globalThis as { __DEV__?: boolean }).__DEV__) {
69
+ return;
70
+ }
71
+ warned = true;
72
+ console.warn(
73
+ 'MapsIndoors: setRoute was given a route MPDirectionsService.getRoute did not return, or one changed ' +
74
+ 'since. It renders, but directions analytics will not attribute its origin and destination.'
75
+ );
76
+ }