@ranamaps/maps-react-native 0.0.0-stage → 0.4.0-beta.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.
- package/BETA-STATUS.md +13 -0
- package/CHANGELOG.md +40 -0
- package/LICENSE.md +13 -0
- package/NOTICES.md +230 -0
- package/README.md +61 -2
- package/RanaMapsReactNative.podspec +17 -0
- package/android/build.gradle +25 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/com/ranamaps/reactnative/RanaMapManager.kt +115 -0
- package/android/src/main/java/com/ranamaps/reactnative/RanaMapsModule.kt +41 -0
- package/android/src/main/java/com/ranamaps/reactnative/RanaMapsPackage.kt +11 -0
- package/app.plugin.js +127 -0
- package/ios/RanaMapViewComponentView.mm +116 -0
- package/ios/RanaMapsModule.mm +32 -0
- package/ios/RanaNativeRuntime.swift +42 -0
- package/lib/bridge.d.ts +40 -0
- package/lib/bridge.js +152 -0
- package/lib/index.d.ts +31 -0
- package/lib/index.js +113 -0
- package/lib/map-lifecycle.d.ts +5 -0
- package/lib/map-lifecycle.js +10 -0
- package/lib/models.d.ts +509 -0
- package/lib/models.js +2 -0
- package/lib/sdk.d.ts +309 -0
- package/lib/sdk.js +588 -0
- package/lib/specs/NativeRanaMaps.d.ts +8 -0
- package/lib/specs/NativeRanaMaps.js +4 -0
- package/lib/specs/RanaMapViewNativeComponent.d.ts +13 -0
- package/lib/specs/RanaMapViewNativeComponent.js +4 -0
- package/lib/specs/RanaMarkerViewNativeComponent.d.ts +9 -0
- package/lib/specs/RanaMarkerViewNativeComponent.js +4 -0
- package/package.json +59 -4
- package/react-native.config.js +12 -0
- package/src/bridge.ts +184 -0
- package/src/index.tsx +168 -0
- package/src/map-lifecycle.ts +11 -0
- package/src/models.ts +510 -0
- package/src/sdk.ts +732 -0
- package/src/specs/NativeRanaMaps.ts +16 -0
- package/src/specs/RanaMapViewNativeComponent.ts +10 -0
- package/src/specs/RanaMarkerViewNativeComponent.ts +9 -0
package/src/sdk.ts
ADDED
|
@@ -0,0 +1,732 @@
|
|
|
1
|
+
import { RanaRuntime, type RanaEvent } from "./bridge";
|
|
2
|
+
import type * as M from "./models";
|
|
3
|
+
export type * from "./models";
|
|
4
|
+
export { RanaError } from "./bridge";
|
|
5
|
+
export interface HttpRequest {
|
|
6
|
+
url: string;
|
|
7
|
+
method: string;
|
|
8
|
+
headers: Readonly<Record<string, string>>;
|
|
9
|
+
body: string | null;
|
|
10
|
+
}
|
|
11
|
+
export interface HttpAnswer {
|
|
12
|
+
status: number;
|
|
13
|
+
headers: Readonly<Record<string, string>>;
|
|
14
|
+
body: string;
|
|
15
|
+
}
|
|
16
|
+
export type HttpTransport = (
|
|
17
|
+
request: HttpRequest,
|
|
18
|
+
signal: AbortSignal,
|
|
19
|
+
) => Promise<HttpAnswer>;
|
|
20
|
+
export type ReroutingProvider = (
|
|
21
|
+
request: M.ReroutingRequest,
|
|
22
|
+
signal: AbortSignal,
|
|
23
|
+
) => Promise<M.ReroutingResult>;
|
|
24
|
+
export type JourneyTraceUploader = (
|
|
25
|
+
batch: M.JourneyTraceBatch,
|
|
26
|
+
signal: AbortSignal,
|
|
27
|
+
) => Promise<M.JourneyTraceReceipt>;
|
|
28
|
+
export interface LocationProvider {
|
|
29
|
+
start(
|
|
30
|
+
onLocation: (fix: M.LocationFix) => void,
|
|
31
|
+
onError: (error: M.ErrorInfo) => void,
|
|
32
|
+
): void;
|
|
33
|
+
stop(): void;
|
|
34
|
+
startHeading?(onHeading: (fix: M.HeadingFix) => void): void;
|
|
35
|
+
stopHeading?(): void;
|
|
36
|
+
}
|
|
37
|
+
export type StyleExpression =
|
|
38
|
+
| { kind: "number"; value: number }
|
|
39
|
+
| { kind: "text" | "color"; value: string }
|
|
40
|
+
| { kind: "get"; property: string }
|
|
41
|
+
| {
|
|
42
|
+
kind: "match";
|
|
43
|
+
property: string;
|
|
44
|
+
cases: Readonly<Record<string, StyleExpression>>;
|
|
45
|
+
fallback: StyleExpression;
|
|
46
|
+
}
|
|
47
|
+
| { kind: "zoom"; stops: ReadonlyArray<{ zoom: number; value: number }> };
|
|
48
|
+
export interface FeatureFilter {
|
|
49
|
+
property: string;
|
|
50
|
+
equals: string;
|
|
51
|
+
}
|
|
52
|
+
export interface DataLayerOptions {
|
|
53
|
+
kind: M.DataLayerKind;
|
|
54
|
+
color?: StyleExpression;
|
|
55
|
+
opacity?: StyleExpression;
|
|
56
|
+
width?: StyleExpression;
|
|
57
|
+
radius?: StyleExpression;
|
|
58
|
+
text?: StyleExpression;
|
|
59
|
+
textSize?: StyleExpression;
|
|
60
|
+
filter?: FeatureFilter;
|
|
61
|
+
visible?: boolean;
|
|
62
|
+
belowLayerId?: string;
|
|
63
|
+
}
|
|
64
|
+
export class RanaResource {
|
|
65
|
+
readonly cleanup: Array<() => void> = [];
|
|
66
|
+
constructor(
|
|
67
|
+
readonly runtime: RanaRuntime,
|
|
68
|
+
readonly handle: string,
|
|
69
|
+
) {}
|
|
70
|
+
onEvent(listener: (event: RanaEvent) => void) {
|
|
71
|
+
const off = this.runtime.on(this.handle, listener);
|
|
72
|
+
this.cleanup.push(off);
|
|
73
|
+
return off;
|
|
74
|
+
}
|
|
75
|
+
protected call<T>(method: string, args: unknown = {}, signal?: AbortSignal) {
|
|
76
|
+
return this.runtime.call<T>(this.handle, method, args, signal);
|
|
77
|
+
}
|
|
78
|
+
async dispose() {
|
|
79
|
+
try {
|
|
80
|
+
await this.call("dispose");
|
|
81
|
+
} finally {
|
|
82
|
+
this.runtime.forgetHandle(this.handle);
|
|
83
|
+
this.cleanup.splice(0).forEach((fn) => fn());
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
export class RanaOverlay extends RanaResource {
|
|
88
|
+
async remove() {
|
|
89
|
+
await this.call("remove");
|
|
90
|
+
this.cleanup.splice(0).forEach((fn) => fn());
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* What a marker shows: one member per Rana Kit marker family, plus your own image. Kit families are drawn from
|
|
95
|
+
* Rana Kit in any colour (see MarkerStyle); "image" keeps your own artwork. For an interactive React element,
|
|
96
|
+
* render a <Marker> child of the map instead.
|
|
97
|
+
*/
|
|
98
|
+
export type MarkerContent =
|
|
99
|
+
/** `label` (up to 3 characters: a stop letter, a number) replaces the glyph. */
|
|
100
|
+
/** `drop: true` is the kit's "pick a spot" pin: a white head ringed in the colour, lifted off the ground. */
|
|
101
|
+
| { kind: "pin"; glyph?: M.PinGlyph; label?: string; size?: M.MarkerSize; drop?: boolean }
|
|
102
|
+
| { kind: "badge"; text: string; icon?: M.PinGlyph }
|
|
103
|
+
/** `live: true` adds the kit's green presence dot. */
|
|
104
|
+
| { kind: "avatar"; name: string; live?: boolean }
|
|
105
|
+
| { kind: "dot" }
|
|
106
|
+
/** A pin whose head is your photo (base64 PNG or JPEG), clipped to the kit's head shape. */
|
|
107
|
+
| { kind: "photo"; imageBase64: string }
|
|
108
|
+
/** A route stop: the start ring, a lettered stop or the destination flag. Night unless the style gives a colour. */
|
|
109
|
+
| { kind: "stop"; role: M.RouteStopRole; label?: string }
|
|
110
|
+
/** Base64 PNG or JPEG bytes. */
|
|
111
|
+
| { kind: "image"; imageBase64: string; anchor?: M.MarkerAnchor; size?: M.MarkerSize };
|
|
112
|
+
|
|
113
|
+
/** One callout button. */
|
|
114
|
+
export interface CalloutAction { label: string; onPress: () => void }
|
|
115
|
+
/**
|
|
116
|
+
* The Rana Kit callout: a name, one line of facts and up to two actions, opened above a marker when it is tapped.
|
|
117
|
+
* Its layout is fixed by the kit (260 wide, 48 dp buttons) and follows the map's day or night theme.
|
|
118
|
+
*/
|
|
119
|
+
export interface Callout { title: string; detail?: string; primary?: CalloutAction; secondary?: CalloutAction }
|
|
120
|
+
|
|
121
|
+
/** Builders for each marker family, e.g. `addMarker(p, MarkerContent.badge("₦2,500"))`. */
|
|
122
|
+
export const MarkerContent = {
|
|
123
|
+
pin: (glyph: M.PinGlyph = "dot", size: M.MarkerSize = "m"): MarkerContent => ({ kind: "pin", glyph, size }),
|
|
124
|
+
labelledPin: (label: string, size: M.MarkerSize = "m"): MarkerContent => ({ kind: "pin", label, size }),
|
|
125
|
+
badge: (text: string, icon?: M.PinGlyph): MarkerContent => (icon ? { kind: "badge", text, icon } : { kind: "badge", text }),
|
|
126
|
+
/** The "pick a spot" pin shown while a user chooses a place. */
|
|
127
|
+
dropPin: (glyph: M.PinGlyph = "dot"): MarkerContent => ({ kind: "pin", glyph, size: "m", drop: true }),
|
|
128
|
+
avatar: (name: string, live = false): MarkerContent => (live ? { kind: "avatar", name, live } : { kind: "avatar", name }),
|
|
129
|
+
dot: (): MarkerContent => ({ kind: "dot" }),
|
|
130
|
+
photo: (imageBase64: string): MarkerContent => ({ kind: "photo", imageBase64 }),
|
|
131
|
+
routeStart: (): MarkerContent => ({ kind: "stop", role: "start" }),
|
|
132
|
+
routeStop: (label?: string): MarkerContent => (label === undefined ? { kind: "stop", role: "waypoint" } : { kind: "stop", role: "waypoint", label }),
|
|
133
|
+
routeDestination: (): MarkerContent => ({ kind: "stop", role: "destination" }),
|
|
134
|
+
image: (imageBase64: string, anchor: M.MarkerAnchor = "bottom", size: M.MarkerSize = "m"): MarkerContent =>
|
|
135
|
+
({ kind: "image", imageBase64, anchor, size }),
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/** The bridge form of marker content (MarkerContentWire in cross-platform/models.json). */
|
|
139
|
+
function contentWire(content: MarkerContent): M.MarkerContentWire {
|
|
140
|
+
switch (content.kind) {
|
|
141
|
+
case "pin": case "badge": case "avatar": case "dot": case "image": case "photo": case "stop": return { ...content };
|
|
142
|
+
default: throw new TypeError(`Unknown marker content: ${(content as { kind?: unknown }).kind}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export class RanaMarker extends RanaOverlay {
|
|
147
|
+
setPosition(point: M.LatLng) {
|
|
148
|
+
return this.call<void>("setPosition", { point });
|
|
149
|
+
}
|
|
150
|
+
/** Replaces what the marker shows, keeping its position, style and taps (a live price keeps its marker). */
|
|
151
|
+
setContent(content: MarkerContent) {
|
|
152
|
+
return this.call<void>("setContent", { content: contentWire(content) });
|
|
153
|
+
}
|
|
154
|
+
/** Restyles the marker. Fields left out take their defaults. */
|
|
155
|
+
setStyle(style: M.MarkerStyle) {
|
|
156
|
+
return this.call<void>("setStyle", { style });
|
|
157
|
+
}
|
|
158
|
+
setVisible(visible: boolean) {
|
|
159
|
+
return this.call<void>("setVisible", { visible });
|
|
160
|
+
}
|
|
161
|
+
/** Calls `listener` on each tap on the marker. Returns a function that stops listening. */
|
|
162
|
+
onTap(listener: () => void) {
|
|
163
|
+
return this.onEvent((event) => { if (event.type === "tap") listener(); });
|
|
164
|
+
}
|
|
165
|
+
private callout?: Callout | null;
|
|
166
|
+
private calloutPresses?: () => void;
|
|
167
|
+
/** Gives the marker a callout that opens when the marker is tapped; null removes it. */
|
|
168
|
+
setCallout(callout: Callout | null) {
|
|
169
|
+
if (callout && !callout.title?.trim()) throw new TypeError("A callout needs a title");
|
|
170
|
+
this.callout = callout;
|
|
171
|
+
this.calloutPresses ??= this.onEvent((event) => {
|
|
172
|
+
if (event.type !== "callout") return;
|
|
173
|
+
const action = (event.value as { action?: string } | null)?.action;
|
|
174
|
+
(action === "primary" ? this.callout?.primary : action === "secondary" ? this.callout?.secondary : undefined)?.onPress();
|
|
175
|
+
});
|
|
176
|
+
const wire: M.CalloutWire | null = callout
|
|
177
|
+
? { title: callout.title, detail: callout.detail, primary: callout.primary?.label, secondary: callout.secondary?.label }
|
|
178
|
+
: null;
|
|
179
|
+
return this.call<void>("setCallout", { callout: wire });
|
|
180
|
+
}
|
|
181
|
+
/** Opens the marker's callout now (one callout is open per map at a time). */
|
|
182
|
+
showCallout() {
|
|
183
|
+
return this.call<void>("showCallout");
|
|
184
|
+
}
|
|
185
|
+
hideCallout() {
|
|
186
|
+
return this.call<void>("hideCallout");
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
export class RanaPolyline extends RanaOverlay {
|
|
190
|
+
setProgress(progress: number) {
|
|
191
|
+
return this.call<void>("setProgress", { progress });
|
|
192
|
+
}
|
|
193
|
+
/** Restyles the line in place (a tapped alternative becomes the chosen route). */
|
|
194
|
+
setOptions(options: M.PolylineOptions) {
|
|
195
|
+
return this.call<void>("setOptions", { options });
|
|
196
|
+
}
|
|
197
|
+
private tappable?: Promise<void>;
|
|
198
|
+
/** Calls `listener` with the point of each tap on the line, with a generous touch area. */
|
|
199
|
+
onTap(listener: (point: M.LatLng) => void) {
|
|
200
|
+
this.tappable ??= this.call<void>("setTappable", { tappable: true });
|
|
201
|
+
return this.onEvent((event) => { if (event.type === "tap") listener(event.value as M.LatLng); });
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* What a vehicle looks like: a Rana Kit vehicle in any colour, or your own artwork (base64 PNG or JPEG). Images and
|
|
206
|
+
* frames should face north (up) at heading 0; they turn with the heading unless `rotates` is false.
|
|
207
|
+
*/
|
|
208
|
+
export const VehicleAppearance = {
|
|
209
|
+
kit: (vehicle: M.VehicleKind, options: { color?: string; base?: M.VehicleBase } = {}): M.VehicleAppearanceWire => ({ kind: "kit", vehicle, ...options }),
|
|
210
|
+
image: (imageBase64: string, options: { size?: number; rotates?: boolean } = {}): M.VehicleAppearanceWire => ({ kind: "image", imageBase64, ...options }),
|
|
211
|
+
/** Frames played in a loop, one every `frameMs` (default 100). Holds the first frame when animations are off. */
|
|
212
|
+
animated: (framesBase64: string[], options: { frameMs?: number; size?: number; rotates?: boolean } = {}): M.VehicleAppearanceWire => {
|
|
213
|
+
if (framesBase64.length === 0) throw new TypeError("An animated vehicle needs at least one frame");
|
|
214
|
+
return { kind: "animated", framesBase64, ...options };
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
/** How the traveller (your own position) is drawn. The puck is the default. */
|
|
219
|
+
export const TravellerAppearance = {
|
|
220
|
+
/** The Rana Kit puck: an arrow that turns with the heading. Without a heading it shows as the dot. */
|
|
221
|
+
puck: (): M.TravellerAppearanceWire => ({ kind: "puck" }),
|
|
222
|
+
dot: (): M.TravellerAppearanceWire => ({ kind: "dot" }),
|
|
223
|
+
/** Any vehicle appearance: a kit vehicle, your image or animation. It keeps its last heading. */
|
|
224
|
+
vehicle: (vehicle: M.VehicleAppearanceWire): M.TravellerAppearanceWire => ({ kind: "vehicle", vehicle }),
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
export class RanaVehicle extends RanaOverlay {
|
|
228
|
+
update(point: M.LatLng, heading = 0, durationMs = 0) {
|
|
229
|
+
return this.call<void>("update", { point, heading, durationMs });
|
|
230
|
+
}
|
|
231
|
+
setState(state: M.VehicleState) {
|
|
232
|
+
return this.call<void>("setState", { state });
|
|
233
|
+
}
|
|
234
|
+
/** Swaps the look in place (a rider picks a new bike), keeping position, heading, state and taps. */
|
|
235
|
+
setAppearance(appearance: M.VehicleAppearanceWire) {
|
|
236
|
+
return this.call<void>("setAppearance", { appearance });
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
export class RanaGeoJsonSource extends RanaOverlay {
|
|
240
|
+
setData(geoJson: string) {
|
|
241
|
+
return this.call<void>("setData", { geoJson });
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
export class RanaStyleLayer extends RanaOverlay {
|
|
245
|
+
setOptions(options: DataLayerOptions) {
|
|
246
|
+
return this.call<void>("setOptions", options);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
export class JourneyTraceRecorder extends RanaResource {
|
|
250
|
+
record(fix: M.LocationFix) {
|
|
251
|
+
return this.call<void>("record", fix);
|
|
252
|
+
}
|
|
253
|
+
flush(signal?: AbortSignal) {
|
|
254
|
+
return this.call<void>("flush", {}, signal);
|
|
255
|
+
}
|
|
256
|
+
stop() {
|
|
257
|
+
return this.call<void>("stop");
|
|
258
|
+
}
|
|
259
|
+
status() {
|
|
260
|
+
return this.call<M.JourneyTraceStatus>("status");
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
export class RanaClient extends RanaResource {
|
|
264
|
+
static async create(
|
|
265
|
+
runtime: RanaRuntime,
|
|
266
|
+
key?: string,
|
|
267
|
+
transport?: HttpTransport,
|
|
268
|
+
) {
|
|
269
|
+
const cb = transport
|
|
270
|
+
? runtime.registerCallback((_, value, signal) =>
|
|
271
|
+
transport(value as HttpRequest, signal),
|
|
272
|
+
)
|
|
273
|
+
: undefined;
|
|
274
|
+
try {
|
|
275
|
+
const client = new RanaClient(
|
|
276
|
+
runtime,
|
|
277
|
+
await runtime.call<string>("runtime", "createClient", {
|
|
278
|
+
key,
|
|
279
|
+
transportId: cb?.id,
|
|
280
|
+
}),
|
|
281
|
+
);
|
|
282
|
+
if (cb) client.cleanup.push(cb.dispose);
|
|
283
|
+
return client;
|
|
284
|
+
} catch (error) {
|
|
285
|
+
cb?.dispose();
|
|
286
|
+
throw error;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
describeKey(signal?: AbortSignal) {
|
|
290
|
+
return this.call<M.KeyInfo>("describeKey", {}, signal);
|
|
291
|
+
}
|
|
292
|
+
directions(options: M.DirectionsOptions, signal?: AbortSignal) {
|
|
293
|
+
return this.call<M.DirectionsResult>("directions", options, signal);
|
|
294
|
+
}
|
|
295
|
+
matrix(options: M.MatrixOptions, signal?: AbortSignal) {
|
|
296
|
+
return this.call<M.MatrixResult>("matrix", options, signal);
|
|
297
|
+
}
|
|
298
|
+
snapToRoads(options: M.SnapOptions, signal?: AbortSignal) {
|
|
299
|
+
return this.call<M.SnapResult>("snapToRoads", options, signal);
|
|
300
|
+
}
|
|
301
|
+
reverseGeocode(point: M.LatLng, signal?: AbortSignal) {
|
|
302
|
+
return this.call<M.Address>("reverseGeocode", { point }, signal);
|
|
303
|
+
}
|
|
304
|
+
geocode(options: M.SearchOptions, signal?: AbortSignal) {
|
|
305
|
+
return this.call<M.GeocodeResult[]>("geocode", options, signal);
|
|
306
|
+
}
|
|
307
|
+
readonly places = {
|
|
308
|
+
autocomplete: (options: M.SearchOptions, signal?: AbortSignal) =>
|
|
309
|
+
this.call<M.PlaceSuggestion[]>("places.autocomplete", options, signal),
|
|
310
|
+
details: (placeId: string, signal?: AbortSignal) =>
|
|
311
|
+
this.call<M.PlaceDetails>("places.details", { placeId }, signal),
|
|
312
|
+
nearby: (options: M.NearbyOptions, signal?: AbortSignal) =>
|
|
313
|
+
this.call<M.NearbyPlace[]>("places.nearby", options, signal),
|
|
314
|
+
};
|
|
315
|
+
uploadJourneyTrace(batch: M.JourneyTraceBatch, signal?: AbortSignal) {
|
|
316
|
+
return this.call<M.JourneyTraceReceipt>(
|
|
317
|
+
"uploadJourneyTrace",
|
|
318
|
+
batch,
|
|
319
|
+
signal,
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
async createTraceRecorder(
|
|
323
|
+
options: M.JourneyTraceOptions,
|
|
324
|
+
uploader?: JourneyTraceUploader,
|
|
325
|
+
) {
|
|
326
|
+
const cb = uploader
|
|
327
|
+
? this.runtime.registerCallback((_, value, signal) =>
|
|
328
|
+
uploader(value as M.JourneyTraceBatch, signal),
|
|
329
|
+
)
|
|
330
|
+
: undefined;
|
|
331
|
+
try {
|
|
332
|
+
const recorder = new JourneyTraceRecorder(
|
|
333
|
+
this.runtime,
|
|
334
|
+
await this.call<string>("createTraceRecorder", {
|
|
335
|
+
...options,
|
|
336
|
+
uploaderId: cb?.id,
|
|
337
|
+
}),
|
|
338
|
+
);
|
|
339
|
+
if (cb) recorder.cleanup.push(cb.dispose);
|
|
340
|
+
return recorder;
|
|
341
|
+
} catch (error) {
|
|
342
|
+
cb?.dispose();
|
|
343
|
+
throw error;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
export class RanaOfflineRegion extends RanaResource {
|
|
348
|
+
constructor(
|
|
349
|
+
runtime: RanaRuntime,
|
|
350
|
+
handle: string,
|
|
351
|
+
readonly info: M.OfflineRegionInfo,
|
|
352
|
+
) {
|
|
353
|
+
super(runtime, handle);
|
|
354
|
+
}
|
|
355
|
+
progress() {
|
|
356
|
+
return this.call<M.OfflineProgress | null>("progress");
|
|
357
|
+
}
|
|
358
|
+
resume() {
|
|
359
|
+
return this.call<void>("resume");
|
|
360
|
+
}
|
|
361
|
+
pause() {
|
|
362
|
+
return this.call<void>("pause");
|
|
363
|
+
}
|
|
364
|
+
refresh(signal?: AbortSignal) {
|
|
365
|
+
return this.call<void>("refresh", {}, signal);
|
|
366
|
+
}
|
|
367
|
+
async remove(signal?: AbortSignal) {
|
|
368
|
+
await this.call<void>("remove", {}, signal);
|
|
369
|
+
this.cleanup.splice(0).forEach((fn) => fn());
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
export class RanaOfflineManager extends RanaResource {
|
|
373
|
+
static async create(runtime: RanaRuntime, key?: string) {
|
|
374
|
+
return new RanaOfflineManager(
|
|
375
|
+
runtime,
|
|
376
|
+
await runtime.call<string>("runtime", "createOfflineManager", { key }),
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
async regions() {
|
|
380
|
+
const entries =
|
|
381
|
+
await this.call<Array<{ handle: string; info: M.OfflineRegionInfo }>>(
|
|
382
|
+
"regions",
|
|
383
|
+
);
|
|
384
|
+
return entries.map(
|
|
385
|
+
(e) => new RanaOfflineRegion(this.runtime, e.handle, e.info),
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
async createRegion(options: M.OfflineRegionOptions) {
|
|
389
|
+
const e = await this.call<{ handle: string; info: M.OfflineRegionInfo }>(
|
|
390
|
+
"createRegion",
|
|
391
|
+
options,
|
|
392
|
+
);
|
|
393
|
+
return new RanaOfflineRegion(this.runtime, e.handle, e.info);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
export class RanaMapController extends RanaResource {
|
|
397
|
+
private providerCleanup?: () => void;
|
|
398
|
+
private reroutingCleanup?: () => void;
|
|
399
|
+
onMarkerDrag(listener: (event: M.MarkerDragEvent) => void) {
|
|
400
|
+
return this.onEvent((e) => {
|
|
401
|
+
if (e.type === "markerDrag") listener(e.value as M.MarkerDragEvent);
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
onCameraMove(listener: (state: M.CameraState) => void) {
|
|
405
|
+
return this.onEvent((e) => {
|
|
406
|
+
if (e.type === "cameraMove") listener(e.value as M.CameraState);
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
onCameraIdle(listener: (state: M.CameraState) => void) {
|
|
410
|
+
return this.onEvent((e) => {
|
|
411
|
+
if (e.type === "cameraIdle") listener(e.value as M.CameraState);
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
onReroutingChanged(listener: (state: M.ReroutingStatus) => void) {
|
|
415
|
+
return this.onEvent((e) => {
|
|
416
|
+
if (e.type === "reroutingChanged") listener(e.value as M.ReroutingStatus);
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
onRerouted(listener: (result: M.ReroutingResult) => void) {
|
|
420
|
+
return this.onEvent((e) => {
|
|
421
|
+
if (e.type === "rerouted") listener(e.value as M.ReroutingResult);
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
/** Each accepted measured location fix, from the connected provider or `updateLocation`. */
|
|
425
|
+
onLocation(listener: (fix: M.LocationFix) => void) {
|
|
426
|
+
return this.onEvent((e) => {
|
|
427
|
+
if (e.type === "location") listener(e.value as M.LocationFix);
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
onLocationRouteChanged(listener: (status: M.LocationRouteStatus) => void) {
|
|
431
|
+
return this.onEvent((e) => {
|
|
432
|
+
if (e.type === "locationRouteChanged")
|
|
433
|
+
listener(e.value as M.LocationRouteStatus);
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
setControls(controls: M.ControlsOptions) {
|
|
437
|
+
return this.update({ controls });
|
|
438
|
+
}
|
|
439
|
+
setAttribution(attribution: M.AttributionOptions) {
|
|
440
|
+
return this.update({ settings: { attribution } });
|
|
441
|
+
}
|
|
442
|
+
applySettings(settings: M.MapSettings) {
|
|
443
|
+
return this.update({ settings });
|
|
444
|
+
}
|
|
445
|
+
moveTo(center: M.LatLng, zoom?: number, animated = true) {
|
|
446
|
+
return this.setCamera({ center, zoom }, animated);
|
|
447
|
+
}
|
|
448
|
+
update(options: M.MapOptions) {
|
|
449
|
+
return this.call<void>("update", options);
|
|
450
|
+
}
|
|
451
|
+
cameraState() {
|
|
452
|
+
return this.call<M.CameraState>("cameraState");
|
|
453
|
+
}
|
|
454
|
+
setCamera(
|
|
455
|
+
camera: M.CameraOptions,
|
|
456
|
+
animated = true,
|
|
457
|
+
durationMs = 300,
|
|
458
|
+
signal?: AbortSignal,
|
|
459
|
+
) {
|
|
460
|
+
return this.call<boolean>(
|
|
461
|
+
"setCamera",
|
|
462
|
+
{ camera, animated, durationMs },
|
|
463
|
+
signal,
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
fitPoints(points: ReadonlyArray<M.LatLng>, padding = 40) {
|
|
467
|
+
return this.call<void>("fitPoints", { points, padding });
|
|
468
|
+
}
|
|
469
|
+
setStyle(style: M.RanaStyle) {
|
|
470
|
+
return this.call<void>("setStyle", style);
|
|
471
|
+
}
|
|
472
|
+
setPadding(padding: M.MapPadding) {
|
|
473
|
+
return this.call<void>("setPadding", padding);
|
|
474
|
+
}
|
|
475
|
+
setTracking(tracking: M.LocationTracking) {
|
|
476
|
+
return this.call<void>("setTracking", { tracking });
|
|
477
|
+
}
|
|
478
|
+
setLocateState(state: M.LocateState) {
|
|
479
|
+
return this.call<void>("setLocateState", { state });
|
|
480
|
+
}
|
|
481
|
+
project(points: ReadonlyArray<M.LatLng>) {
|
|
482
|
+
return this.call<M.ScreenPoint[]>("project", { points });
|
|
483
|
+
}
|
|
484
|
+
unproject(points: ReadonlyArray<M.ScreenPoint>) {
|
|
485
|
+
return this.call<M.LatLng[]>("unproject", { points });
|
|
486
|
+
}
|
|
487
|
+
setProjectionPoints(points: Readonly<Record<string, M.LatLng>>) {
|
|
488
|
+
return this.call<void>("setProjectionPoints", { points });
|
|
489
|
+
}
|
|
490
|
+
/** Adds a marker: any Rana Kit family (`MarkerContent.pin()`, `.badge()`, `.avatar()`, `.dot()`) or your own image. */
|
|
491
|
+
/** Adds many markers with one redraw and one call across the bridge, returning their handles in the same order. */
|
|
492
|
+
async addMarkers(entries: ReadonlyArray<{ point: M.LatLng; content?: MarkerContent; style?: M.MarkerStyle }>) {
|
|
493
|
+
const handles = await this.call<string[]>("addMarkers", {
|
|
494
|
+
markers: entries.map((e) => ({ point: e.point, content: contentWire(e.content ?? { kind: "pin" }), style: e.style ?? {} })),
|
|
495
|
+
});
|
|
496
|
+
return handles.map((handle) => new RanaMarker(this.runtime, handle));
|
|
497
|
+
}
|
|
498
|
+
async addMarker(point: M.LatLng, content: MarkerContent = { kind: "pin" }, style: M.MarkerStyle = {}) {
|
|
499
|
+
return new RanaMarker(
|
|
500
|
+
this.runtime,
|
|
501
|
+
await this.call<string>("addMarker", { point, content: contentWire(content), style }),
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
async addPoints(
|
|
505
|
+
points: ReadonlyArray<M.LatLng>,
|
|
506
|
+
options: M.ClusterOptions = {},
|
|
507
|
+
) {
|
|
508
|
+
return new RanaOverlay(
|
|
509
|
+
this.runtime,
|
|
510
|
+
await this.call<string>("addPoints", { points, options }),
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
/** A vehicle that glides between updates: a Rana Kit vehicle (default a car) or your own image or animation. */
|
|
514
|
+
async addVehicle(point: M.LatLng, appearance: M.VehicleAppearanceWire = VehicleAppearance.kit("car"), options: M.VehicleOptions = {}) {
|
|
515
|
+
return new RanaVehicle(
|
|
516
|
+
this.runtime,
|
|
517
|
+
await this.call<string>("addVehicle", { point, appearance, options }),
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
async addPolyline(
|
|
521
|
+
points: ReadonlyArray<M.LatLng>,
|
|
522
|
+
options: M.PolylineOptions = {},
|
|
523
|
+
) {
|
|
524
|
+
return new RanaPolyline(
|
|
525
|
+
this.runtime,
|
|
526
|
+
await this.call<string>("addPolyline", { points, options }),
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
showRoute(route: M.Route, options: M.PolylineOptions = {}) {
|
|
530
|
+
return this.addPolyline(decodePolyline(route.geometry), options);
|
|
531
|
+
}
|
|
532
|
+
async addPolygon(
|
|
533
|
+
rings: ReadonlyArray<ReadonlyArray<M.LatLng>>,
|
|
534
|
+
options: M.ShapeOptions = {},
|
|
535
|
+
) {
|
|
536
|
+
return new RanaOverlay(
|
|
537
|
+
this.runtime,
|
|
538
|
+
await this.call<string>("addPolygon", { rings, options }),
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
async addCircle(
|
|
542
|
+
center: M.LatLng,
|
|
543
|
+
radiusMetres: number,
|
|
544
|
+
options: M.ShapeOptions = {},
|
|
545
|
+
) {
|
|
546
|
+
return new RanaOverlay(
|
|
547
|
+
this.runtime,
|
|
548
|
+
await this.call<string>("addCircle", { center, radiusMetres, options }),
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
showUserLocation(point: M.LatLng, options: M.UserLocationOptions = {}) {
|
|
552
|
+
return this.call<void>("showUserLocation", { point, options });
|
|
553
|
+
}
|
|
554
|
+
hideUserLocation() {
|
|
555
|
+
return this.call<void>("hideUserLocation");
|
|
556
|
+
}
|
|
557
|
+
updateLocation(fix: M.LocationFix) {
|
|
558
|
+
return this.call<void>("updateLocation", fix);
|
|
559
|
+
}
|
|
560
|
+
updateDeviceHeading(fix: M.HeadingFix) {
|
|
561
|
+
return this.call<void>("updateDeviceHeading", fix);
|
|
562
|
+
}
|
|
563
|
+
useNativeLocationProvider(options: M.NativeLocationOptions = {}) {
|
|
564
|
+
this.providerCleanup?.();
|
|
565
|
+
this.providerCleanup = undefined;
|
|
566
|
+
return this.call<void>("setLocationProvider", {
|
|
567
|
+
provider: "native",
|
|
568
|
+
options,
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
async setLocationProvider(provider: "native" | LocationProvider | null) {
|
|
572
|
+
this.providerCleanup?.();
|
|
573
|
+
this.providerCleanup = undefined;
|
|
574
|
+
if (typeof provider === "string" || provider === null) {
|
|
575
|
+
await this.call("setLocationProvider", { provider: provider ?? "none" });
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
const callbackId = `location-${this.handle}-${Date.now()}`;
|
|
579
|
+
let handle: string | undefined;
|
|
580
|
+
let generation = 0;
|
|
581
|
+
let headingGeneration = 0;
|
|
582
|
+
const fixes: Array<() => void> = [];
|
|
583
|
+
const deliver = (method: string, value: unknown) => {
|
|
584
|
+
const send = () => {
|
|
585
|
+
if (handle) void this.runtime.call(handle, method, value);
|
|
586
|
+
};
|
|
587
|
+
if (handle) send();
|
|
588
|
+
else fixes.push(send);
|
|
589
|
+
};
|
|
590
|
+
const off = this.runtime.on(callbackId, (event) => {
|
|
591
|
+
if (event.type === "providerStart") {
|
|
592
|
+
const token = ++generation;
|
|
593
|
+
provider.start(
|
|
594
|
+
(f) => {
|
|
595
|
+
if (token === generation) deliver("location", f);
|
|
596
|
+
},
|
|
597
|
+
(e) => {
|
|
598
|
+
if (token === generation) deliver("error", e);
|
|
599
|
+
},
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
if (event.type === "providerStop") {
|
|
603
|
+
generation++;
|
|
604
|
+
provider.stop();
|
|
605
|
+
}
|
|
606
|
+
if (event.type === "headingStart") {
|
|
607
|
+
const token = ++headingGeneration;
|
|
608
|
+
provider.startHeading?.((f) => {
|
|
609
|
+
if (token === headingGeneration) deliver("heading", f);
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
if (event.type === "headingStop") {
|
|
613
|
+
headingGeneration++;
|
|
614
|
+
provider.stopHeading?.();
|
|
615
|
+
}
|
|
616
|
+
});
|
|
617
|
+
const cleanup = () => {
|
|
618
|
+
generation++;
|
|
619
|
+
headingGeneration++;
|
|
620
|
+
off();
|
|
621
|
+
provider.stop();
|
|
622
|
+
provider.stopHeading?.();
|
|
623
|
+
fixes.length = 0;
|
|
624
|
+
};
|
|
625
|
+
try {
|
|
626
|
+
handle = await this.call<string>("setLocationProvider", {
|
|
627
|
+
provider: "custom",
|
|
628
|
+
callbackId,
|
|
629
|
+
});
|
|
630
|
+
fixes.splice(0).forEach((send) => send());
|
|
631
|
+
this.providerCleanup = cleanup;
|
|
632
|
+
} catch (error) {
|
|
633
|
+
cleanup();
|
|
634
|
+
throw error;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
setLocationRoute(
|
|
638
|
+
points: ReadonlyArray<M.LatLng> | null,
|
|
639
|
+
options: M.LocationRouteOptions = {},
|
|
640
|
+
) {
|
|
641
|
+
return this.call<void>("setLocationRoute", { points, options });
|
|
642
|
+
}
|
|
643
|
+
locationRouteStatus() {
|
|
644
|
+
return this.call<M.LocationRouteStatus>("locationRouteStatus");
|
|
645
|
+
}
|
|
646
|
+
async setAutomaticRerouting(
|
|
647
|
+
options: M.ReroutingOptions | null,
|
|
648
|
+
provider?: RanaClient | ReroutingProvider,
|
|
649
|
+
) {
|
|
650
|
+
this.reroutingCleanup?.();
|
|
651
|
+
this.reroutingCleanup = undefined;
|
|
652
|
+
const cb =
|
|
653
|
+
typeof provider === "function"
|
|
654
|
+
? this.runtime.registerCallback((_, value, signal) =>
|
|
655
|
+
provider(value as M.ReroutingRequest, signal),
|
|
656
|
+
)
|
|
657
|
+
: undefined;
|
|
658
|
+
try {
|
|
659
|
+
await this.call("setAutomaticRerouting", {
|
|
660
|
+
options,
|
|
661
|
+
client: provider instanceof RanaClient ? provider.handle : undefined,
|
|
662
|
+
providerId: cb?.id,
|
|
663
|
+
});
|
|
664
|
+
this.reroutingCleanup = cb?.dispose;
|
|
665
|
+
} catch (error) {
|
|
666
|
+
cb?.dispose();
|
|
667
|
+
throw error;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
setJourneyTraceRecorder(recorder: JourneyTraceRecorder | null) {
|
|
671
|
+
return this.call<void>("setJourneyTraceRecorder", {
|
|
672
|
+
recorder: recorder?.handle ?? null,
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
async addGeoJsonSource(id: string, geoJson: string) {
|
|
676
|
+
return new RanaGeoJsonSource(
|
|
677
|
+
this.runtime,
|
|
678
|
+
await this.call<string>("addGeoJsonSource", { id, geoJson }),
|
|
679
|
+
);
|
|
680
|
+
}
|
|
681
|
+
async addLayer(id: string, sourceId: string, options: DataLayerOptions) {
|
|
682
|
+
return new RanaStyleLayer(
|
|
683
|
+
this.runtime,
|
|
684
|
+
await this.call<string>("addLayer", { id, sourceId, options }),
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
layerIds() {
|
|
688
|
+
return this.call<string[]>("layerIds");
|
|
689
|
+
}
|
|
690
|
+
setLayerVisibility(id: string, visible: boolean) {
|
|
691
|
+
return this.call<void>("setLayerVisibility", { id, visible });
|
|
692
|
+
}
|
|
693
|
+
setLayerColor(id: string, color: string) {
|
|
694
|
+
return this.call<void>("setLayerColor", { id, color });
|
|
695
|
+
}
|
|
696
|
+
queryFeatures(point: M.LatLng, layerIds: ReadonlyArray<string> = []) {
|
|
697
|
+
return this.call<string[]>("queryFeatures", { point, layerIds });
|
|
698
|
+
}
|
|
699
|
+
override async dispose() {
|
|
700
|
+
this.cleanup.splice(0).forEach((fn) => fn());
|
|
701
|
+
this.providerCleanup?.();
|
|
702
|
+
this.reroutingCleanup?.();
|
|
703
|
+
await super.dispose();
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
export function decodePolyline(value: string, precision = 6): M.LatLng[] {
|
|
707
|
+
let index = 0,
|
|
708
|
+
latitude = 0,
|
|
709
|
+
longitude = 0;
|
|
710
|
+
const next = () => {
|
|
711
|
+
let result = 0,
|
|
712
|
+
shift = 0,
|
|
713
|
+
b: number;
|
|
714
|
+
do {
|
|
715
|
+
if (index >= value.length || shift > 30)
|
|
716
|
+
throw new Error("Invalid polyline");
|
|
717
|
+
b = value.charCodeAt(index++) - 63;
|
|
718
|
+
if (b < 0 || b > 63) throw new Error("Invalid polyline");
|
|
719
|
+
result |= (b & 31) << shift;
|
|
720
|
+
shift += 5;
|
|
721
|
+
} while (b >= 32);
|
|
722
|
+
return result & 1 ? ~(result >>> 1) : result >>> 1;
|
|
723
|
+
};
|
|
724
|
+
const points: M.LatLng[] = [];
|
|
725
|
+
const scale = 10 ** precision;
|
|
726
|
+
while (index < value.length) {
|
|
727
|
+
latitude += next();
|
|
728
|
+
longitude += next();
|
|
729
|
+
points.push({ latitude: latitude / scale, longitude: longitude / scale });
|
|
730
|
+
}
|
|
731
|
+
return points;
|
|
732
|
+
}
|