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

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,30 @@ function addProfileOptions(search, profile, opts) {
1241
1315
  search.set("pedestrian.walkingSpeed", String(o.walkingSpeed));
1242
1316
  }
1243
1317
  }
1318
+ async function processDirectionsResponse(res) {
1319
+ if (!res.ok) {
1320
+ throw new ServiceError(res);
1321
+ }
1322
+ const response = await res.json();
1323
+ for (const leg of response.route.legs) {
1324
+ if (typeof leg.geometry === "string") {
1325
+ leg.geometry = decode(leg.geometry, 6).map(([lat, lon]) => [lon, lat]);
1326
+ }
1327
+ }
1328
+ return response;
1329
+ }
1244
1330
  async function directionsPost(body) {
1245
- const url = new URL("routing/directions", defaults.maptilerApiURL);
1331
+ const url = new URL("routing/v1/directions", defaults.maptilerApiURL);
1246
1332
  url.searchParams.set("key", config.apiKey);
1247
1333
  const res = await callFetch(url.toString(), {
1248
1334
  method: "POST",
1249
1335
  headers: { "content-type": "application/json" },
1250
1336
  body: JSON.stringify(body)
1251
1337
  });
1252
- if (!res.ok) {
1253
- throw new ServiceError(res);
1254
- }
1255
- return await res.json();
1338
+ return await processDirectionsResponse(res);
1256
1339
  }
1257
1340
  async function directionsGet(req) {
1258
- const url = new URL("routing/directions", defaults.maptilerApiURL);
1341
+ const url = new URL("routing/v1/directions", defaults.maptilerApiURL);
1259
1342
  const search = url.searchParams;
1260
1343
  search.set("key", config.apiKey);
1261
1344
  search.set("profile", req.profile);
@@ -1266,20 +1349,15 @@ async function directionsGet(req) {
1266
1349
  addResponseOptions(search, req.response);
1267
1350
  addProfileOptions(search, req.profile, req.profileOptions);
1268
1351
  const res = await callFetch(url.toString());
1269
- if (!res.ok) {
1270
- throw new ServiceError(res);
1271
- }
1272
- return await res.json();
1352
+ return await processDirectionsResponse(res);
1273
1353
  }
1274
1354
  const routing = {
1275
1355
  /**
1276
1356
  * Get directions using POST request
1277
- * @experimental This API is experimental and can change anytime!
1278
1357
  */
1279
1358
  directionsPost,
1280
1359
  /**
1281
1360
  * Get directions using GET request
1282
- * @experimental This API is experimental and can change anytime!
1283
1361
  */
1284
1362
  directionsGet
1285
1363
  };