@maptiler/client 3.1.0 → 3.2.0-rc.1

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.
@@ -1212,6 +1212,239 @@ const geolocation = {
1212
1212
  info
1213
1213
  };
1214
1214
 
1215
+ /**
1216
+ * Copyright 2020 Google LLC
1217
+ *
1218
+ * Licensed under the Apache License, Version 2.0 (the "License");
1219
+ * you may not use this file except in compliance with the License.
1220
+ * You may obtain a copy of the License at
1221
+ *
1222
+ * http://www.apache.org/licenses/LICENSE-2.0
1223
+ *
1224
+ * Unless required by applicable law or agreed to in writing, software
1225
+ * distributed under the License is distributed on an "AS IS" BASIS,
1226
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1227
+ * See the License for the specific language governing permissions and
1228
+ * limitations under the License.
1229
+ */
1230
+ /**
1231
+ * Decodes an encoded path string into a sequence of LatLngs.
1232
+ *
1233
+ * See {@link https://developers.google.com/maps/documentation/utilities/polylinealgorithm}
1234
+ *
1235
+ * #### Example
1236
+ *
1237
+ * ```js
1238
+ * import { decode } from "@googlemaps/polyline-codec";
1239
+ *
1240
+ * const encoded = "_p~iF~ps|U_ulLnnqC_mqNvxq`@";
1241
+ * console.log(decode(encoded, 5));
1242
+ * // [
1243
+ * // [38.5, -120.2],
1244
+ * // [40.7, -120.95],
1245
+ * // [43.252, -126.453],
1246
+ * // ]
1247
+ * ```
1248
+ */
1249
+ var decode = function (encodedPath, precision) {
1250
+ var factor = Math.pow(10, precision);
1251
+ var len = encodedPath.length;
1252
+ // For speed we preallocate to an upper bound on the final length, then
1253
+ // truncate the array before returning.
1254
+ var path = new Array(Math.floor(encodedPath.length / 2));
1255
+ var index = 0;
1256
+ var lat = 0;
1257
+ var lng = 0;
1258
+ var pointIndex = 0;
1259
+ // This code has been profiled and optimized, so don't modify it without
1260
+ // measuring its performance.
1261
+ for (; index < len; ++pointIndex) {
1262
+ // Fully unrolling the following loops speeds things up about 5%.
1263
+ var result = 1;
1264
+ var shift = 0;
1265
+ var b = void 0;
1266
+ do {
1267
+ // Invariant: "result" is current partial result plus (1 << shift).
1268
+ // The following line effectively clears this bit by decrementing "b".
1269
+ b = encodedPath.charCodeAt(index++) - 63 - 1;
1270
+ result += b << shift;
1271
+ shift += 5;
1272
+ } while (b >= 0x1f); // See note above.
1273
+ lat += result & 1 ? ~(result >> 1) : result >> 1;
1274
+ result = 1;
1275
+ shift = 0;
1276
+ do {
1277
+ b = encodedPath.charCodeAt(index++) - 63 - 1;
1278
+ result += b << shift;
1279
+ shift += 5;
1280
+ } while (b >= 0x1f);
1281
+ lng += result & 1 ? ~(result >> 1) : result >> 1;
1282
+ path[pointIndex] = [lat / factor, lng / factor];
1283
+ }
1284
+ // truncate array
1285
+ path.length = pointIndex;
1286
+ return path;
1287
+ };
1288
+
1289
+ const POLYLINE_PRECISION = 6;
1290
+ function addLocations(search, locations) {
1291
+ for (const loc of locations) {
1292
+ search.append("location", `${loc.lon},${loc.lat}`);
1293
+ }
1294
+ }
1295
+ function addResponseOptions(search, response) {
1296
+ if (!response) return;
1297
+ if (response.units !== void 0)
1298
+ search.set("response.units", response.units);
1299
+ if (response.language !== void 0)
1300
+ search.set("response.language", response.language);
1301
+ if (response.alternates !== void 0)
1302
+ search.set("alternates", String(response.alternates));
1303
+ if (response.additionalData?.detailLevel !== void 0)
1304
+ search.set(
1305
+ "response.additionalData.detailLevel",
1306
+ response.additionalData.detailLevel
1307
+ );
1308
+ }
1309
+ function addSpeedAndAvoidances(search, opts) {
1310
+ if (opts.topSpeed !== void 0)
1311
+ search.set("topSpeed", String(opts.topSpeed));
1312
+ const { avoidances } = opts;
1313
+ if (!avoidances) return;
1314
+ if (avoidances.tolls !== void 0)
1315
+ search.set("avoidances.tolls", String(avoidances.tolls));
1316
+ if (avoidances.ferry !== void 0)
1317
+ search.set("avoidances.ferry", String(avoidances.ferry));
1318
+ if (avoidances.highway !== void 0)
1319
+ search.set("avoidances.highway", String(avoidances.highway));
1320
+ }
1321
+ function addProfileOptions(search, req) {
1322
+ switch (req.profile) {
1323
+ case "car": {
1324
+ const opts = req.profileOptions;
1325
+ if (!opts) return;
1326
+ if (opts.mode !== void 0) search.set("car.mode", opts.mode);
1327
+ addSpeedAndAvoidances(search, opts);
1328
+ return;
1329
+ }
1330
+ case "truck": {
1331
+ const opts = req.profileOptions;
1332
+ if (!opts) return;
1333
+ if (opts.height !== void 0)
1334
+ search.set("truck.height", String(opts.height));
1335
+ if (opts.weight !== void 0)
1336
+ search.set("truck.weight", String(opts.weight));
1337
+ if (opts.axleLoad !== void 0)
1338
+ search.set("truck.axleLoad", String(opts.axleLoad));
1339
+ if (opts.hazmat !== void 0)
1340
+ search.set("truck.hazmat", String(opts.hazmat));
1341
+ addSpeedAndAvoidances(search, opts);
1342
+ return;
1343
+ }
1344
+ case "bicycle": {
1345
+ const opts = req.profileOptions;
1346
+ if (!opts) return;
1347
+ if (opts.type !== void 0) search.set("bicycle.type", opts.type);
1348
+ if (opts.cyclingSpeed !== void 0)
1349
+ search.set("bicycle.cyclingSpeed", String(opts.cyclingSpeed));
1350
+ return;
1351
+ }
1352
+ case "pedestrian": {
1353
+ const opts = req.profileOptions;
1354
+ if (!opts) return;
1355
+ if (opts.walkingSpeed !== void 0)
1356
+ search.set("pedestrian.walkingSpeed", String(opts.walkingSpeed));
1357
+ return;
1358
+ }
1359
+ }
1360
+ }
1361
+ function addRoutingOptions(search, options) {
1362
+ const { adjustSearchParams, apiKey } = options;
1363
+ if (typeof adjustSearchParams === "function") {
1364
+ adjustSearchParams(search);
1365
+ }
1366
+ search.set("key", apiKey ?? config.apiKey);
1367
+ }
1368
+ function decodeLegGeometry(geometry) {
1369
+ if (typeof geometry === "string") {
1370
+ return decode(geometry, POLYLINE_PRECISION).map(([lat, lon]) => [lon, lat]);
1371
+ }
1372
+ return geometry;
1373
+ }
1374
+ function decodeRoute(route) {
1375
+ return {
1376
+ ...route,
1377
+ legs: route.legs.map((leg) => ({
1378
+ ...leg,
1379
+ geometry: decodeLegGeometry(leg.geometry)
1380
+ }))
1381
+ };
1382
+ }
1383
+ async function getErrorMessage(res) {
1384
+ let payload = null;
1385
+ try {
1386
+ payload = await res.json();
1387
+ } catch {
1388
+ return "";
1389
+ }
1390
+ return typeof payload?.message === "string" ? payload.message : "";
1391
+ }
1392
+ async function processDirectionsResponse(res) {
1393
+ if (!res.ok) {
1394
+ throw new ServiceError(res, await getErrorMessage(res));
1395
+ }
1396
+ const { route, alternates, ...rest } = await res.json();
1397
+ const response = { ...rest, route: decodeRoute(route) };
1398
+ if (alternates) {
1399
+ response.alternates = alternates.map(decodeRoute);
1400
+ }
1401
+ return response;
1402
+ }
1403
+ async function directionsPost(body, options = {}) {
1404
+ const { fetchExtras } = options;
1405
+ const url = new URL("routing/v1/directions", config.apiURL);
1406
+ addRoutingOptions(url.searchParams, options);
1407
+ const headers = new Headers(fetchExtras?.headers);
1408
+ headers.set("content-type", "application/json");
1409
+ const res = await callFetch(url.toString(), {
1410
+ ...fetchExtras,
1411
+ method: "POST",
1412
+ headers,
1413
+ body: JSON.stringify(body)
1414
+ });
1415
+ return await processDirectionsResponse(res);
1416
+ }
1417
+ async function directionsGet(req, options = {}) {
1418
+ const { fetchExtras } = options;
1419
+ const url = new URL("routing/v1/directions", config.apiURL);
1420
+ const search = url.searchParams;
1421
+ search.set("profile", req.profile);
1422
+ if (req.id !== void 0) search.set("id", req.id);
1423
+ if (req.departureTime !== void 0)
1424
+ search.set("departureTime", req.departureTime);
1425
+ if (req.arrivalTime !== void 0) search.set("arrivalTime", req.arrivalTime);
1426
+ addLocations(search, req.locations);
1427
+ addResponseOptions(search, req.response);
1428
+ addProfileOptions(search, req);
1429
+ addRoutingOptions(search, options);
1430
+ const res = await callFetch(url.toString(), {
1431
+ ...fetchExtras,
1432
+ method: "GET",
1433
+ body: null
1434
+ });
1435
+ return await processDirectionsResponse(res);
1436
+ }
1437
+ const routing = {
1438
+ /**
1439
+ * Get directions using POST request
1440
+ */
1441
+ directionsPost,
1442
+ /**
1443
+ * Get directions using GET request
1444
+ */
1445
+ directionsGet
1446
+ };
1447
+
1215
1448
  const customMessages$2 = {
1216
1449
  403: "Key is missing, invalid or restricted"
1217
1450
  };
@@ -3355,6 +3588,7 @@ exports.isLanguageInfo = isLanguageInfo;
3355
3588
  exports.mapStylePresetList = mapStylePresetList;
3356
3589
  exports.math = math;
3357
3590
  exports.misc = misc;
3591
+ exports.routing = routing;
3358
3592
  exports.staticMaps = staticMaps;
3359
3593
  exports.styleToStyle = styleToStyle;
3360
3594
  exports.toLanguageInfo = toLanguageInfo;