@maptiler/client 3.1.0-rc.1 → 3.1.0-rc.3

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.
@@ -1189,9 +1189,83 @@ const geolocation = {
1189
1189
  info
1190
1190
  };
1191
1191
 
1192
+ /**
1193
+ * Copyright 2020 Google LLC
1194
+ *
1195
+ * Licensed under the Apache License, Version 2.0 (the "License");
1196
+ * you may not use this file except in compliance with the License.
1197
+ * You may obtain a copy of the License at
1198
+ *
1199
+ * http://www.apache.org/licenses/LICENSE-2.0
1200
+ *
1201
+ * Unless required by applicable law or agreed to in writing, software
1202
+ * distributed under the License is distributed on an "AS IS" BASIS,
1203
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1204
+ * See the License for the specific language governing permissions and
1205
+ * limitations under the License.
1206
+ */
1207
+ /**
1208
+ * Decodes an encoded path string into a sequence of LatLngs.
1209
+ *
1210
+ * See {@link https://developers.google.com/maps/documentation/utilities/polylinealgorithm}
1211
+ *
1212
+ * #### Example
1213
+ *
1214
+ * ```js
1215
+ * import { decode } from "@googlemaps/polyline-codec";
1216
+ *
1217
+ * const encoded = "_p~iF~ps|U_ulLnnqC_mqNvxq`@";
1218
+ * console.log(decode(encoded, 5));
1219
+ * // [
1220
+ * // [38.5, -120.2],
1221
+ * // [40.7, -120.95],
1222
+ * // [43.252, -126.453],
1223
+ * // ]
1224
+ * ```
1225
+ */
1226
+ var decode = function (encodedPath, precision) {
1227
+ var factor = Math.pow(10, precision);
1228
+ var len = encodedPath.length;
1229
+ // For speed we preallocate to an upper bound on the final length, then
1230
+ // truncate the array before returning.
1231
+ var path = new Array(Math.floor(encodedPath.length / 2));
1232
+ var index = 0;
1233
+ var lat = 0;
1234
+ var lng = 0;
1235
+ var pointIndex = 0;
1236
+ // This code has been profiled and optimized, so don't modify it without
1237
+ // measuring its performance.
1238
+ for (; index < len; ++pointIndex) {
1239
+ // Fully unrolling the following loops speeds things up about 5%.
1240
+ var result = 1;
1241
+ var shift = 0;
1242
+ var b = void 0;
1243
+ do {
1244
+ // Invariant: "result" is current partial result plus (1 << shift).
1245
+ // The following line effectively clears this bit by decrementing "b".
1246
+ b = encodedPath.charCodeAt(index++) - 63 - 1;
1247
+ result += b << shift;
1248
+ shift += 5;
1249
+ } while (b >= 0x1f); // See note above.
1250
+ lat += result & 1 ? ~(result >> 1) : result >> 1;
1251
+ result = 1;
1252
+ shift = 0;
1253
+ do {
1254
+ b = encodedPath.charCodeAt(index++) - 63 - 1;
1255
+ result += b << shift;
1256
+ shift += 5;
1257
+ } while (b >= 0x1f);
1258
+ lng += result & 1 ? ~(result >> 1) : result >> 1;
1259
+ path[pointIndex] = [lat / factor, lng / factor];
1260
+ }
1261
+ // truncate array
1262
+ path.length = pointIndex;
1263
+ return path;
1264
+ };
1265
+
1192
1266
  function addLocations(search, locations) {
1193
1267
  for (const loc of locations) {
1194
- search.append("location", `${loc.lat},${loc.lon}`);
1268
+ search.append("location", `${loc.lon},${loc.lat}`);
1195
1269
  }
1196
1270
  }
1197
1271
  function addResponseOptions(search, response) {
@@ -1241,21 +1315,44 @@ function addProfileOptions(search, profile, opts) {
1241
1315
  search.set("pedestrian.walkingSpeed", String(o.walkingSpeed));
1242
1316
  }
1243
1317
  }
1244
- async function directionsPost(body) {
1245
- const url = new URL("routing/directions", defaults.maptilerApiURL);
1318
+ function decodeLegGeometry(leg) {
1319
+ if (typeof leg.geometry === "string") {
1320
+ return decode(leg.geometry, 6).map(([lat, lon]) => [lon, lat]);
1321
+ }
1322
+ return leg.geometry;
1323
+ }
1324
+ async function processDirectionsResponse(res) {
1325
+ if (!res.ok) {
1326
+ throw new ServiceError(res);
1327
+ }
1328
+ const response = await res.json();
1329
+ for (const leg of response.route.legs) {
1330
+ leg.geometry = decodeLegGeometry(leg);
1331
+ }
1332
+ if (response.alternates) {
1333
+ for (const alternateRoute of response.alternates) {
1334
+ for (const leg of alternateRoute.legs) {
1335
+ leg.geometry = decodeLegGeometry(leg);
1336
+ }
1337
+ }
1338
+ }
1339
+ return response;
1340
+ }
1341
+ async function directionsPost(body, fetchExtras) {
1342
+ const url = new URL("routing/v1/directions", defaults.maptilerApiURL);
1246
1343
  url.searchParams.set("key", config.apiKey);
1344
+ const headers = new Headers(fetchExtras?.headers);
1345
+ headers.set("content-type", "application/json");
1247
1346
  const res = await callFetch(url.toString(), {
1347
+ ...fetchExtras,
1248
1348
  method: "POST",
1249
- headers: { "content-type": "application/json" },
1349
+ headers,
1250
1350
  body: JSON.stringify(body)
1251
1351
  });
1252
- if (!res.ok) {
1253
- throw new ServiceError(res);
1254
- }
1255
- return await res.json();
1352
+ return await processDirectionsResponse(res);
1256
1353
  }
1257
- async function directionsGet(req) {
1258
- const url = new URL("routing/directions", defaults.maptilerApiURL);
1354
+ async function directionsGet(req, fetchExtras) {
1355
+ const url = new URL("routing/v1/directions", defaults.maptilerApiURL);
1259
1356
  const search = url.searchParams;
1260
1357
  search.set("key", config.apiKey);
1261
1358
  search.set("profile", req.profile);
@@ -1265,21 +1362,20 @@ async function directionsGet(req) {
1265
1362
  addLocations(search, req.locations);
1266
1363
  addResponseOptions(search, req.response);
1267
1364
  addProfileOptions(search, req.profile, req.profileOptions);
1268
- const res = await callFetch(url.toString());
1269
- if (!res.ok) {
1270
- throw new ServiceError(res);
1271
- }
1272
- return await res.json();
1365
+ const res = await callFetch(url.toString(), {
1366
+ ...fetchExtras,
1367
+ method: "GET",
1368
+ body: null
1369
+ });
1370
+ return await processDirectionsResponse(res);
1273
1371
  }
1274
1372
  const routing = {
1275
1373
  /**
1276
1374
  * Get directions using POST request
1277
- * @experimental This API is experimental and can change anytime!
1278
1375
  */
1279
1376
  directionsPost,
1280
1377
  /**
1281
1378
  * Get directions using GET request
1282
- * @experimental This API is experimental and can change anytime!
1283
1379
  */
1284
1380
  directionsGet
1285
1381
  };