@ingissa/navcore-mapbox 1.0.3 → 1.0.8
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/LICENSE +3 -0
- package/package.json +17 -9
- package/src/MapboxDirectionsProvider.ts +198 -0
- package/src/index.ts +9 -0
package/LICENSE
ADDED
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ingissa/navcore-mapbox",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "Mapbox GL adapter for NavCore — renderer and directions provider.",
|
|
5
5
|
"author": "Ingissa <8abnav@gmail.com>",
|
|
6
|
-
"license": "
|
|
7
|
-
"homepage": "https://navcore
|
|
6
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
7
|
+
"homepage": "https://navcore.dev",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
|
-
"url": "git+https://github.com/ingissa/
|
|
10
|
+
"url": "git+https://github.com/ingissa/navcore-sdk.git"
|
|
11
11
|
},
|
|
12
12
|
"bugs": {
|
|
13
|
-
"url": "https://github.com/ingissa/
|
|
13
|
+
"url": "https://github.com/ingissa/navcore-sdk/issues"
|
|
14
14
|
},
|
|
15
15
|
"main": "dist/index.js",
|
|
16
16
|
"module": "dist/index.mjs",
|
|
@@ -22,23 +22,31 @@
|
|
|
22
22
|
"types": "./dist/index.d.ts"
|
|
23
23
|
}
|
|
24
24
|
},
|
|
25
|
-
"files": [
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"src",
|
|
28
|
+
"LICENSE"
|
|
29
|
+
],
|
|
26
30
|
"sideEffects": false,
|
|
27
31
|
"publishConfig": {
|
|
28
32
|
"access": "public"
|
|
29
33
|
},
|
|
30
|
-
"engines": {
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=16"
|
|
36
|
+
},
|
|
31
37
|
"scripts": {
|
|
32
38
|
"build": "tsc -p tsconfig.build.json",
|
|
33
39
|
"lint": "tsc --noEmit",
|
|
34
40
|
"prepublishOnly": "npm run build"
|
|
35
41
|
},
|
|
36
42
|
"peerDependencies": {
|
|
37
|
-
"@ingissa/navcore-core": "^1.0.
|
|
43
|
+
"@ingissa/navcore-core": "^1.0.8",
|
|
38
44
|
"@rnmapbox/maps": ">=10.0.0"
|
|
39
45
|
},
|
|
40
46
|
"peerDependenciesMeta": {
|
|
41
|
-
"@rnmapbox/maps": {
|
|
47
|
+
"@rnmapbox/maps": {
|
|
48
|
+
"optional": true
|
|
49
|
+
}
|
|
42
50
|
},
|
|
43
51
|
"dependencies": {}
|
|
44
52
|
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MapboxDirectionsProvider
|
|
3
|
+
*
|
|
4
|
+
* Implements DirectionsProvider using the Mapbox Directions API.
|
|
5
|
+
* Handles waypoint chunking internally for routes with >25 waypoints.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
type Coordinate = [number, number];
|
|
9
|
+
|
|
10
|
+
export interface MapboxDirectionsConfig {
|
|
11
|
+
/** Mapbox access token */
|
|
12
|
+
accessToken: string;
|
|
13
|
+
/** Routing profile (default: 'driving') */
|
|
14
|
+
profile?: 'driving' | 'driving-traffic' | 'cycling' | 'walking';
|
|
15
|
+
/** API base URL (default: Mapbox production) */
|
|
16
|
+
baseUrl?: string;
|
|
17
|
+
/** Max waypoints per API call (default: 25, Mapbox limit) */
|
|
18
|
+
maxWaypointsPerChunk?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface RouteResult {
|
|
22
|
+
geometry: Coordinate[];
|
|
23
|
+
distance: number;
|
|
24
|
+
duration: number;
|
|
25
|
+
legs: Array<{
|
|
26
|
+
distance: number;
|
|
27
|
+
duration: number;
|
|
28
|
+
steps?: Array<{
|
|
29
|
+
distance: number;
|
|
30
|
+
duration: number;
|
|
31
|
+
geometry: Coordinate[];
|
|
32
|
+
maneuver: {
|
|
33
|
+
type: string;
|
|
34
|
+
modifier?: string;
|
|
35
|
+
location: Coordinate;
|
|
36
|
+
bearing_before: number;
|
|
37
|
+
bearing_after: number;
|
|
38
|
+
instruction?: string;
|
|
39
|
+
};
|
|
40
|
+
name?: string;
|
|
41
|
+
}>;
|
|
42
|
+
}>;
|
|
43
|
+
raw?: unknown;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface RouteOptions {
|
|
47
|
+
profile?: string;
|
|
48
|
+
alternatives?: boolean;
|
|
49
|
+
steps?: boolean;
|
|
50
|
+
geometries?: string;
|
|
51
|
+
overview?: string;
|
|
52
|
+
language?: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const DEFAULT_BASE_URL = 'https://api.mapbox.com/directions/v5/mapbox';
|
|
56
|
+
const MAX_WAYPOINTS = 25;
|
|
57
|
+
|
|
58
|
+
export class MapboxDirectionsProvider {
|
|
59
|
+
private readonly config: Required<MapboxDirectionsConfig>;
|
|
60
|
+
|
|
61
|
+
constructor(config: MapboxDirectionsConfig) {
|
|
62
|
+
this.config = {
|
|
63
|
+
accessToken: config.accessToken,
|
|
64
|
+
profile: config.profile ?? 'driving',
|
|
65
|
+
baseUrl: config.baseUrl ?? DEFAULT_BASE_URL,
|
|
66
|
+
maxWaypointsPerChunk: config.maxWaypointsPerChunk ?? MAX_WAYPOINTS,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Get a route through the given waypoints.
|
|
72
|
+
* Automatically chunks if waypoints exceed the Mapbox limit.
|
|
73
|
+
*/
|
|
74
|
+
async getRoute(waypoints: Coordinate[], options?: RouteOptions): Promise<RouteResult> {
|
|
75
|
+
if (waypoints.length < 2) {
|
|
76
|
+
throw new Error('At least 2 waypoints are required');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// If within limit, single request
|
|
80
|
+
if (waypoints.length <= this.config.maxWaypointsPerChunk) {
|
|
81
|
+
return this.fetchRoute(waypoints, options);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Otherwise chunk with OVERLAP_1 strategy
|
|
85
|
+
return this.fetchChunkedRoute(waypoints, options);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Fetch a single route segment from the Mapbox API.
|
|
90
|
+
*/
|
|
91
|
+
private async fetchRoute(waypoints: Coordinate[], options?: RouteOptions): Promise<RouteResult> {
|
|
92
|
+
const profile = options?.profile ?? this.config.profile;
|
|
93
|
+
const coords = waypoints.map(w => `${w[0]},${w[1]}`).join(';');
|
|
94
|
+
|
|
95
|
+
const params = new URLSearchParams({
|
|
96
|
+
access_token: this.config.accessToken,
|
|
97
|
+
geometries: options?.geometries ?? 'geojson',
|
|
98
|
+
overview: options?.overview ?? 'full',
|
|
99
|
+
steps: String(options?.steps ?? true),
|
|
100
|
+
alternatives: String(options?.alternatives ?? false),
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
if (options?.language) {
|
|
104
|
+
params.set('language', options.language);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const url = `${this.config.baseUrl}/${profile}/${coords}?${params}`;
|
|
108
|
+
const response = await fetch(url);
|
|
109
|
+
|
|
110
|
+
if (!response.ok) {
|
|
111
|
+
throw new Error(`Mapbox API error: ${response.status} ${response.statusText}`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const data = await response.json();
|
|
115
|
+
|
|
116
|
+
if (!data.routes || data.routes.length === 0) {
|
|
117
|
+
throw new Error('No route found');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const route = data.routes[0];
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
geometry: route.geometry.coordinates as Coordinate[],
|
|
124
|
+
distance: route.distance,
|
|
125
|
+
duration: route.duration,
|
|
126
|
+
legs: route.legs.map((leg: { distance: number; duration: number; steps?: unknown[] }) => ({
|
|
127
|
+
distance: leg.distance,
|
|
128
|
+
duration: leg.duration,
|
|
129
|
+
steps: leg.steps?.map((step: any) => ({
|
|
130
|
+
distance: step.distance,
|
|
131
|
+
duration: step.duration,
|
|
132
|
+
geometry: step.geometry?.coordinates ?? [],
|
|
133
|
+
maneuver: {
|
|
134
|
+
type: step.maneuver?.type ?? 'unknown',
|
|
135
|
+
modifier: step.maneuver?.modifier,
|
|
136
|
+
location: step.maneuver?.location ?? [0, 0],
|
|
137
|
+
bearing_before: step.maneuver?.bearing_before ?? 0,
|
|
138
|
+
bearing_after: step.maneuver?.bearing_after ?? 0,
|
|
139
|
+
instruction: step.maneuver?.instruction,
|
|
140
|
+
},
|
|
141
|
+
name: step.name,
|
|
142
|
+
})),
|
|
143
|
+
})),
|
|
144
|
+
raw: data,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Fetch multi-segment route, stitching chunks with OVERLAP_1 strategy.
|
|
150
|
+
*/
|
|
151
|
+
private async fetchChunkedRoute(waypoints: Coordinate[], options?: RouteOptions): Promise<RouteResult> {
|
|
152
|
+
const chunkSize = this.config.maxWaypointsPerChunk;
|
|
153
|
+
const step = chunkSize - 1; // OVERLAP_1
|
|
154
|
+
const chunks: Coordinate[][] = [];
|
|
155
|
+
|
|
156
|
+
for (let i = 0; i < waypoints.length; i += step) {
|
|
157
|
+
const chunk = waypoints.slice(i, i + chunkSize);
|
|
158
|
+
if (chunk.length < 2) break;
|
|
159
|
+
chunks.push(chunk);
|
|
160
|
+
if (i + chunkSize >= waypoints.length) break;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Fetch all chunks in parallel
|
|
164
|
+
const results = await Promise.all(
|
|
165
|
+
chunks.map(chunk => this.fetchRoute(chunk, options))
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
// Stitch geometries — drop first point of subsequent chunks (it's the overlap)
|
|
169
|
+
const stitchedGeometry: Coordinate[] = [];
|
|
170
|
+
let totalDistance = 0;
|
|
171
|
+
let totalDuration = 0;
|
|
172
|
+
const allLegs: RouteResult['legs'] = [];
|
|
173
|
+
|
|
174
|
+
for (let i = 0; i < results.length; i++) {
|
|
175
|
+
const result = results[i]!;
|
|
176
|
+
const geom = result.geometry;
|
|
177
|
+
|
|
178
|
+
if (i === 0) {
|
|
179
|
+
stitchedGeometry.push(...geom);
|
|
180
|
+
} else {
|
|
181
|
+
// Skip first point (overlap with previous chunk's last point)
|
|
182
|
+
stitchedGeometry.push(...geom.slice(1));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
totalDistance += result.distance;
|
|
186
|
+
totalDuration += result.duration;
|
|
187
|
+
allLegs.push(...result.legs);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return {
|
|
191
|
+
geometry: stitchedGeometry,
|
|
192
|
+
distance: totalDistance,
|
|
193
|
+
duration: totalDuration,
|
|
194
|
+
legs: allLegs,
|
|
195
|
+
raw: results.map(r => r.raw),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @navcore/mapbox — Mapbox GL adapters
|
|
3
|
+
*
|
|
4
|
+
* Provides NavCoreRenderer and DirectionsProvider implementations
|
|
5
|
+
* for Mapbox GL (via @rnmapbox/maps for React Native, or mapbox-gl for web).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export { MapboxDirectionsProvider } from './MapboxDirectionsProvider';
|
|
9
|
+
export type { MapboxDirectionsConfig } from './MapboxDirectionsProvider';
|