@superrouteca/smap 1.1.7 → 1.2.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/CHANGELOG.md +50 -0
- package/README.md +118 -0
- package/dist/SOURCE.json +8 -8
- package/dist/smap-client.cjs +2 -2
- package/dist/smap-client.js +2 -2
- package/dist/smap-client.mjs +2 -2
- package/dist/smap.cjs +3319 -26
- package/dist/smap.css +42 -0
- package/dist/smap.js +3311 -23
- package/dist/smap.mjs +3318 -27
- package/package.json +1 -1
- package/types/smap.d.cts +291 -6
- package/types/smap.d.mts +291 -6
package/dist/smap.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* Generated by js/build.mjs from js/src (see SOURCE.json). Do not edit. */
|
|
2
2
|
'use strict';
|
|
3
|
-
/*! sMap Web SDK 1.
|
|
3
|
+
/*! sMap Web SDK 1.2.0. Bundles maplibre-gl 6.16.0 (BSD-3-Clause), see vendor-licenses.txt. */
|
|
4
4
|
|
|
5
5
|
var smapCore = null;
|
|
6
6
|
function smapCreateEngine() {
|
|
@@ -28283,6 +28283,131 @@ function smapCreateInterop() {
|
|
|
28283
28283
|
return json;
|
|
28284
28284
|
}
|
|
28285
28285
|
|
|
28286
|
+
// ------------------------------------------------------------------ geometry helpers (G14)
|
|
28287
|
+
|
|
28288
|
+
var EARTH_RADIUS = 6371008.8;
|
|
28289
|
+
function rad(d) { return d * Math.PI / 180; }
|
|
28290
|
+
function lngLatOf(v) {
|
|
28291
|
+
if (Array.isArray(v)) { return [+v[0], +v[1]]; }
|
|
28292
|
+
if (v && typeof v === 'object') {
|
|
28293
|
+
if (v.type === 'Feature') { return lngLatOf(v.geometry); }
|
|
28294
|
+
if (v.type === 'Point') { return lngLatOf(v.coordinates); }
|
|
28295
|
+
return [+(v.lng !== undefined ? v.lng : v.lon), +v.lat];
|
|
28296
|
+
}
|
|
28297
|
+
throw new Error('sMap: not a position');
|
|
28298
|
+
}
|
|
28299
|
+
function geometryOf(v) { return v && v.type === 'Feature' ? v.geometry : v; }
|
|
28300
|
+
|
|
28301
|
+
/** Great-circle distance in metres between two positions ([lng, lat], {lng, lat} or a Point). */
|
|
28302
|
+
function distance(a, b) {
|
|
28303
|
+
var p = lngLatOf(a), q = lngLatOf(b);
|
|
28304
|
+
var dLat = rad(q[1] - p[1]), dLng = rad(q[0] - p[0]);
|
|
28305
|
+
var h = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(rad(p[1])) * Math.cos(rad(q[1])) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
|
|
28306
|
+
return 2 * EARTH_RADIUS * Math.asin(Math.min(1, Math.sqrt(h)));
|
|
28307
|
+
}
|
|
28308
|
+
|
|
28309
|
+
function lineCoords(line) {
|
|
28310
|
+
var g = geometryOf(line);
|
|
28311
|
+
if (Array.isArray(g)) { return g.map(lngLatOf); }
|
|
28312
|
+
if (g && g.type === 'LineString') { return g.coordinates; }
|
|
28313
|
+
throw new Error('sMap: not a line');
|
|
28314
|
+
}
|
|
28315
|
+
|
|
28316
|
+
/** Length in metres of a line ([[lng, lat], …], a LineString or its Feature). */
|
|
28317
|
+
function length(line) {
|
|
28318
|
+
var c = lineCoords(line), total = 0;
|
|
28319
|
+
for (var i = 1; i < c.length; i++) { total += distance(c[i - 1], c[i]); }
|
|
28320
|
+
return total;
|
|
28321
|
+
}
|
|
28322
|
+
|
|
28323
|
+
/** [[west, south], [east, north]] of any GeoJSON (or null when it holds no position). */
|
|
28324
|
+
function bounds(geojson) {
|
|
28325
|
+
var b = null;
|
|
28326
|
+
(function walk(v) {
|
|
28327
|
+
if (!v) { return; }
|
|
28328
|
+
if (Array.isArray(v)) {
|
|
28329
|
+
if (typeof v[0] === 'number') {
|
|
28330
|
+
if (!b) { b = [[v[0], v[1]], [v[0], v[1]]]; return; }
|
|
28331
|
+
b[0][0] = Math.min(b[0][0], v[0]); b[0][1] = Math.min(b[0][1], v[1]);
|
|
28332
|
+
b[1][0] = Math.max(b[1][0], v[0]); b[1][1] = Math.max(b[1][1], v[1]);
|
|
28333
|
+
return;
|
|
28334
|
+
}
|
|
28335
|
+
v.forEach(walk);
|
|
28336
|
+
return;
|
|
28337
|
+
}
|
|
28338
|
+
if (v.type === 'FeatureCollection') { (v.features || []).forEach(walk); return; }
|
|
28339
|
+
if (v.type === 'Feature') { walk(v.geometry); return; }
|
|
28340
|
+
if (v.type === 'GeometryCollection') { (v.geometries || []).forEach(walk); return; }
|
|
28341
|
+
walk(v.coordinates);
|
|
28342
|
+
})(geojson);
|
|
28343
|
+
return b;
|
|
28344
|
+
}
|
|
28345
|
+
|
|
28346
|
+
function inRing(p, ring) {
|
|
28347
|
+
var inside = false;
|
|
28348
|
+
for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) {
|
|
28349
|
+
var xi = ring[i][0], yi = ring[i][1], xj = ring[j][0], yj = ring[j][1];
|
|
28350
|
+
if ((yi > p[1]) !== (yj > p[1]) && p[0] < (xj - xi) * (p[1] - yi) / (yj - yi) + xi) { inside = !inside; }
|
|
28351
|
+
}
|
|
28352
|
+
return inside;
|
|
28353
|
+
}
|
|
28354
|
+
|
|
28355
|
+
/** Whether a position is inside a Polygon or MultiPolygon (holes excluded), a Feature of one, or a ring of positions. */
|
|
28356
|
+
function pointInPolygon(point, polygon) {
|
|
28357
|
+
var p = lngLatOf(point), g = geometryOf(polygon);
|
|
28358
|
+
var polys = Array.isArray(g) ? [[g.map(lngLatOf)]] : (g && g.type === 'Polygon' ? [g.coordinates] : (g && g.type === 'MultiPolygon' ? g.coordinates : null));
|
|
28359
|
+
if (!polys) { throw new Error('sMap: not a polygon'); }
|
|
28360
|
+
return polys.some(function (rings) {
|
|
28361
|
+
if (!rings.length || !inRing(p, rings[0])) { return false; }
|
|
28362
|
+
for (var k = 1; k < rings.length; k++) { if (inRing(p, rings[k])) { return false; } }
|
|
28363
|
+
return true;
|
|
28364
|
+
});
|
|
28365
|
+
}
|
|
28366
|
+
|
|
28367
|
+
/**
|
|
28368
|
+
* The point of a line nearest to a position: { point: [lng, lat], distance (metres from the position), index (of the
|
|
28369
|
+
* segment's first vertex), along (metres from the line's start) }. Measured in a local flat projection around the
|
|
28370
|
+
* position, which is exact to well under a metre for the distances a map shows.
|
|
28371
|
+
*/
|
|
28372
|
+
function nearestPointOnLine(line, point) {
|
|
28373
|
+
var c = lineCoords(line), p = lngLatOf(point);
|
|
28374
|
+
if (!c.length) { return null; }
|
|
28375
|
+
var kx = Math.cos(rad(p[1])) * rad(1) * EARTH_RADIUS, ky = rad(1) * EARTH_RADIUS;
|
|
28376
|
+
var best = null, walked = 0;
|
|
28377
|
+
for (var i = 0; i < Math.max(1, c.length - 1); i++) {
|
|
28378
|
+
var a = c[i], b = c[Math.min(i + 1, c.length - 1)];
|
|
28379
|
+
var ax = (a[0] - p[0]) * kx, ay = (a[1] - p[1]) * ky, bx = (b[0] - p[0]) * kx, by = (b[1] - p[1]) * ky;
|
|
28380
|
+
var dx = bx - ax, dy = by - ay, len2 = dx * dx + dy * dy;
|
|
28381
|
+
var t = len2 ? Math.max(0, Math.min(1, -(ax * dx + ay * dy) / len2)) : 0;
|
|
28382
|
+
var x = ax + t * dx, y = ay + t * dy, d = Math.sqrt(x * x + y * y);
|
|
28383
|
+
var seg = distance(a, b);
|
|
28384
|
+
if (!best || d < best.distance) {
|
|
28385
|
+
best = { point: [p[0] + x / kx, p[1] + y / ky], distance: d, index: i, along: walked + seg * t };
|
|
28386
|
+
}
|
|
28387
|
+
walked += seg;
|
|
28388
|
+
}
|
|
28389
|
+
return best;
|
|
28390
|
+
}
|
|
28391
|
+
|
|
28392
|
+
/** Metres from a position to the nearest point of a line. */
|
|
28393
|
+
function distanceToLine(line, point) { var n = nearestPointOnLine(line, point); return n ? n.distance : Infinity; }
|
|
28394
|
+
|
|
28395
|
+
var geo = {
|
|
28396
|
+
distance: distance, length: length, bounds: bounds, pointInPolygon: pointInPolygon,
|
|
28397
|
+
nearestPointOnLine: nearestPointOnLine, distanceToLine: distanceToLine,
|
|
28398
|
+
decodePolyline: function (str, precision) { return decodePolyline(str, precision); },
|
|
28399
|
+
encodePolyline: function (coords, precision) { return encodePolyline(coords, precision); },
|
|
28400
|
+
};
|
|
28401
|
+
|
|
28402
|
+
// three.js for 3D models (G12), handed over once by the page (never bundled, like deck.gl).
|
|
28403
|
+
var three = null;
|
|
28404
|
+
function useThree(THREE, GLTFLoader) {
|
|
28405
|
+
if (!THREE || !THREE.Scene || typeof GLTFLoader !== 'function') { throw new Error('sMap: useThree(THREE, GLTFLoader) takes three.js and its GLTFLoader'); }
|
|
28406
|
+
three = { THREE: THREE, GLTFLoader: GLTFLoader };
|
|
28407
|
+
return three;
|
|
28408
|
+
}
|
|
28409
|
+
function getThree() { return three; }
|
|
28410
|
+
|
|
28286
28411
|
return {
|
|
28287
28412
|
DEFAULT_BASE: DEFAULT_BASE,
|
|
28288
28413
|
SLOTS: SLOTS,
|
|
@@ -28305,6 +28430,9 @@ function smapCreateInterop() {
|
|
|
28305
28430
|
encodePolyline: encodePolyline,
|
|
28306
28431
|
},
|
|
28307
28432
|
convert: convert,
|
|
28433
|
+
geo: geo,
|
|
28434
|
+
useThree: useThree,
|
|
28435
|
+
getThree: getThree,
|
|
28308
28436
|
styleUrl: styleUrl,
|
|
28309
28437
|
rasterUrl: rasterUrl,
|
|
28310
28438
|
rasterTileJsonUrl: rasterTileJsonUrl,
|
|
@@ -28328,6 +28456,1342 @@ function smapCreateInterop() {
|
|
|
28328
28456
|
|
|
28329
28457
|
return module.exports;
|
|
28330
28458
|
}
|
|
28459
|
+
function smapCreateCanvas() {
|
|
28460
|
+
var module = { exports: {} };
|
|
28461
|
+
var exports = module.exports;
|
|
28462
|
+
var define;
|
|
28463
|
+
var self = {};
|
|
28464
|
+
// sMap canvas renderer (M61): the map drawn on a 2D canvas for browsers without WebGL 2. It answers the part of the
|
|
28465
|
+
// rendering engine's map interface the Web SDK uses — camera, events, sources, layers of the kinds the SDK makes
|
|
28466
|
+
// (raster, fill, line, circle, symbol, heatmap), hit tests, controls and popups — so the SDK's calls work unchanged.
|
|
28467
|
+
// The street map comes as raster tiles. What needs WebGL (3D buildings, terrain, tilting, turning, custom 3D layers,
|
|
28468
|
+
// hillshade) is left out and named in an 'smap.unsupported' event.
|
|
28469
|
+
(function (root, factory) {
|
|
28470
|
+
if (typeof module === 'object' && module.exports) { module.exports = factory(); } else { root.smapCanvas = factory(); }
|
|
28471
|
+
})(typeof self !== 'undefined' ? self : this, function () {
|
|
28472
|
+
'use strict';
|
|
28473
|
+
|
|
28474
|
+
var TILE = 512; // world size per zoom level, as the engine counts zoom (512 px tiles)
|
|
28475
|
+
var MAX_LAT = 85.051129;
|
|
28476
|
+
var raf = typeof requestAnimationFrame === 'function' ? requestAnimationFrame.bind(typeof window !== 'undefined' ? window : this) : function (fn) { return setTimeout(function () { fn(Date.now()); }, 16); };
|
|
28477
|
+
|
|
28478
|
+
// ------------------------------------------------------------------ geometry
|
|
28479
|
+
|
|
28480
|
+
function clampLat(lat) { return Math.max(-MAX_LAT, Math.min(MAX_LAT, lat)); }
|
|
28481
|
+
function lngX(lng, scale) { return (lng + 180) / 360 * scale; }
|
|
28482
|
+
function latY(lat, scale) { var s = Math.sin(clampLat(lat) * Math.PI / 180); return (0.5 - Math.log((1 + s) / (1 - s)) / (4 * Math.PI)) * scale; }
|
|
28483
|
+
function xLng(x, scale) { return x / scale * 360 - 180; }
|
|
28484
|
+
function yLat(y, scale) { var n = Math.PI - 2 * Math.PI * y / scale; return 180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n))); }
|
|
28485
|
+
function toLngLat(v) {
|
|
28486
|
+
if (Array.isArray(v)) { return { lng: +v[0], lat: +v[1] }; }
|
|
28487
|
+
if (v && v.lng !== undefined) { return { lng: +v.lng, lat: +v.lat }; }
|
|
28488
|
+
if (v && v.lon !== undefined) { return { lng: +v.lon, lat: +v.lat }; }
|
|
28489
|
+
return { lng: 0, lat: 0 };
|
|
28490
|
+
}
|
|
28491
|
+
|
|
28492
|
+
function LngLat(lng, lat) { this.lng = lng; this.lat = lat; }
|
|
28493
|
+
LngLat.prototype.toArray = function () { return [this.lng, this.lat]; };
|
|
28494
|
+
|
|
28495
|
+
function LngLatBounds(sw, ne) {
|
|
28496
|
+
this._sw = null; this._ne = null;
|
|
28497
|
+
if (sw) { this.extend(sw); }
|
|
28498
|
+
if (ne) { this.extend(ne); }
|
|
28499
|
+
}
|
|
28500
|
+
LngLatBounds.prototype.extend = function (v) {
|
|
28501
|
+
if (v instanceof LngLatBounds) { if (v._sw) { this.extend(v._sw); this.extend(v._ne); } return this; }
|
|
28502
|
+
if (Array.isArray(v) && Array.isArray(v[0])) { this.extend(v[0]); this.extend(v[1]); return this; }
|
|
28503
|
+
var p = toLngLat(v);
|
|
28504
|
+
if (!this._sw) { this._sw = new LngLat(p.lng, p.lat); this._ne = new LngLat(p.lng, p.lat); return this; }
|
|
28505
|
+
this._sw.lng = Math.min(this._sw.lng, p.lng); this._sw.lat = Math.min(this._sw.lat, p.lat);
|
|
28506
|
+
this._ne.lng = Math.max(this._ne.lng, p.lng); this._ne.lat = Math.max(this._ne.lat, p.lat);
|
|
28507
|
+
return this;
|
|
28508
|
+
};
|
|
28509
|
+
LngLatBounds.prototype.getWest = function () { return this._sw.lng; };
|
|
28510
|
+
LngLatBounds.prototype.getSouth = function () { return this._sw.lat; };
|
|
28511
|
+
LngLatBounds.prototype.getEast = function () { return this._ne.lng; };
|
|
28512
|
+
LngLatBounds.prototype.getNorth = function () { return this._ne.lat; };
|
|
28513
|
+
LngLatBounds.prototype.getSouthWest = function () { return this._sw; };
|
|
28514
|
+
LngLatBounds.prototype.getNorthEast = function () { return this._ne; };
|
|
28515
|
+
LngLatBounds.prototype.getCenter = function () { return new LngLat((this._sw.lng + this._ne.lng) / 2, (this._sw.lat + this._ne.lat) / 2); };
|
|
28516
|
+
LngLatBounds.prototype.toArray = function () { return [[this._sw.lng, this._sw.lat], [this._ne.lng, this._ne.lat]]; };
|
|
28517
|
+
LngLatBounds.prototype.isEmpty = function () { return !this._sw; };
|
|
28518
|
+
|
|
28519
|
+
function segDist(px, py, ax, ay, bx, by) {
|
|
28520
|
+
var dx = bx - ax, dy = by - ay, l2 = dx * dx + dy * dy;
|
|
28521
|
+
var t = l2 ? Math.max(0, Math.min(1, ((px - ax) * dx + (py - ay) * dy) / l2)) : 0;
|
|
28522
|
+
var x = ax + t * dx - px, y = ay + t * dy - py;
|
|
28523
|
+
return Math.sqrt(x * x + y * y);
|
|
28524
|
+
}
|
|
28525
|
+
function inRing(x, y, ring) {
|
|
28526
|
+
var inside = false;
|
|
28527
|
+
for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) {
|
|
28528
|
+
var xi = ring[i][0], yi = ring[i][1], xj = ring[j][0], yj = ring[j][1];
|
|
28529
|
+
if ((yi > y) !== (yj > y) && x < (xj - xi) * (y - yi) / (yj - yi) + xi) { inside = !inside; }
|
|
28530
|
+
}
|
|
28531
|
+
return inside;
|
|
28532
|
+
}
|
|
28533
|
+
|
|
28534
|
+
// ------------------------------------------------------------------ expressions (the Style Specification subset the SDK writes)
|
|
28535
|
+
|
|
28536
|
+
function typeName(g) { if (!g) { return null; } var t = g.type; return t === 'MultiPoint' ? 'Point' : (t === 'MultiLineString' ? 'LineString' : (t === 'MultiPolygon' ? 'Polygon' : t)); }
|
|
28537
|
+
|
|
28538
|
+
function parseColor(c) { return c; }
|
|
28539
|
+
|
|
28540
|
+
function evaluate(e, ctx) {
|
|
28541
|
+
if (!Array.isArray(e)) { return e; }
|
|
28542
|
+
var op = e[0], p = (ctx.f && ctx.f.properties) || {};
|
|
28543
|
+
var ev = function (x) { return evaluate(x, ctx); };
|
|
28544
|
+
switch (op) {
|
|
28545
|
+
case 'literal': return e[1];
|
|
28546
|
+
case 'get': return e.length > 2 ? (ev(e[2]) || {})[ev(e[1])] : p[ev(e[1])];
|
|
28547
|
+
case 'has': return Object.prototype.hasOwnProperty.call(p, ev(e[1])) && p[ev(e[1])] !== undefined;
|
|
28548
|
+
case '!': return !ev(e[1]);
|
|
28549
|
+
case '==': return ev(e[1]) === ev(e[2]);
|
|
28550
|
+
case '!=': return ev(e[1]) !== ev(e[2]);
|
|
28551
|
+
case '>': return ev(e[1]) > ev(e[2]);
|
|
28552
|
+
case '>=': return ev(e[1]) >= ev(e[2]);
|
|
28553
|
+
case '<': return ev(e[1]) < ev(e[2]);
|
|
28554
|
+
case '<=': return ev(e[1]) <= ev(e[2]);
|
|
28555
|
+
case 'all': for (var a = 1; a < e.length; a++) { if (!ev(e[a])) { return false; } } return true;
|
|
28556
|
+
case 'any': for (var b = 1; b < e.length; b++) { if (ev(e[b])) { return true; } } return false;
|
|
28557
|
+
case 'case':
|
|
28558
|
+
for (var c = 1; c < e.length - 1; c += 2) { if (ev(e[c])) { return ev(e[c + 1]); } }
|
|
28559
|
+
return ev(e[e.length - 1]);
|
|
28560
|
+
case 'match': {
|
|
28561
|
+
var v = ev(e[1]);
|
|
28562
|
+
for (var d = 2; d < e.length - 1; d += 2) {
|
|
28563
|
+
var lab = e[d];
|
|
28564
|
+
if (Array.isArray(lab) ? lab.indexOf(v) >= 0 : lab === v) { return ev(e[d + 1]); }
|
|
28565
|
+
}
|
|
28566
|
+
return ev(e[e.length - 1]);
|
|
28567
|
+
}
|
|
28568
|
+
case 'coalesce': for (var k = 1; k < e.length; k++) { var x = ev(e[k]); if (x !== null && x !== undefined) { return x; } } return null;
|
|
28569
|
+
case 'to-string': { var s = ev(e[1]); return s === null || s === undefined ? '' : String(s); }
|
|
28570
|
+
case 'to-number': {
|
|
28571
|
+
for (var n = 1; n < e.length; n++) { var num = Number(ev(e[n])); if (!isNaN(num) && ev(e[n]) !== null && ev(e[n]) !== '') { return num; } }
|
|
28572
|
+
return 0;
|
|
28573
|
+
}
|
|
28574
|
+
case 'to-color': for (var q = 1; q < e.length; q++) { var col = ev(e[q]); if (typeof col === 'string' && col) { return col; } } return '#000000';
|
|
28575
|
+
case 'concat': { var out = ''; for (var r = 1; r < e.length; r++) { var part = ev(e[r]); out += part === null || part === undefined ? '' : String(part); } return out; }
|
|
28576
|
+
case 'zoom': return ctx.zoom;
|
|
28577
|
+
case 'heatmap-density': return ctx.density || 0;
|
|
28578
|
+
case 'geometry-type': return typeName(ctx.f && ctx.f.geometry);
|
|
28579
|
+
case 'id': return ctx.f ? ctx.f.id : null;
|
|
28580
|
+
case 'step': {
|
|
28581
|
+
var input = ev(e[1]), val = ev(e[2]);
|
|
28582
|
+
for (var st = 3; st < e.length; st += 2) { if (input >= e[st]) { val = ev(e[st + 1]); } else { break; } }
|
|
28583
|
+
return val;
|
|
28584
|
+
}
|
|
28585
|
+
case 'interpolate': {
|
|
28586
|
+
var at = ev(e[2]), stops = e.slice(3);
|
|
28587
|
+
if (at <= stops[0]) { return ev(stops[1]); }
|
|
28588
|
+
for (var i = 2; i < stops.length; i += 2) {
|
|
28589
|
+
if (at <= stops[i]) {
|
|
28590
|
+
var lo = ev(stops[i - 1]), hi = ev(stops[i]), t = (at - stops[i - 2]) / (stops[i] - stops[i - 2]);
|
|
28591
|
+
hi = ev(stops[i + 1]);
|
|
28592
|
+
if (typeof lo === 'number' && typeof hi === 'number') { return lo + t * (hi - lo); }
|
|
28593
|
+
return t < 0.5 ? lo : hi;
|
|
28594
|
+
}
|
|
28595
|
+
}
|
|
28596
|
+
return ev(stops[stops.length - 1]);
|
|
28597
|
+
}
|
|
28598
|
+
case '+': { var sum = 0; for (var u = 1; u < e.length; u++) { sum += ev(e[u]); } return sum; }
|
|
28599
|
+
case '*': { var prod = 1; for (var w = 1; w < e.length; w++) { prod *= ev(e[w]); } return prod; }
|
|
28600
|
+
case '-': return e.length === 2 ? -ev(e[1]) : ev(e[1]) - ev(e[2]);
|
|
28601
|
+
case '/': return ev(e[1]) / ev(e[2]);
|
|
28602
|
+
default:
|
|
28603
|
+
// A plain array value (an offset, a dash): every member evaluated.
|
|
28604
|
+
return e.map(ev);
|
|
28605
|
+
}
|
|
28606
|
+
}
|
|
28607
|
+
|
|
28608
|
+
// Legacy filters (['==', 'key', value]) are read as expressions too.
|
|
28609
|
+
function passes(filter, ctx) {
|
|
28610
|
+
if (!filter) { return true; }
|
|
28611
|
+
return !!evaluate(filter, ctx);
|
|
28612
|
+
}
|
|
28613
|
+
|
|
28614
|
+
// ------------------------------------------------------------------ clustering (a grid per zoom, like the engine's)
|
|
28615
|
+
|
|
28616
|
+
function clusterAt(features, zoom, radius, maxZoom) {
|
|
28617
|
+
var z = Math.floor(zoom);
|
|
28618
|
+
if (z > maxZoom) { return { features: features, clusters: {} }; }
|
|
28619
|
+
var scale = TILE * Math.pow(2, z), cell = radius;
|
|
28620
|
+
var grid = {}, out = [], clusters = {};
|
|
28621
|
+
features.forEach(function (f) {
|
|
28622
|
+
var g = f.geometry;
|
|
28623
|
+
if (!g || g.type !== 'Point') { out.push(f); return; }
|
|
28624
|
+
var x = lngX(g.coordinates[0], scale), y = latY(g.coordinates[1], scale);
|
|
28625
|
+
var key = Math.floor(x / cell) + ':' + Math.floor(y / cell);
|
|
28626
|
+
(grid[key] = grid[key] || []).push({ f: f, x: x, y: y });
|
|
28627
|
+
});
|
|
28628
|
+
var seq = 0;
|
|
28629
|
+
Object.keys(grid).forEach(function (key) {
|
|
28630
|
+
var members = grid[key];
|
|
28631
|
+
if (members.length === 1) { out.push(members[0].f); return; }
|
|
28632
|
+
var sx = 0, sy = 0;
|
|
28633
|
+
members.forEach(function (m) { sx += m.x; sy += m.y; });
|
|
28634
|
+
var id = z * 100000 + (++seq);
|
|
28635
|
+
var count = members.length;
|
|
28636
|
+
clusters[id] = { leaves: members.map(function (m) { return m.f; }), zoom: z };
|
|
28637
|
+
out.push({ type: 'Feature', id: id, properties: { cluster: true, cluster_id: id, point_count: count, point_count_abbreviated: count >= 1000 ? Math.round(count / 100) / 10 + 'k' : String(count) },
|
|
28638
|
+
geometry: { type: 'Point', coordinates: [xLng(sx / count, scale), yLat(sy / count, scale)] } });
|
|
28639
|
+
});
|
|
28640
|
+
return { features: out, clusters: clusters };
|
|
28641
|
+
}
|
|
28642
|
+
|
|
28643
|
+
function GeoJSONSource(map, id, spec) {
|
|
28644
|
+
this.map = map; this.id = id; this.type = 'geojson'; this.spec = spec;
|
|
28645
|
+
this.setData(spec.data);
|
|
28646
|
+
}
|
|
28647
|
+
GeoJSONSource.prototype.setData = function (data) {
|
|
28648
|
+
var fc = !data ? { type: 'FeatureCollection', features: [] } : (data.type === 'FeatureCollection' ? data : (data.type === 'Feature' ? { type: 'FeatureCollection', features: [data] } : { type: 'FeatureCollection', features: [{ type: 'Feature', properties: {}, geometry: data }] }));
|
|
28649
|
+
var spec = this.spec, idp = spec.promoteId;
|
|
28650
|
+
this.features = fc.features.map(function (f, i) {
|
|
28651
|
+
var id = f.id;
|
|
28652
|
+
if (spec.generateId) { id = i; } else if (idp && f.properties && f.properties[idp] !== undefined) { id = f.properties[idp]; }
|
|
28653
|
+
return id === f.id ? f : Object.assign({}, f, { id: id });
|
|
28654
|
+
});
|
|
28655
|
+
this._clusterZoom = null;
|
|
28656
|
+
this.map._changed();
|
|
28657
|
+
var self = this;
|
|
28658
|
+
setTimeout(function () { self.map._fire('sourcedata', { sourceId: self.id, isSourceLoaded: true, dataType: 'source' }); }, 0);
|
|
28659
|
+
return this;
|
|
28660
|
+
};
|
|
28661
|
+
GeoJSONSource.prototype._shown = function (zoom) {
|
|
28662
|
+
if (!this.spec.cluster) { return this.features; }
|
|
28663
|
+
var z = Math.floor(zoom);
|
|
28664
|
+
if (this._clusterZoom !== z) {
|
|
28665
|
+
var c = clusterAt(this.features, z, (this.spec.clusterRadius || 50) * 1, this.spec.clusterMaxZoom === undefined ? 14 : this.spec.clusterMaxZoom);
|
|
28666
|
+
this._cluster = c; this._clusterZoom = z;
|
|
28667
|
+
}
|
|
28668
|
+
return this._cluster.features;
|
|
28669
|
+
};
|
|
28670
|
+
GeoJSONSource.prototype.getClusterLeaves = function (id, limit, offset) {
|
|
28671
|
+
var c = this._cluster && this._cluster.clusters[id];
|
|
28672
|
+
var leaves = c ? c.leaves : [];
|
|
28673
|
+
return Promise.resolve(leaves.slice(offset || 0, (offset || 0) + (limit === undefined || limit === Infinity ? leaves.length : limit)));
|
|
28674
|
+
};
|
|
28675
|
+
GeoJSONSource.prototype.getClusterExpansionZoom = function (id) {
|
|
28676
|
+
var c = this._cluster && this._cluster.clusters[id];
|
|
28677
|
+
return Promise.resolve(c ? Math.min(c.zoom + 2, (this.spec.clusterMaxZoom === undefined ? 14 : this.spec.clusterMaxZoom) + 1) : this.map.getZoom() + 2);
|
|
28678
|
+
};
|
|
28679
|
+
|
|
28680
|
+
// Raster tiles: loaded as images, kept in a small cache, a parent tile drawn while a tile is on its way.
|
|
28681
|
+
function RasterSource(map, id, spec) {
|
|
28682
|
+
this.map = map; this.id = id; this.type = spec.type; this.spec = spec;
|
|
28683
|
+
this.tiles = {};
|
|
28684
|
+
this.order = [];
|
|
28685
|
+
}
|
|
28686
|
+
RasterSource.prototype._url = function (z, x, y) {
|
|
28687
|
+
var t = this.spec.tiles[(x + y) % this.spec.tiles.length];
|
|
28688
|
+
if (this.spec.scheme === 'tms') { y = Math.pow(2, z) - 1 - y; }
|
|
28689
|
+
return t.replace('{z}', z).replace('{x}', x).replace('{y}', y).replace('{ratio}', '');
|
|
28690
|
+
};
|
|
28691
|
+
RasterSource.prototype.tile = function (z, x, y) {
|
|
28692
|
+
var key = z + '/' + x + '/' + y, t = this.tiles[key];
|
|
28693
|
+
if (t) { return t; }
|
|
28694
|
+
var self = this, url = this._url(z, x, y);
|
|
28695
|
+
t = this.tiles[key] = { img: null, ready: false, failed: false };
|
|
28696
|
+
this.order.push(key);
|
|
28697
|
+
if (this.order.length > 400) { delete this.tiles[this.order.shift()]; }
|
|
28698
|
+
var doc = this.map._doc;
|
|
28699
|
+
var done = function (img) { t.img = img; t.ready = true; self.map._changed(); self.map._fire('sourcedata', { sourceId: self.id, isSourceLoaded: true, tile: { key: key } }); };
|
|
28700
|
+
var fail = function (err) { t.failed = true; self.map._fire('error', { type: 'error', sourceId: self.id, error: err || new Error('tile failed: ' + url) }); };
|
|
28701
|
+
var scheme = /^([a-z][a-z0-9+.-]*):\/\//i.exec(url);
|
|
28702
|
+
var proto = scheme && protocols[scheme[1].toLowerCase()];
|
|
28703
|
+
if (proto) {
|
|
28704
|
+
Promise.resolve(proto({ url: url, type: 'image' }, { signal: null })).then(function (r) {
|
|
28705
|
+
var blob = new Blob([r.data], { type: 'image/png' });
|
|
28706
|
+
var img = new (doc.defaultView.Image)();
|
|
28707
|
+
img.onload = function () { done(img); };
|
|
28708
|
+
img.onerror = function () { fail(); };
|
|
28709
|
+
img.src = URL.createObjectURL(blob);
|
|
28710
|
+
}, fail);
|
|
28711
|
+
return t;
|
|
28712
|
+
}
|
|
28713
|
+
var img = new (doc.defaultView.Image)();
|
|
28714
|
+
img.crossOrigin = 'anonymous';
|
|
28715
|
+
img.onload = function () { done(img); };
|
|
28716
|
+
img.onerror = function () { fail(); };
|
|
28717
|
+
var request = this.map._transform ? this.map._transform(url, 'Tile') : { url: url };
|
|
28718
|
+
img.src = request && request.url ? request.url : url;
|
|
28719
|
+
return t;
|
|
28720
|
+
};
|
|
28721
|
+
|
|
28722
|
+
var protocols = {};
|
|
28723
|
+
function addProtocol(name, fn) { protocols[String(name).toLowerCase()] = fn; }
|
|
28724
|
+
function removeProtocol(name) { delete protocols[String(name).toLowerCase()]; }
|
|
28725
|
+
|
|
28726
|
+
// ------------------------------------------------------------------ the map
|
|
28727
|
+
|
|
28728
|
+
var CORNERS = ['top-left', 'top-right', 'bottom-left', 'bottom-right'];
|
|
28729
|
+
|
|
28730
|
+
function Handler(map, name) { this.map = map; this.name = name; this.on = true; }
|
|
28731
|
+
Handler.prototype.enable = function () { this.on = true; };
|
|
28732
|
+
Handler.prototype.disable = function () { this.on = false; if (this.name === 'dragPan') { this.map._drag = null; } };
|
|
28733
|
+
Handler.prototype.isEnabled = function () { return this.on; };
|
|
28734
|
+
|
|
28735
|
+
/**
|
|
28736
|
+
* The canvas map. Options as the engine's: container, style ({ sources, layers } with raster sources), center,
|
|
28737
|
+
* zoom, minZoom, maxZoom, attributionControl, transformRequest.
|
|
28738
|
+
*/
|
|
28739
|
+
function CanvasMap(options) {
|
|
28740
|
+
var self = this;
|
|
28741
|
+
this._doc = null;
|
|
28742
|
+
var container = typeof options.container === 'string' ? document.getElementById(options.container) : options.container;
|
|
28743
|
+
if (!container) { throw new Error('sMap: no map container'); }
|
|
28744
|
+
this._doc = container.ownerDocument;
|
|
28745
|
+
this._container = container;
|
|
28746
|
+
this._transform = options.transformRequest || null;
|
|
28747
|
+
this._events = {};
|
|
28748
|
+
this._layerEvents = [];
|
|
28749
|
+
this._sources = {};
|
|
28750
|
+
this._layers = [];
|
|
28751
|
+
this._images = {};
|
|
28752
|
+
this._controls = [];
|
|
28753
|
+
this._center = toLngLat(options.center || [0, 0]);
|
|
28754
|
+
this._zoom = options.zoom || 0;
|
|
28755
|
+
this._minZoom = options.minZoom !== undefined ? options.minZoom : 0;
|
|
28756
|
+
this._maxZoom = options.maxZoom !== undefined ? options.maxZoom : 22;
|
|
28757
|
+
this._drawn = [];
|
|
28758
|
+
this._dirty = false;
|
|
28759
|
+
container.classList.add('maplibregl-map', 'smap-canvas-map');
|
|
28760
|
+
var canvasContainer = this._doc.createElement('div');
|
|
28761
|
+
canvasContainer.className = 'maplibregl-canvas-container maplibregl-interactive';
|
|
28762
|
+
var canvas = this._doc.createElement('canvas');
|
|
28763
|
+
canvas.className = 'maplibregl-canvas';
|
|
28764
|
+
canvas.setAttribute('tabindex', '0');
|
|
28765
|
+
canvas.style.position = 'absolute'; canvas.style.left = '0'; canvas.style.top = '0';
|
|
28766
|
+
canvasContainer.appendChild(canvas);
|
|
28767
|
+
container.appendChild(canvasContainer);
|
|
28768
|
+
var controls = this._doc.createElement('div');
|
|
28769
|
+
controls.className = 'maplibregl-control-container';
|
|
28770
|
+
this._corners = {};
|
|
28771
|
+
CORNERS.forEach(function (c) {
|
|
28772
|
+
var el = self._doc.createElement('div');
|
|
28773
|
+
el.className = 'maplibregl-ctrl-' + c;
|
|
28774
|
+
controls.appendChild(el);
|
|
28775
|
+
self._corners[c] = el;
|
|
28776
|
+
});
|
|
28777
|
+
container.appendChild(controls);
|
|
28778
|
+
this._canvas = canvas;
|
|
28779
|
+
this._canvasContainer = canvasContainer;
|
|
28780
|
+
this.dragPan = new Handler(this, 'dragPan');
|
|
28781
|
+
this.scrollZoom = new Handler(this, 'scrollZoom');
|
|
28782
|
+
this.boxZoom = new Handler(this, 'boxZoom');
|
|
28783
|
+
this.doubleClickZoom = new Handler(this, 'doubleClickZoom');
|
|
28784
|
+
this.touchZoomRotate = new Handler(this, 'touchZoomRotate');
|
|
28785
|
+
this.keyboard = new Handler(this, 'keyboard');
|
|
28786
|
+
this.dragRotate = new Handler(this, 'dragRotate');
|
|
28787
|
+
this._resize();
|
|
28788
|
+
this._input();
|
|
28789
|
+
if (options.attributionControl !== false) { this.addControl(new AttributionControl(options.attributionControl || {}), 'bottom-right'); }
|
|
28790
|
+
this.setStyle(options.style);
|
|
28791
|
+
var win = this._doc.defaultView;
|
|
28792
|
+
if (win && win.ResizeObserver) {
|
|
28793
|
+
this._ro = new win.ResizeObserver(function () { self.resize(); });
|
|
28794
|
+
this._ro.observe(container);
|
|
28795
|
+
} else if (win) { win.addEventListener('resize', function () { self.resize(); }); }
|
|
28796
|
+
setTimeout(function () { self._fire('load', {}); }, 0);
|
|
28797
|
+
}
|
|
28798
|
+
|
|
28799
|
+
CanvasMap.prototype.isCanvas = true;
|
|
28800
|
+
|
|
28801
|
+
// ---- events
|
|
28802
|
+
CanvasMap.prototype.on = function (type, layer, fn) {
|
|
28803
|
+
if (typeof layer === 'function') { (this._events[type] = this._events[type] || []).push(layer); return this; }
|
|
28804
|
+
this._layerEvents.push({ type: type, layer: layer, fn: fn, inside: false });
|
|
28805
|
+
return this;
|
|
28806
|
+
};
|
|
28807
|
+
CanvasMap.prototype.off = function (type, layer, fn) {
|
|
28808
|
+
if (typeof layer === 'function') { this._events[type] = (this._events[type] || []).filter(function (f) { return f !== layer; }); return this; }
|
|
28809
|
+
this._layerEvents = this._layerEvents.filter(function (h) { return !(h.type === type && h.layer === layer && h.fn === fn); });
|
|
28810
|
+
return this;
|
|
28811
|
+
};
|
|
28812
|
+
CanvasMap.prototype.once = function (type, fn) {
|
|
28813
|
+
var self = this;
|
|
28814
|
+
var wrapped = function (e) { self.off(type, wrapped); if (fn) { fn(e); } };
|
|
28815
|
+
if (!fn) { return new Promise(function (resolve) { fn = resolve; self.on(type, wrapped); }); }
|
|
28816
|
+
return this.on(type, wrapped);
|
|
28817
|
+
};
|
|
28818
|
+
CanvasMap.prototype.fire = function (e) { this._fire(e.type, e); return this; };
|
|
28819
|
+
CanvasMap.prototype._fire = function (type, data) {
|
|
28820
|
+
var e = data || {};
|
|
28821
|
+
e.type = type;
|
|
28822
|
+
e.target = this;
|
|
28823
|
+
(this._events[type] || []).slice().forEach(function (fn) { fn(e); });
|
|
28824
|
+
return e;
|
|
28825
|
+
};
|
|
28826
|
+
|
|
28827
|
+
// ---- camera
|
|
28828
|
+
CanvasMap.prototype._scale = function (z) { return TILE * Math.pow(2, z === undefined ? this._zoom : z); };
|
|
28829
|
+
CanvasMap.prototype.getContainer = function () { return this._container; };
|
|
28830
|
+
CanvasMap.prototype.getCanvas = function () { return this._canvas; };
|
|
28831
|
+
CanvasMap.prototype.getCanvasContainer = function () { return this._canvasContainer; };
|
|
28832
|
+
CanvasMap.prototype.getCenter = function () { return new LngLat(this._center.lng, this._center.lat); };
|
|
28833
|
+
CanvasMap.prototype.getZoom = function () { return this._zoom; };
|
|
28834
|
+
CanvasMap.prototype.getBearing = function () { return 0; };
|
|
28835
|
+
CanvasMap.prototype.getPitch = function () { return 0; };
|
|
28836
|
+
CanvasMap.prototype.getMinZoom = function () { return this._minZoom; };
|
|
28837
|
+
CanvasMap.prototype.getMaxZoom = function () { return this._maxZoom; };
|
|
28838
|
+
CanvasMap.prototype.setMinZoom = function (z) { this._minZoom = z; return this; };
|
|
28839
|
+
CanvasMap.prototype.setMaxZoom = function (z) { this._maxZoom = z; return this; };
|
|
28840
|
+
CanvasMap.prototype.project = function (v) {
|
|
28841
|
+
var p = toLngLat(v), s = this._scale();
|
|
28842
|
+
return { x: lngX(p.lng, s) - lngX(this._center.lng, s) + this._w / 2, y: latY(p.lat, s) - latY(this._center.lat, s) + this._h / 2 };
|
|
28843
|
+
};
|
|
28844
|
+
CanvasMap.prototype.unproject = function (pt) {
|
|
28845
|
+
var x = Array.isArray(pt) ? pt[0] : pt.x, y = Array.isArray(pt) ? pt[1] : pt.y, s = this._scale();
|
|
28846
|
+
return new LngLat(xLng(lngX(this._center.lng, s) + x - this._w / 2, s), yLat(latY(this._center.lat, s) + y - this._h / 2, s));
|
|
28847
|
+
};
|
|
28848
|
+
CanvasMap.prototype.getBounds = function () {
|
|
28849
|
+
var nw = this.unproject([0, 0]), se = this.unproject([this._w, this._h]);
|
|
28850
|
+
return new LngLatBounds([nw.lng, se.lat], [se.lng, nw.lat]);
|
|
28851
|
+
};
|
|
28852
|
+
CanvasMap.prototype._set = function (center, zoom) {
|
|
28853
|
+
var zoomed = zoom !== undefined && zoom !== this._zoom;
|
|
28854
|
+
if (center) { var c = toLngLat(center); this._center = { lng: ((c.lng + 540) % 360) - 180, lat: clampLat(c.lat) }; }
|
|
28855
|
+
if (zoom !== undefined) { this._zoom = Math.max(this._minZoom, Math.min(this._maxZoom, zoom)); }
|
|
28856
|
+
this._changed(true);
|
|
28857
|
+
this._fire('move', {});
|
|
28858
|
+
if (zoomed) { this._fire('zoom', {}); }
|
|
28859
|
+
};
|
|
28860
|
+
CanvasMap.prototype._end = function (zoomed) { this._fire('moveend', {}); if (zoomed) { this._fire('zoomend', {}); } this._fire('idle', {}); };
|
|
28861
|
+
CanvasMap.prototype.jumpTo = function (o) {
|
|
28862
|
+
var z0 = this._zoom;
|
|
28863
|
+
this._set(o.center, o.zoom);
|
|
28864
|
+
this._end(this._zoom !== z0);
|
|
28865
|
+
return this;
|
|
28866
|
+
};
|
|
28867
|
+
CanvasMap.prototype.easeTo = function (o) {
|
|
28868
|
+
var self = this, from = this.getCenter(), z0 = this._zoom;
|
|
28869
|
+
var to = o.center ? toLngLat(o.center) : from, z1 = o.zoom !== undefined ? o.zoom : z0;
|
|
28870
|
+
if (o.offset) { var p = this.project(to); to = this.unproject([p.x - o.offset[0], p.y - o.offset[1]]); }
|
|
28871
|
+
var duration = o.duration === undefined ? 300 : o.duration, start = null, seq = ++this._anim;
|
|
28872
|
+
if (!duration) { return this.jumpTo({ center: to, zoom: z1 }); }
|
|
28873
|
+
var step = function (now) {
|
|
28874
|
+
if (seq !== self._anim) { return; }
|
|
28875
|
+
if (start === null) { start = now; }
|
|
28876
|
+
var t = Math.min(1, (now - start) / duration), k = 1 - Math.pow(1 - t, 3);
|
|
28877
|
+
// Zoom moves evenly in its own scale; the centre follows in world pixels of the zoom reached.
|
|
28878
|
+
var z = z0 + (z1 - z0) * k;
|
|
28879
|
+
var s = TILE * Math.pow(2, z);
|
|
28880
|
+
var x = lngX(from.lng, s) + (lngX(to.lng, s) - lngX(from.lng, s)) * k, y = latY(from.lat, s) + (latY(to.lat, s) - latY(from.lat, s)) * k;
|
|
28881
|
+
self._set([xLng(x, s), yLat(y, s)], z);
|
|
28882
|
+
if (t < 1) { raf(step); } else { self._end(z1 !== z0); }
|
|
28883
|
+
};
|
|
28884
|
+
raf(step);
|
|
28885
|
+
return this;
|
|
28886
|
+
};
|
|
28887
|
+
CanvasMap.prototype._anim = 0;
|
|
28888
|
+
CanvasMap.prototype.flyTo = function (o) { return this.easeTo(Object.assign({ duration: o.duration === undefined ? 800 : o.duration }, o)); };
|
|
28889
|
+
CanvasMap.prototype.panBy = function (offset, o) {
|
|
28890
|
+
var c = this.project(this._center);
|
|
28891
|
+
return this.easeTo(Object.assign({ center: this.unproject([c.x + offset[0], c.y + offset[1]]) }, o || {}));
|
|
28892
|
+
};
|
|
28893
|
+
CanvasMap.prototype.panTo = function (center, o) { return this.easeTo(Object.assign({ center: center }, o || {})); };
|
|
28894
|
+
CanvasMap.prototype.zoomTo = function (z, o) { return this.easeTo(Object.assign({ zoom: z }, o || {})); };
|
|
28895
|
+
CanvasMap.prototype.zoomIn = function (o) { return this.zoomTo(this._zoom + 1, o); };
|
|
28896
|
+
CanvasMap.prototype.zoomOut = function (o) { return this.zoomTo(this._zoom - 1, o); };
|
|
28897
|
+
CanvasMap.prototype.setCenter = function (c) { return this.jumpTo({ center: c }); };
|
|
28898
|
+
CanvasMap.prototype.setZoom = function (z) { return this.jumpTo({ zoom: z }); };
|
|
28899
|
+
CanvasMap.prototype.cameraForBounds = function (bounds, o) {
|
|
28900
|
+
var b = bounds instanceof LngLatBounds ? bounds : new LngLatBounds(bounds);
|
|
28901
|
+
if (b.isEmpty()) { return null; }
|
|
28902
|
+
o = o || {};
|
|
28903
|
+
var pad = typeof o.padding === 'number' ? { top: o.padding, bottom: o.padding, left: o.padding, right: o.padding } : Object.assign({ top: 0, bottom: 0, left: 0, right: 0 }, o.padding || {});
|
|
28904
|
+
var w = Math.max(1, this._w - pad.left - pad.right), h = Math.max(1, this._h - pad.top - pad.bottom);
|
|
28905
|
+
var s1 = TILE, dx = Math.abs(lngX(b.getEast(), s1) - lngX(b.getWest(), s1)), dy = Math.abs(latY(b.getSouth(), s1) - latY(b.getNorth(), s1));
|
|
28906
|
+
var z = Math.min(Math.log2(w / Math.max(dx, 1e-9)), Math.log2(h / Math.max(dy, 1e-9)));
|
|
28907
|
+
z = Math.min(z, o.maxZoom !== undefined ? o.maxZoom : this._maxZoom);
|
|
28908
|
+
var c = b.getCenter(), s = TILE * Math.pow(2, z);
|
|
28909
|
+
// The padding moves the centre.
|
|
28910
|
+
var cx = lngX(c.lng, s) - (pad.left - pad.right) / 2, cy = latY(c.lat, s) - (pad.top - pad.bottom) / 2;
|
|
28911
|
+
return { center: [xLng(cx, s), yLat(cy, s)], zoom: z };
|
|
28912
|
+
};
|
|
28913
|
+
CanvasMap.prototype.fitBounds = function (bounds, o) {
|
|
28914
|
+
var cam = this.cameraForBounds(bounds, o);
|
|
28915
|
+
if (!cam) { return this; }
|
|
28916
|
+
return this.easeTo(Object.assign({}, cam, { duration: o && o.duration !== undefined ? o.duration : (o && o.animate === false ? 0 : 300) }));
|
|
28917
|
+
};
|
|
28918
|
+
CanvasMap.prototype.setProjection = function () { return this; };
|
|
28919
|
+
CanvasMap.prototype.getProjection = function () { return { type: 'mercator' }; };
|
|
28920
|
+
CanvasMap.prototype.setTerrain = function (t) { if (t) { this._unsupported('terrain'); } return this; };
|
|
28921
|
+
CanvasMap.prototype._unsupported = function (what) {
|
|
28922
|
+
this._said = this._said || {};
|
|
28923
|
+
if (this._said[what]) { return; }
|
|
28924
|
+
this._said[what] = true;
|
|
28925
|
+
this._fire('smap.unsupported', { feature: what });
|
|
28926
|
+
};
|
|
28927
|
+
|
|
28928
|
+
CanvasMap.prototype.resize = function () { if (this._resize()) { this._changed(true); this._fire('resize', {}); this._fire('moveend', {}); } return this; };
|
|
28929
|
+
CanvasMap.prototype._resize = function () {
|
|
28930
|
+
var w = this._container.clientWidth || 300, h = this._container.clientHeight || 150;
|
|
28931
|
+
var win = this._doc.defaultView || {}, ratio = Math.min(3, win.devicePixelRatio || 1);
|
|
28932
|
+
if (w === this._w && h === this._h && ratio === this._ratio) { return false; }
|
|
28933
|
+
this._w = w; this._h = h; this._ratio = ratio;
|
|
28934
|
+
this._canvas.width = Math.round(w * ratio); this._canvas.height = Math.round(h * ratio);
|
|
28935
|
+
this._canvas.style.width = w + 'px'; this._canvas.style.height = h + 'px';
|
|
28936
|
+
return true;
|
|
28937
|
+
};
|
|
28938
|
+
CanvasMap.prototype.triggerRepaint = function () { this._changed(); };
|
|
28939
|
+
CanvasMap.prototype._changed = function () {
|
|
28940
|
+
if (this._dirty || this._removed) { return; }
|
|
28941
|
+
this._dirty = true;
|
|
28942
|
+
var self = this;
|
|
28943
|
+
raf(function () { self._dirty = false; self._render(); });
|
|
28944
|
+
};
|
|
28945
|
+
CanvasMap.prototype.loaded = function () { return true; };
|
|
28946
|
+
CanvasMap.prototype.isStyleLoaded = function () { return true; };
|
|
28947
|
+
CanvasMap.prototype.remove = function () {
|
|
28948
|
+
this._removed = true;
|
|
28949
|
+
if (this._ro) { this._ro.disconnect(); }
|
|
28950
|
+
if (this._canvasContainer.parentNode) { this._canvasContainer.parentNode.removeChild(this._canvasContainer); }
|
|
28951
|
+
var cc = this._container.querySelector('.maplibregl-control-container');
|
|
28952
|
+
if (cc) { cc.parentNode.removeChild(cc); }
|
|
28953
|
+
this._fire('remove', {});
|
|
28954
|
+
};
|
|
28955
|
+
|
|
28956
|
+
// ---- style, sources, layers
|
|
28957
|
+
CanvasMap.prototype.setStyle = function (style) {
|
|
28958
|
+
var self = this;
|
|
28959
|
+
this._sources = {};
|
|
28960
|
+
this._layers = [];
|
|
28961
|
+
style = style && typeof style === 'object' ? style : { sources: {}, layers: [] };
|
|
28962
|
+
Object.keys(style.sources || {}).forEach(function (id) { self.addSource(id, style.sources[id]); });
|
|
28963
|
+
(style.layers || []).forEach(function (l) { self.addLayer(l); });
|
|
28964
|
+
this._changed();
|
|
28965
|
+
setTimeout(function () { self._fire('style.load', {}); self._fire('styledata', {}); }, 0);
|
|
28966
|
+
return this;
|
|
28967
|
+
};
|
|
28968
|
+
CanvasMap.prototype.getStyle = function () {
|
|
28969
|
+
var self = this, sources = {};
|
|
28970
|
+
Object.keys(this._sources).forEach(function (id) { sources[id] = self._sources[id].spec; });
|
|
28971
|
+
return { version: 8, sources: sources, layers: this._layers.map(function (l) { return l; }) };
|
|
28972
|
+
};
|
|
28973
|
+
CanvasMap.prototype.addSource = function (id, spec) {
|
|
28974
|
+
if (this._sources[id]) { throw new Error('There is already a source with this ID'); }
|
|
28975
|
+
if (spec.type === 'geojson') { this._sources[id] = new GeoJSONSource(this, id, spec); }
|
|
28976
|
+
else if (spec.type === 'raster' && spec.tiles) { this._sources[id] = new RasterSource(this, id, spec); }
|
|
28977
|
+
else { this._sources[id] = { id: id, type: spec.type, spec: spec, setData: function () {} }; if (spec.type !== 'raster-dem') { this._unsupported('source:' + spec.type); } }
|
|
28978
|
+
this._updateAttribution();
|
|
28979
|
+
return this;
|
|
28980
|
+
};
|
|
28981
|
+
CanvasMap.prototype.getSource = function (id) { return this._sources[id]; };
|
|
28982
|
+
CanvasMap.prototype.removeSource = function (id) { delete this._sources[id]; this._updateAttribution(); this._changed(); return this; };
|
|
28983
|
+
CanvasMap.prototype.addLayer = function (layer, before) {
|
|
28984
|
+
if (this.getLayer(layer.id)) { throw new Error('Layer with id "' + layer.id + '" already exists'); }
|
|
28985
|
+
var l = layer.type === 'custom' ? layer : JSON.parse(JSON.stringify(layer));
|
|
28986
|
+
if (typeof l.source === 'object' && l.source) { this.addSource(l.id, l.source); l.source = l.id; }
|
|
28987
|
+
if (['raster', 'fill', 'line', 'circle', 'symbol', 'heatmap', 'background', 'fill-extrusion'].indexOf(l.type) < 0) { this._unsupported('layer:' + l.type); }
|
|
28988
|
+
var at = before ? this._layers.findIndex(function (x) { return x.id === before; }) : -1;
|
|
28989
|
+
if (at >= 0) { this._layers.splice(at, 0, l); } else { this._layers.push(l); }
|
|
28990
|
+
if (l.type === 'custom' && l.onAdd) { this._unsupported('3d'); }
|
|
28991
|
+
this._changed();
|
|
28992
|
+
return this;
|
|
28993
|
+
};
|
|
28994
|
+
CanvasMap.prototype.getLayer = function (id) { for (var i = 0; i < this._layers.length; i++) { if (this._layers[i].id === id) { return this._layers[i]; } } return undefined; };
|
|
28995
|
+
CanvasMap.prototype.removeLayer = function (id) { this._layers = this._layers.filter(function (l) { return l.id !== id; }); this._changed(); return this; };
|
|
28996
|
+
CanvasMap.prototype.moveLayer = function (id, before) {
|
|
28997
|
+
var l = this.getLayer(id);
|
|
28998
|
+
if (!l) { return this; }
|
|
28999
|
+
this._layers = this._layers.filter(function (x) { return x !== l; });
|
|
29000
|
+
var at = before ? this._layers.findIndex(function (x) { return x.id === before; }) : -1;
|
|
29001
|
+
if (at >= 0) { this._layers.splice(at, 0, l); } else { this._layers.push(l); }
|
|
29002
|
+
this._changed();
|
|
29003
|
+
return this;
|
|
29004
|
+
};
|
|
29005
|
+
CanvasMap.prototype.setLayoutProperty = function (id, k, v) { var l = this.getLayer(id); if (l) { l.layout = Object.assign({}, l.layout, {}); l.layout[k] = v; this._changed(); } return this; };
|
|
29006
|
+
CanvasMap.prototype.getLayoutProperty = function (id, k) { var l = this.getLayer(id); return l && l.layout ? l.layout[k] : undefined; };
|
|
29007
|
+
CanvasMap.prototype.setPaintProperty = function (id, k, v) { var l = this.getLayer(id); if (l) { l.paint = Object.assign({}, l.paint, {}); l.paint[k] = v; this._changed(); } return this; };
|
|
29008
|
+
CanvasMap.prototype.getPaintProperty = function (id, k) { var l = this.getLayer(id); return l && l.paint ? l.paint[k] : undefined; };
|
|
29009
|
+
CanvasMap.prototype.setFilter = function (id, f) { var l = this.getLayer(id); if (l) { l.filter = f || undefined; this._changed(); } return this; };
|
|
29010
|
+
CanvasMap.prototype.getFilter = function (id) { var l = this.getLayer(id); return l ? l.filter : undefined; };
|
|
29011
|
+
CanvasMap.prototype.setLayerZoomRange = function (id, min, max) { var l = this.getLayer(id); if (l) { l.minzoom = min; l.maxzoom = max; this._changed(); } return this; };
|
|
29012
|
+
|
|
29013
|
+
// ---- images (the SDK's drawn pins, numbers, badges and arrows; icons)
|
|
29014
|
+
CanvasMap.prototype.hasImage = function (id) { return !!this._images[id]; };
|
|
29015
|
+
CanvasMap.prototype.addImage = function (id, image, o) {
|
|
29016
|
+
var ratio = (o && o.pixelRatio) || 1, canvas;
|
|
29017
|
+
if (image && image.data && image.width !== undefined && !(image.getContext)) {
|
|
29018
|
+
canvas = this._doc.createElement('canvas');
|
|
29019
|
+
canvas.width = image.width; canvas.height = image.height;
|
|
29020
|
+
var ctx = canvas.getContext('2d');
|
|
29021
|
+
var data = ctx.createImageData(image.width, image.height);
|
|
29022
|
+
data.data.set(image.data);
|
|
29023
|
+
ctx.putImageData(data, 0, 0);
|
|
29024
|
+
} else { canvas = image; }
|
|
29025
|
+
this._images[id] = { source: canvas, ratio: ratio, w: (canvas.width || canvas.naturalWidth) / ratio, h: (canvas.height || canvas.naturalHeight) / ratio };
|
|
29026
|
+
this._changed();
|
|
29027
|
+
return this;
|
|
29028
|
+
};
|
|
29029
|
+
CanvasMap.prototype.removeImage = function (id) { delete this._images[id]; return this; };
|
|
29030
|
+
CanvasMap.prototype.loadImage = function (url) {
|
|
29031
|
+
var doc = this._doc;
|
|
29032
|
+
return new Promise(function (resolve, reject) {
|
|
29033
|
+
var img = new (doc.defaultView.Image)();
|
|
29034
|
+
img.crossOrigin = 'anonymous';
|
|
29035
|
+
img.onload = function () { resolve({ data: img }); };
|
|
29036
|
+
img.onerror = function () { reject(new Error('sMap: image failed ' + url)); };
|
|
29037
|
+
img.src = url;
|
|
29038
|
+
});
|
|
29039
|
+
};
|
|
29040
|
+
CanvasMap.prototype.setMissingStyleImageResolver = function (fn) { this._missing = fn; return this; };
|
|
29041
|
+
CanvasMap.prototype._image = function (id) {
|
|
29042
|
+
if (!this._images[id] && this._missing) { this._missing(id); }
|
|
29043
|
+
return this._images[id] || null;
|
|
29044
|
+
};
|
|
29045
|
+
|
|
29046
|
+
// ---- controls
|
|
29047
|
+
CanvasMap.prototype.addControl = function (control, position) {
|
|
29048
|
+
var pos = position || (control.getDefaultPosition && control.getDefaultPosition()) || 'top-right';
|
|
29049
|
+
var el = control.onAdd(this);
|
|
29050
|
+
var corner = this._corners[pos];
|
|
29051
|
+
if (pos.indexOf('bottom') === 0) { corner.insertBefore(el, corner.firstChild); } else { corner.appendChild(el); }
|
|
29052
|
+
this._controls.push(control);
|
|
29053
|
+
return this;
|
|
29054
|
+
};
|
|
29055
|
+
CanvasMap.prototype.removeControl = function (control) {
|
|
29056
|
+
this._controls = this._controls.filter(function (c) { return c !== control; });
|
|
29057
|
+
if (control.onRemove) { control.onRemove(this); }
|
|
29058
|
+
return this;
|
|
29059
|
+
};
|
|
29060
|
+
CanvasMap.prototype.hasControl = function (control) { return this._controls.indexOf(control) >= 0; };
|
|
29061
|
+
CanvasMap.prototype._updateAttribution = function () {
|
|
29062
|
+
var self = this, seen = {}, parts = [];
|
|
29063
|
+
Object.keys(this._sources).forEach(function (id) { var a = self._sources[id].spec && self._sources[id].spec.attribution; if (a && !seen[a]) { seen[a] = true; parts.push(a); } });
|
|
29064
|
+
this._attribution = parts.join(' | ');
|
|
29065
|
+
this._controls.forEach(function (c) { if (c instanceof AttributionControl) { c._update(); } });
|
|
29066
|
+
};
|
|
29067
|
+
|
|
29068
|
+
// ---- drawing
|
|
29069
|
+
|
|
29070
|
+
CanvasMap.prototype._visible = function (l) {
|
|
29071
|
+
if (l.layout && l.layout.visibility === 'none') { return false; }
|
|
29072
|
+
if (l.minzoom !== undefined && this._zoom < l.minzoom) { return false; }
|
|
29073
|
+
if (l.maxzoom !== undefined && this._zoom >= l.maxzoom) { return false; }
|
|
29074
|
+
return true;
|
|
29075
|
+
};
|
|
29076
|
+
|
|
29077
|
+
CanvasMap.prototype._render = function () {
|
|
29078
|
+
var ctx = this._canvas.getContext('2d');
|
|
29079
|
+
if (!ctx) { return; }
|
|
29080
|
+
var r = this._ratio;
|
|
29081
|
+
ctx.setTransform(r, 0, 0, r, 0, 0);
|
|
29082
|
+
ctx.clearRect(0, 0, this._w, this._h);
|
|
29083
|
+
this._drawn = [];
|
|
29084
|
+
var self = this, zoom = this._zoom;
|
|
29085
|
+
// Raster layers first (the street map, imagery), then every other layer in its order.
|
|
29086
|
+
var rasters = this._layers.filter(function (l) { return l.type === 'raster' || l.type === 'background'; });
|
|
29087
|
+
var others = this._layers.filter(function (l) { return l.type !== 'raster' && l.type !== 'background'; });
|
|
29088
|
+
rasters.concat(others).forEach(function (l) {
|
|
29089
|
+
if (!self._visible(l)) { return; }
|
|
29090
|
+
if (l.type === 'background') { ctx.fillStyle = evaluate((l.paint || {})['background-color'], { zoom: zoom }) || '#ffffff'; ctx.fillRect(0, 0, self._w, self._h); return; }
|
|
29091
|
+
if (l.type === 'raster') { self._drawRaster(ctx, l); return; }
|
|
29092
|
+
var src = self._sources[l.source];
|
|
29093
|
+
if (!src || !src._shown) { return; }
|
|
29094
|
+
var features = src._shown(zoom);
|
|
29095
|
+
if (l.type === 'heatmap') { self._drawHeatmap(ctx, l, features); return; }
|
|
29096
|
+
for (var i = 0; i < features.length; i++) {
|
|
29097
|
+
var f = features[i], c = { zoom: zoom, f: f };
|
|
29098
|
+
if (!f.geometry || !passes(l.filter, c)) { continue; }
|
|
29099
|
+
if (l.type === 'fill' || l.type === 'fill-extrusion') { self._drawFill(ctx, l, f, c); }
|
|
29100
|
+
else if (l.type === 'line') { self._drawLine(ctx, l, f, c); }
|
|
29101
|
+
else if (l.type === 'circle') { self._drawCircle(ctx, l, f, c); }
|
|
29102
|
+
else if (l.type === 'symbol') { self._drawSymbol(ctx, l, f, c); }
|
|
29103
|
+
}
|
|
29104
|
+
});
|
|
29105
|
+
this._fire('render', {});
|
|
29106
|
+
};
|
|
29107
|
+
|
|
29108
|
+
CanvasMap.prototype._drawRaster = function (ctx, l) {
|
|
29109
|
+
var src = this._sources[l.source];
|
|
29110
|
+
if (!src || !src.tile) { return; }
|
|
29111
|
+
var spec = src.spec, ts = spec.tileSize || 256;
|
|
29112
|
+
// The tile zoom whose tiles are closest to their own size at this zoom.
|
|
29113
|
+
var tz = Math.round(this._zoom + Math.log2(TILE / ts));
|
|
29114
|
+
tz = Math.max(spec.minzoom || 0, Math.min(tz, spec.maxzoom === undefined ? 22 : spec.maxzoom));
|
|
29115
|
+
var n = Math.pow(2, tz), size = this._scale() / n;
|
|
29116
|
+
var s = this._scale();
|
|
29117
|
+
var ox = lngX(this._center.lng, s) - this._w / 2, oy = latY(this._center.lat, s) - this._h / 2;
|
|
29118
|
+
var x0 = Math.floor(ox / size), x1 = Math.floor((ox + this._w) / size), y0 = Math.max(0, Math.floor(oy / size)), y1 = Math.min(n - 1, Math.floor((oy + this._h) / size));
|
|
29119
|
+
var opacity = (l.paint && l.paint['raster-opacity'] !== undefined) ? l.paint['raster-opacity'] : 1;
|
|
29120
|
+
ctx.save();
|
|
29121
|
+
ctx.globalAlpha = opacity;
|
|
29122
|
+
ctx.imageSmoothingEnabled = true;
|
|
29123
|
+
for (var x = x0; x <= x1; x++) {
|
|
29124
|
+
for (var y = y0; y <= y1; y++) {
|
|
29125
|
+
var wx = ((x % n) + n) % n;
|
|
29126
|
+
var px = x * size - ox, py = y * size - oy;
|
|
29127
|
+
var t = src.tile(tz, wx, y);
|
|
29128
|
+
if (t.ready) { ctx.drawImage(t.img, px, py, size + 0.5, size + 0.5); continue; }
|
|
29129
|
+
// A parent tile already loaded, enlarged, while this one is on its way.
|
|
29130
|
+
for (var up = 1; up <= 4 && tz - up >= 0; up++) {
|
|
29131
|
+
var pz = tz - up, k = Math.pow(2, up), pt = src.tiles[pz + '/' + Math.floor(wx / k) + '/' + Math.floor(y / k)];
|
|
29132
|
+
if (pt && pt.ready) {
|
|
29133
|
+
var sw = pt.img.width / k, sx = (wx % k) * sw, sy = (y % k) * (pt.img.height / k);
|
|
29134
|
+
ctx.drawImage(pt.img, sx, sy, sw, pt.img.height / k, px, py, size + 0.5, size + 0.5);
|
|
29135
|
+
break;
|
|
29136
|
+
}
|
|
29137
|
+
}
|
|
29138
|
+
}
|
|
29139
|
+
}
|
|
29140
|
+
ctx.restore();
|
|
29141
|
+
};
|
|
29142
|
+
|
|
29143
|
+
// Screen points of a geometry's parts: [{ type, rings: [[[x, y], …]] }].
|
|
29144
|
+
CanvasMap.prototype._screen = function (g) {
|
|
29145
|
+
var self = this, s = this._scale();
|
|
29146
|
+
var ox = lngX(this._center.lng, s) - this._w / 2, oy = latY(this._center.lat, s) - this._h / 2;
|
|
29147
|
+
var pt = function (c) { return [lngX(c[0], s) - ox, latY(c[1], s) - oy]; };
|
|
29148
|
+
var line = function (cs) { return cs.map(pt); };
|
|
29149
|
+
switch (g.type) {
|
|
29150
|
+
case 'Point': return [pt(g.coordinates)];
|
|
29151
|
+
case 'MultiPoint': return g.coordinates.map(pt);
|
|
29152
|
+
case 'LineString': return [line(g.coordinates)];
|
|
29153
|
+
case 'MultiLineString': return g.coordinates.map(line);
|
|
29154
|
+
case 'Polygon': return [g.coordinates.map(line)];
|
|
29155
|
+
case 'MultiPolygon': return g.coordinates.map(function (p) { return p.map(line); });
|
|
29156
|
+
default: return [];
|
|
29157
|
+
}
|
|
29158
|
+
};
|
|
29159
|
+
|
|
29160
|
+
function paintOf(l, k, c, d) { var v = l.paint ? l.paint[k] : undefined; v = v === undefined ? d : evaluate(v, c); return v === undefined || v === null ? d : v; }
|
|
29161
|
+
function layoutOf(l, k, c, d) { var v = l.layout ? l.layout[k] : undefined; v = v === undefined ? d : evaluate(v, c); return v === undefined || v === null ? d : v; }
|
|
29162
|
+
|
|
29163
|
+
CanvasMap.prototype._record = function (l, f, hit) { this._drawn.push({ layer: l, f: f, hit: hit }); };
|
|
29164
|
+
|
|
29165
|
+
CanvasMap.prototype._drawFill = function (ctx, l, f, c) {
|
|
29166
|
+
var g = f.geometry;
|
|
29167
|
+
if (g.type !== 'Polygon' && g.type !== 'MultiPolygon') { return; }
|
|
29168
|
+
var polys = this._screen(g);
|
|
29169
|
+
ctx.save();
|
|
29170
|
+
ctx.globalAlpha = paintOf(l, l.type === 'fill' ? 'fill-opacity' : 'fill-extrusion-opacity', c, 1);
|
|
29171
|
+
ctx.fillStyle = paintOf(l, l.type === 'fill' ? 'fill-color' : 'fill-extrusion-color', c, '#000000');
|
|
29172
|
+
ctx.beginPath();
|
|
29173
|
+
polys.forEach(function (rings) { rings.forEach(function (ring) { ring.forEach(function (p, i) { if (i) { ctx.lineTo(p[0], p[1]); } else { ctx.moveTo(p[0], p[1]); } }); ctx.closePath(); }); });
|
|
29174
|
+
ctx.fill('evenodd');
|
|
29175
|
+
ctx.restore();
|
|
29176
|
+
this._record(l, f, { kind: 'fill', polys: polys });
|
|
29177
|
+
};
|
|
29178
|
+
|
|
29179
|
+
CanvasMap.prototype._drawLine = function (ctx, l, f, c) {
|
|
29180
|
+
var g = f.geometry, lines;
|
|
29181
|
+
if (g.type === 'LineString' || g.type === 'MultiLineString') { lines = this._screen(g); }
|
|
29182
|
+
else if (g.type === 'Polygon' || g.type === 'MultiPolygon') { lines = []; this._screen(g).forEach(function (rings) { rings.forEach(function (r) { lines.push(r); }); }); }
|
|
29183
|
+
else { return; }
|
|
29184
|
+
var width = paintOf(l, 'line-width', c, 1);
|
|
29185
|
+
ctx.save();
|
|
29186
|
+
ctx.globalAlpha = paintOf(l, 'line-opacity', c, 1);
|
|
29187
|
+
ctx.strokeStyle = paintOf(l, 'line-color', c, '#000000');
|
|
29188
|
+
ctx.lineWidth = width;
|
|
29189
|
+
ctx.lineCap = layoutOf(l, 'line-cap', c, 'butt');
|
|
29190
|
+
ctx.lineJoin = layoutOf(l, 'line-join', c, 'miter');
|
|
29191
|
+
var dash = paintOf(l, 'line-dasharray', c, null);
|
|
29192
|
+
if (Array.isArray(dash)) { ctx.setLineDash(dash.map(function (d) { return d * width; })); }
|
|
29193
|
+
ctx.beginPath();
|
|
29194
|
+
lines.forEach(function (line) { line.forEach(function (p, i) { if (i) { ctx.lineTo(p[0], p[1]); } else { ctx.moveTo(p[0], p[1]); } }); });
|
|
29195
|
+
ctx.stroke();
|
|
29196
|
+
ctx.restore();
|
|
29197
|
+
this._record(l, f, { kind: 'line', lines: lines, width: width });
|
|
29198
|
+
};
|
|
29199
|
+
|
|
29200
|
+
CanvasMap.prototype._drawCircle = function (ctx, l, f, c) {
|
|
29201
|
+
var self = this, radius = paintOf(l, 'circle-radius', c, 5), sw = paintOf(l, 'circle-stroke-width', c, 0);
|
|
29202
|
+
this._screen(f.geometry).forEach(function (p) {
|
|
29203
|
+
if (!Array.isArray(p) || typeof p[0] !== 'number') { return; }
|
|
29204
|
+
if (p[0] < -radius - 50 || p[1] < -radius - 50 || p[0] > self._w + radius + 50 || p[1] > self._h + radius + 50) { return; }
|
|
29205
|
+
ctx.save();
|
|
29206
|
+
ctx.globalAlpha = paintOf(l, 'circle-opacity', c, 1);
|
|
29207
|
+
ctx.beginPath();
|
|
29208
|
+
ctx.arc(p[0], p[1], radius, 0, Math.PI * 2);
|
|
29209
|
+
ctx.fillStyle = paintOf(l, 'circle-color', c, '#000000');
|
|
29210
|
+
ctx.fill();
|
|
29211
|
+
if (sw > 0) { ctx.lineWidth = sw; ctx.strokeStyle = paintOf(l, 'circle-stroke-color', c, '#000000'); ctx.globalAlpha = paintOf(l, 'circle-stroke-opacity', c, 1); ctx.stroke(); }
|
|
29212
|
+
ctx.restore();
|
|
29213
|
+
self._record(l, f, { kind: 'circle', x: p[0], y: p[1], r: radius + sw / 2 });
|
|
29214
|
+
});
|
|
29215
|
+
};
|
|
29216
|
+
|
|
29217
|
+
var ANCHOR_SHIFT = {
|
|
29218
|
+
center: [-0.5, -0.5], top: [-0.5, 0], bottom: [-0.5, -1], left: [0, -0.5], right: [-1, -0.5],
|
|
29219
|
+
'top-left': [0, 0], 'top-right': [-1, 0], 'bottom-left': [0, -1], 'bottom-right': [-1, -1],
|
|
29220
|
+
};
|
|
29221
|
+
|
|
29222
|
+
CanvasMap.prototype._drawSymbol = function (ctx, l, f, c) {
|
|
29223
|
+
var self = this;
|
|
29224
|
+
var placement = layoutOf(l, 'symbol-placement', c, 'point');
|
|
29225
|
+
var iconId = layoutOf(l, 'icon-image', c, null);
|
|
29226
|
+
var text = layoutOf(l, 'text-field', c, null);
|
|
29227
|
+
if (Array.isArray(text)) { text = text.join(''); }
|
|
29228
|
+
if (placement === 'line') {
|
|
29229
|
+
var g = f.geometry;
|
|
29230
|
+
if (g.type !== 'LineString' && g.type !== 'MultiLineString') { return; }
|
|
29231
|
+
var spacing = layoutOf(l, 'symbol-spacing', c, 250);
|
|
29232
|
+
this._screen(g).forEach(function (line) {
|
|
29233
|
+
var walked = spacing / 2;
|
|
29234
|
+
for (var i = 1; i < line.length; i++) {
|
|
29235
|
+
var a = line[i - 1], b = line[i], len = Math.sqrt(Math.pow(b[0] - a[0], 2) + Math.pow(b[1] - a[1], 2));
|
|
29236
|
+
while (walked <= len) {
|
|
29237
|
+
var t = walked / len, x = a[0] + (b[0] - a[0]) * t, y = a[1] + (b[1] - a[1]) * t;
|
|
29238
|
+
var angle = Math.atan2(b[1] - a[1], b[0] - a[0]);
|
|
29239
|
+
if (x > -50 && y > -50 && x < self._w + 50 && y < self._h + 50) {
|
|
29240
|
+
if (iconId) { self._icon(ctx, l, c, iconId, x, y, angle); }
|
|
29241
|
+
if (text) { self._text(ctx, l, c, String(text), x, y, angle > Math.PI / 2 || angle < -Math.PI / 2 ? angle + Math.PI : angle); }
|
|
29242
|
+
}
|
|
29243
|
+
walked += spacing;
|
|
29244
|
+
}
|
|
29245
|
+
walked -= len;
|
|
29246
|
+
}
|
|
29247
|
+
});
|
|
29248
|
+
return;
|
|
29249
|
+
}
|
|
29250
|
+
this._screen(f.geometry).forEach(function (p) {
|
|
29251
|
+
if (!Array.isArray(p) || typeof p[0] !== 'number') { return; }
|
|
29252
|
+
if (p[0] < -100 || p[1] < -100 || p[0] > self._w + 100 || p[1] > self._h + 100) { return; }
|
|
29253
|
+
var box = null;
|
|
29254
|
+
if (iconId) { box = self._icon(ctx, l, c, iconId, p[0], p[1], 0); }
|
|
29255
|
+
if (text !== null && text !== undefined && text !== '') { var tb = self._text(ctx, l, c, String(text), p[0], p[1], 0); box = box || tb; }
|
|
29256
|
+
if (box) { self._record(l, f, { kind: 'box', box: box }); }
|
|
29257
|
+
});
|
|
29258
|
+
};
|
|
29259
|
+
|
|
29260
|
+
CanvasMap.prototype._icon = function (ctx, l, c, id, x, y, angle) {
|
|
29261
|
+
var img = this._image(String(id));
|
|
29262
|
+
if (!img) { return null; }
|
|
29263
|
+
var size = layoutOf(l, 'icon-size', c, 1);
|
|
29264
|
+
var w = img.w * size, h = img.h * size;
|
|
29265
|
+
var anchor = ANCHOR_SHIFT[layoutOf(l, 'icon-anchor', c, 'center')] || ANCHOR_SHIFT.center;
|
|
29266
|
+
var off = layoutOf(l, 'icon-offset', c, [0, 0]);
|
|
29267
|
+
var dx = off[0] * size + anchor[0] * w, dy = off[1] * size + anchor[1] * h;
|
|
29268
|
+
ctx.save();
|
|
29269
|
+
ctx.globalAlpha = paintOf(l, 'icon-opacity', c, 1);
|
|
29270
|
+
ctx.translate(x, y);
|
|
29271
|
+
if (angle) { ctx.rotate(angle); }
|
|
29272
|
+
ctx.drawImage(img.source, dx, dy, w, h);
|
|
29273
|
+
ctx.restore();
|
|
29274
|
+
return angle ? null : [x + dx, y + dy, x + dx + w, y + dy + h];
|
|
29275
|
+
};
|
|
29276
|
+
|
|
29277
|
+
CanvasMap.prototype._text = function (ctx, l, c, text, x, y, angle) {
|
|
29278
|
+
var size = layoutOf(l, 'text-size', c, 16);
|
|
29279
|
+
var font = layoutOf(l, 'text-font', c, ['Noto Sans Regular']);
|
|
29280
|
+
var bold = /Bold|Medium|Semibold/i.test(Array.isArray(font) ? font[0] : font);
|
|
29281
|
+
ctx.save();
|
|
29282
|
+
ctx.font = (bold ? '600 ' : '') + size + 'px -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans", Arial, sans-serif';
|
|
29283
|
+
var w = ctx.measureText(text).width, h = size * 1.2;
|
|
29284
|
+
var anchor = ANCHOR_SHIFT[layoutOf(l, 'text-anchor', c, 'center')] || ANCHOR_SHIFT.center;
|
|
29285
|
+
var off = layoutOf(l, 'text-offset', c, [0, 0]);
|
|
29286
|
+
var dx = off[0] * size + anchor[0] * w, dy = off[1] * size + anchor[1] * h;
|
|
29287
|
+
ctx.translate(x, y);
|
|
29288
|
+
if (angle) { ctx.rotate(angle); }
|
|
29289
|
+
ctx.textBaseline = 'middle';
|
|
29290
|
+
var halo = paintOf(l, 'text-halo-width', c, 0);
|
|
29291
|
+
if (halo > 0) { ctx.lineWidth = halo * 2; ctx.strokeStyle = paintOf(l, 'text-halo-color', c, '#ffffff'); ctx.lineJoin = 'round'; ctx.strokeText(text, dx, dy + h / 2); }
|
|
29292
|
+
ctx.globalAlpha = paintOf(l, 'text-opacity', c, 1);
|
|
29293
|
+
ctx.fillStyle = paintOf(l, 'text-color', c, '#000000');
|
|
29294
|
+
ctx.fillText(text, dx, dy + h / 2);
|
|
29295
|
+
ctx.restore();
|
|
29296
|
+
return angle ? null : [x + dx, y + dy, x + dx + w, y + dy + h];
|
|
29297
|
+
};
|
|
29298
|
+
|
|
29299
|
+
// A heat map: each point a blurred spot of its weight on an offscreen canvas, then coloured by density.
|
|
29300
|
+
CanvasMap.prototype._drawHeatmap = function (ctx, l, features) {
|
|
29301
|
+
var self = this, zoom = this._zoom, w = this._w, h = this._h;
|
|
29302
|
+
var off = this._heatCanvas = this._heatCanvas || this._doc.createElement('canvas');
|
|
29303
|
+
off.width = w; off.height = h;
|
|
29304
|
+
var hc = off.getContext('2d');
|
|
29305
|
+
if (!hc) { return; }
|
|
29306
|
+
hc.clearRect(0, 0, w, h);
|
|
29307
|
+
var radius = paintOf(l, 'heatmap-radius', { zoom: zoom }, 30), intensity = paintOf(l, 'heatmap-intensity', { zoom: zoom }, 1);
|
|
29308
|
+
features.forEach(function (f) {
|
|
29309
|
+
var c = { zoom: zoom, f: f };
|
|
29310
|
+
if (!f.geometry || !passes(l.filter, c)) { return; }
|
|
29311
|
+
var weight = Math.max(0, Math.min(1, paintOf(l, 'heatmap-weight', c, 1) * intensity * 0.35));
|
|
29312
|
+
self._screen(f.geometry).forEach(function (p) {
|
|
29313
|
+
if (!Array.isArray(p) || typeof p[0] !== 'number' || p[0] < -radius || p[1] < -radius || p[0] > w + radius || p[1] > h + radius) { return; }
|
|
29314
|
+
// A falloff close to the engine's Gaussian kernel of the same radius.
|
|
29315
|
+
var g = hc.createRadialGradient(p[0], p[1], 0, p[0], p[1], radius);
|
|
29316
|
+
g.addColorStop(0, 'rgba(0,0,0,' + weight + ')');
|
|
29317
|
+
g.addColorStop(0.35, 'rgba(0,0,0,' + (weight * 0.45) + ')');
|
|
29318
|
+
g.addColorStop(0.7, 'rgba(0,0,0,' + (weight * 0.08) + ')');
|
|
29319
|
+
g.addColorStop(1, 'rgba(0,0,0,0)');
|
|
29320
|
+
hc.fillStyle = g;
|
|
29321
|
+
hc.fillRect(p[0] - radius, p[1] - radius, radius * 2, radius * 2);
|
|
29322
|
+
});
|
|
29323
|
+
});
|
|
29324
|
+
var img = hc.getImageData(0, 0, w, h), d = img.data;
|
|
29325
|
+
// The colour ramp, sampled once for 256 densities.
|
|
29326
|
+
var ramp = this._ramp(l), opacity = paintOf(l, 'heatmap-opacity', { zoom: zoom }, 1);
|
|
29327
|
+
for (var i = 0; i < d.length; i += 4) {
|
|
29328
|
+
var a = d[i + 3];
|
|
29329
|
+
if (!a) { continue; }
|
|
29330
|
+
var k = a * 4;
|
|
29331
|
+
d[i] = ramp[k]; d[i + 1] = ramp[k + 1]; d[i + 2] = ramp[k + 2]; d[i + 3] = ramp[k + 3] * opacity;
|
|
29332
|
+
}
|
|
29333
|
+
hc.putImageData(img, 0, 0);
|
|
29334
|
+
ctx.save();
|
|
29335
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
29336
|
+
ctx.drawImage(off, 0, 0, w, h, 0, 0, this._canvas.width, this._canvas.height);
|
|
29337
|
+
ctx.restore();
|
|
29338
|
+
};
|
|
29339
|
+
|
|
29340
|
+
CanvasMap.prototype._ramp = function (l) {
|
|
29341
|
+
var expr = l.paint && l.paint['heatmap-color'];
|
|
29342
|
+
var key = JSON.stringify(expr);
|
|
29343
|
+
if (this._rampKey === key && this._rampData) { return this._rampData; }
|
|
29344
|
+
var c = this._doc.createElement('canvas');
|
|
29345
|
+
c.width = 256; c.height = 1;
|
|
29346
|
+
var x = c.getContext('2d');
|
|
29347
|
+
var stops = Array.isArray(expr) && expr[0] === 'interpolate' ? expr.slice(3) : [0, 'rgba(0,0,255,0)', 0.5, 'rgb(0,255,0)', 1, 'rgb(255,0,0)'];
|
|
29348
|
+
var grad = x.createLinearGradient(0, 0, 256, 0);
|
|
29349
|
+
for (var i = 0; i < stops.length; i += 2) { grad.addColorStop(Math.max(0, Math.min(1, stops[i])), stops[i + 1]); }
|
|
29350
|
+
x.fillStyle = grad;
|
|
29351
|
+
x.fillRect(0, 0, 256, 1);
|
|
29352
|
+
this._rampKey = key;
|
|
29353
|
+
this._rampData = x.getImageData(0, 0, 256, 1).data;
|
|
29354
|
+
return this._rampData;
|
|
29355
|
+
};
|
|
29356
|
+
|
|
29357
|
+
// ---- hit tests
|
|
29358
|
+
|
|
29359
|
+
function hits(entry, x0, y0, x1, y1) {
|
|
29360
|
+
var h = entry.hit, cx = (x0 + x1) / 2, cy = (y0 + y1) / 2, pad = Math.max(x1 - x0, y1 - y0) / 2;
|
|
29361
|
+
if (h.kind === 'circle') { return Math.sqrt(Math.pow(h.x - cx, 2) + Math.pow(h.y - cy, 2)) <= h.r + pad; }
|
|
29362
|
+
if (h.kind === 'box') { return h.box[0] <= x1 && h.box[2] >= x0 && h.box[1] <= y1 && h.box[3] >= y0; }
|
|
29363
|
+
if (h.kind === 'line') {
|
|
29364
|
+
var reach = h.width / 2 + Math.max(3, pad);
|
|
29365
|
+
for (var i = 0; i < h.lines.length; i++) {
|
|
29366
|
+
var ln = h.lines[i];
|
|
29367
|
+
for (var j = 1; j < ln.length; j++) { if (segDist(cx, cy, ln[j - 1][0], ln[j - 1][1], ln[j][0], ln[j][1]) <= reach) { return true; } }
|
|
29368
|
+
}
|
|
29369
|
+
return false;
|
|
29370
|
+
}
|
|
29371
|
+
if (h.kind === 'fill') {
|
|
29372
|
+
return h.polys.some(function (rings) {
|
|
29373
|
+
if (!rings.length || !inRing(cx, cy, rings[0])) { return false; }
|
|
29374
|
+
for (var k = 1; k < rings.length; k++) { if (inRing(cx, cy, rings[k])) { return false; } }
|
|
29375
|
+
return true;
|
|
29376
|
+
});
|
|
29377
|
+
}
|
|
29378
|
+
return false;
|
|
29379
|
+
}
|
|
29380
|
+
|
|
29381
|
+
/** Features drawn at a point or in a box, top first: { type, id, properties, geometry, layer: { id }, source }. */
|
|
29382
|
+
CanvasMap.prototype.queryRenderedFeatures = function (at, o) {
|
|
29383
|
+
if (at && !Array.isArray(at) && at.layers) { o = at; at = null; }
|
|
29384
|
+
o = o || {};
|
|
29385
|
+
var x0, y0, x1, y1;
|
|
29386
|
+
if (!at) { x0 = 0; y0 = 0; x1 = this._w; y1 = this._h; }
|
|
29387
|
+
else if (Array.isArray(at) && Array.isArray(at[0])) { x0 = Math.min(at[0][0], at[1][0]); x1 = Math.max(at[0][0], at[1][0]); y0 = Math.min(at[0][1], at[1][1]); y1 = Math.max(at[0][1], at[1][1]); }
|
|
29388
|
+
else { var p = Array.isArray(at) ? { x: at[0], y: at[1] } : at; x0 = x1 = p.x; y0 = y1 = p.y; }
|
|
29389
|
+
var out = [], seen = [];
|
|
29390
|
+
for (var i = this._drawn.length - 1; i >= 0; i--) {
|
|
29391
|
+
var d = this._drawn[i];
|
|
29392
|
+
if (o.layers && o.layers.indexOf(d.layer.id) < 0) { continue; }
|
|
29393
|
+
if (!hits(d, x0, y0, x1, y1)) { continue; }
|
|
29394
|
+
if (seen.indexOf(d) >= 0) { continue; }
|
|
29395
|
+
seen.push(d);
|
|
29396
|
+
out.push({ type: 'Feature', id: d.f.id, properties: d.f.properties || {}, geometry: d.f.geometry, layer: { id: d.layer.id, type: d.layer.type }, source: d.layer.source });
|
|
29397
|
+
}
|
|
29398
|
+
return out;
|
|
29399
|
+
};
|
|
29400
|
+
|
|
29401
|
+
CanvasMap.prototype.querySourceFeatures = function (id, o) {
|
|
29402
|
+
var src = this._sources[id];
|
|
29403
|
+
if (!src || !src._shown) { return []; }
|
|
29404
|
+
var zoom = this._zoom, filter = o && o.filter;
|
|
29405
|
+
return src._shown(zoom).filter(function (f) { return passes(filter, { zoom: zoom, f: f }); });
|
|
29406
|
+
};
|
|
29407
|
+
|
|
29408
|
+
// ---- input: pointer, wheel, touch, keyboard
|
|
29409
|
+
|
|
29410
|
+
CanvasMap.prototype._event = function (type, original, point) {
|
|
29411
|
+
var self = this;
|
|
29412
|
+
var e = {
|
|
29413
|
+
type: type, originalEvent: original, point: point, lngLat: this.unproject(point), target: this, defaultPrevented: false,
|
|
29414
|
+
preventDefault: function () { e.defaultPrevented = true; if (type === 'mousedown' || type === 'touchstart') { self._drag = null; } },
|
|
29415
|
+
};
|
|
29416
|
+
if (original && original.touches) { e.points = Array.prototype.map.call(original.touches, function (t) { return self._point(t); }); }
|
|
29417
|
+
return e;
|
|
29418
|
+
};
|
|
29419
|
+
|
|
29420
|
+
CanvasMap.prototype._point = function (ev) {
|
|
29421
|
+
var r = this._container.getBoundingClientRect();
|
|
29422
|
+
return { x: ev.clientX - r.left, y: ev.clientY - r.top };
|
|
29423
|
+
};
|
|
29424
|
+
|
|
29425
|
+
// A map event to the map's listeners and to the layers' listeners whose features are under the point.
|
|
29426
|
+
CanvasMap.prototype._dispatch = function (type, original, point) {
|
|
29427
|
+
var e = this._event(type, original, point);
|
|
29428
|
+
var self = this;
|
|
29429
|
+
var layerTypes = { click: 1, dblclick: 1, mousedown: 1, mouseup: 1, mousemove: 1, touchstart: 1, touchend: 1, contextmenu: 1 };
|
|
29430
|
+
if (layerTypes[type]) {
|
|
29431
|
+
this._layerEvents.forEach(function (h) {
|
|
29432
|
+
if (h.type !== type) { return; }
|
|
29433
|
+
var features = self.queryRenderedFeatures(point, { layers: [h.layer] });
|
|
29434
|
+
if (!features.length) { return; }
|
|
29435
|
+
var le = self._event(type, original, point);
|
|
29436
|
+
le.features = features;
|
|
29437
|
+
h.fn(le);
|
|
29438
|
+
if (le.defaultPrevented) { e.preventDefault(); }
|
|
29439
|
+
});
|
|
29440
|
+
}
|
|
29441
|
+
if (type === 'mousemove') {
|
|
29442
|
+
// mouseenter / mouseleave per layer.
|
|
29443
|
+
this._layerEvents.forEach(function (h) {
|
|
29444
|
+
if (h.type !== 'mouseenter' && h.type !== 'mouseleave') { return; }
|
|
29445
|
+
var features = self.queryRenderedFeatures(point, { layers: [h.layer] });
|
|
29446
|
+
var inside = features.length > 0;
|
|
29447
|
+
if (inside && !h.inside && h.type === 'mouseenter') { var en = self._event('mouseenter', original, point); en.features = features; h.fn(en); }
|
|
29448
|
+
if (!inside && h.inside && h.type === 'mouseleave') { h.fn(self._event('mouseleave', original, point)); }
|
|
29449
|
+
h.inside = inside;
|
|
29450
|
+
});
|
|
29451
|
+
}
|
|
29452
|
+
(this._events[type] || []).slice().forEach(function (fn) { fn(e); });
|
|
29453
|
+
return e;
|
|
29454
|
+
};
|
|
29455
|
+
|
|
29456
|
+
CanvasMap.prototype._input = function () {
|
|
29457
|
+
var self = this, el = this._canvasContainer, doc = this._doc;
|
|
29458
|
+
var down = null, moved = false, lastClick = 0;
|
|
29459
|
+
var start = function (ev, point) {
|
|
29460
|
+
var e = self._dispatch(ev.touches ? 'touchstart' : 'mousedown', ev, point);
|
|
29461
|
+
moved = false;
|
|
29462
|
+
down = point;
|
|
29463
|
+
self._drag = e.defaultPrevented || !self.dragPan.on ? null : { from: point, center: self.project(self._center) };
|
|
29464
|
+
};
|
|
29465
|
+
el.addEventListener('mousedown', function (ev) {
|
|
29466
|
+
if (ev.button !== 0 || ev.target !== self._canvas) { return; }
|
|
29467
|
+
start(ev, self._point(ev));
|
|
29468
|
+
});
|
|
29469
|
+
doc.addEventListener('mousemove', function (ev) {
|
|
29470
|
+
var point = self._point(ev);
|
|
29471
|
+
if (self._drag) {
|
|
29472
|
+
var d = self._drag;
|
|
29473
|
+
if (!moved && Math.abs(point.x - d.from.x) + Math.abs(point.y - d.from.y) < 3) { return; }
|
|
29474
|
+
if (!moved) { self._fire('movestart', {}); self._fire('dragstart', {}); }
|
|
29475
|
+
moved = true;
|
|
29476
|
+
self._set(self.unproject([d.center.x - (point.x - d.from.x), d.center.y - (point.y - d.from.y)]));
|
|
29477
|
+
self._fire('drag', {});
|
|
29478
|
+
return;
|
|
29479
|
+
}
|
|
29480
|
+
if (down) { moved = moved || Math.abs(point.x - down.x) + Math.abs(point.y - down.y) >= 3; }
|
|
29481
|
+
var inside = point.x >= 0 && point.y >= 0 && point.x <= self._w && point.y <= self._h;
|
|
29482
|
+
if (inside || down) { self._dispatch('mousemove', ev, point); }
|
|
29483
|
+
else if (self._wasInside) { self._dispatch('mouseout', ev, point); }
|
|
29484
|
+
self._wasInside = inside;
|
|
29485
|
+
});
|
|
29486
|
+
doc.addEventListener('mouseup', function (ev) {
|
|
29487
|
+
if (!down) { return; }
|
|
29488
|
+
var point = self._point(ev);
|
|
29489
|
+
var wasDrag = !!self._drag && moved;
|
|
29490
|
+
self._drag = null;
|
|
29491
|
+
self._dispatch('mouseup', ev, point);
|
|
29492
|
+
if (wasDrag) { self._fire('dragend', {}); self._end(false); }
|
|
29493
|
+
else if (!moved && ev.target && (ev.target === self._canvas)) {
|
|
29494
|
+
var now = Date.now();
|
|
29495
|
+
self._dispatch('click', ev, point);
|
|
29496
|
+
if (now - lastClick < 300) {
|
|
29497
|
+
var dbl = self._dispatch('dblclick', ev, point);
|
|
29498
|
+
if (!dbl.defaultPrevented && self.doubleClickZoom.on) { self.easeTo({ zoom: self._zoom + (ev.shiftKey ? -1 : 1), center: self.unproject(point), duration: 250 }); }
|
|
29499
|
+
lastClick = 0;
|
|
29500
|
+
} else { lastClick = now; }
|
|
29501
|
+
}
|
|
29502
|
+
down = null;
|
|
29503
|
+
});
|
|
29504
|
+
el.addEventListener('wheel', function (ev) {
|
|
29505
|
+
if (!self.scrollZoom.on) { return; }
|
|
29506
|
+
ev.preventDefault();
|
|
29507
|
+
var point = self._point(ev);
|
|
29508
|
+
var dz = -ev.deltaY * (ev.deltaMode === 1 ? 0.05 : 0.0025);
|
|
29509
|
+
dz = Math.max(-1, Math.min(1, dz));
|
|
29510
|
+
var before = self.unproject(point);
|
|
29511
|
+
self._set(null, self._zoom + dz);
|
|
29512
|
+
// Keep the place under the pointer where it was.
|
|
29513
|
+
var p = self.project(before), c = self.project(self._center);
|
|
29514
|
+
self._set(self.unproject([c.x + p.x - point.x, c.y + p.y - point.y]));
|
|
29515
|
+
clearTimeout(self._wheelEnd);
|
|
29516
|
+
self._wheelEnd = setTimeout(function () { self._end(true); }, 150);
|
|
29517
|
+
}, { passive: false });
|
|
29518
|
+
// Touch: one finger pans, two fingers pinch.
|
|
29519
|
+
var pinch = null;
|
|
29520
|
+
el.addEventListener('touchstart', function (ev) {
|
|
29521
|
+
if (ev.touches.length === 1) { start(ev, self._point(ev.touches[0])); }
|
|
29522
|
+
else if (ev.touches.length === 2 && self.touchZoomRotate.on) {
|
|
29523
|
+
self._drag = null;
|
|
29524
|
+
var a = self._point(ev.touches[0]), b = self._point(ev.touches[1]);
|
|
29525
|
+
pinch = { d: Math.hypot(a.x - b.x, a.y - b.y), zoom: self._zoom, mid: self.unproject([(a.x + b.x) / 2, (a.y + b.y) / 2]) };
|
|
29526
|
+
}
|
|
29527
|
+
}, { passive: true });
|
|
29528
|
+
el.addEventListener('touchmove', function (ev) {
|
|
29529
|
+
if (pinch && ev.touches.length === 2) {
|
|
29530
|
+
ev.preventDefault();
|
|
29531
|
+
var a = self._point(ev.touches[0]), b = self._point(ev.touches[1]);
|
|
29532
|
+
var d = Math.hypot(a.x - b.x, a.y - b.y), mid = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
|
|
29533
|
+
self._set(null, pinch.zoom + Math.log2(d / Math.max(1, pinch.d)));
|
|
29534
|
+
var p = self.project(pinch.mid), c = self.project(self._center);
|
|
29535
|
+
self._set(self.unproject([c.x + p.x - mid.x, c.y + p.y - mid.y]));
|
|
29536
|
+
return;
|
|
29537
|
+
}
|
|
29538
|
+
if (ev.touches.length !== 1) { return; }
|
|
29539
|
+
var point = self._point(ev.touches[0]);
|
|
29540
|
+
if (self._drag) {
|
|
29541
|
+
ev.preventDefault();
|
|
29542
|
+
var dr = self._drag;
|
|
29543
|
+
moved = moved || Math.abs(point.x - dr.from.x) + Math.abs(point.y - dr.from.y) >= 3;
|
|
29544
|
+
self._set(self.unproject([dr.center.x - (point.x - dr.from.x), dr.center.y - (point.y - dr.from.y)]));
|
|
29545
|
+
return;
|
|
29546
|
+
}
|
|
29547
|
+
ev.preventDefault();
|
|
29548
|
+
moved = true;
|
|
29549
|
+
self._dispatch('touchmove', ev, point);
|
|
29550
|
+
}, { passive: false });
|
|
29551
|
+
el.addEventListener('touchend', function (ev) {
|
|
29552
|
+
if (pinch && ev.touches.length < 2) { pinch = null; self._end(true); return; }
|
|
29553
|
+
var t = ev.changedTouches[0];
|
|
29554
|
+
var point = t ? self._point(t) : down || { x: 0, y: 0 };
|
|
29555
|
+
var wasDrag = !!self._drag && moved;
|
|
29556
|
+
self._drag = null;
|
|
29557
|
+
self._dispatch('touchend', ev, point);
|
|
29558
|
+
if (wasDrag) { self._end(false); } else if (!moved) { self._dispatch('click', ev, point); }
|
|
29559
|
+
down = null;
|
|
29560
|
+
});
|
|
29561
|
+
this._canvas.addEventListener('keydown', function (ev) {
|
|
29562
|
+
if (!self.keyboard.on) { return; }
|
|
29563
|
+
var k = ev.key, step = 100;
|
|
29564
|
+
if (k === '+' || k === '=') { self.zoomIn(); } else if (k === '-') { self.zoomOut(); }
|
|
29565
|
+
else if (k === 'ArrowLeft') { self.panBy([-step, 0]); } else if (k === 'ArrowRight') { self.panBy([step, 0]); }
|
|
29566
|
+
else if (k === 'ArrowUp') { self.panBy([0, -step]); } else if (k === 'ArrowDown') { self.panBy([0, step]); }
|
|
29567
|
+
else { return; }
|
|
29568
|
+
ev.preventDefault();
|
|
29569
|
+
});
|
|
29570
|
+
};
|
|
29571
|
+
|
|
29572
|
+
// ------------------------------------------------------------------ popups
|
|
29573
|
+
|
|
29574
|
+
function Popup(options) {
|
|
29575
|
+
this.options = Object.assign({ closeButton: true, closeOnClick: true, offset: 0 }, options || {});
|
|
29576
|
+
this._events = {};
|
|
29577
|
+
}
|
|
29578
|
+
Popup.prototype.setLngLat = function (ll) { this._lngLat = toLngLat(ll); this._update(); return this; };
|
|
29579
|
+
Popup.prototype.getLngLat = function () { return this._lngLat ? new LngLat(this._lngLat.lng, this._lngLat.lat) : null; };
|
|
29580
|
+
Popup.prototype._ensure = function (doc) {
|
|
29581
|
+
if (this._el) { return; }
|
|
29582
|
+
var self = this;
|
|
29583
|
+
var el = this._el = doc.createElement('div');
|
|
29584
|
+
el.className = 'maplibregl-popup' + (this.options.className ? ' ' + this.options.className : '');
|
|
29585
|
+
el.style.position = 'absolute'; el.style.left = '0'; el.style.top = '0';
|
|
29586
|
+
var tip = doc.createElement('div'); tip.className = 'maplibregl-popup-tip';
|
|
29587
|
+
var content = this._content = doc.createElement('div'); content.className = 'maplibregl-popup-content';
|
|
29588
|
+
el.appendChild(tip); el.appendChild(content);
|
|
29589
|
+
if (this.options.maxWidth) { el.style.maxWidth = this.options.maxWidth; }
|
|
29590
|
+
if (this.options.closeButton) {
|
|
29591
|
+
var b = doc.createElement('button');
|
|
29592
|
+
b.type = 'button'; b.className = 'maplibregl-popup-close-button'; b.setAttribute('aria-label', 'Close popup'); b.innerHTML = '×';
|
|
29593
|
+
b.addEventListener('click', function () { self.remove(); });
|
|
29594
|
+
this._closeButton = b;
|
|
29595
|
+
}
|
|
29596
|
+
};
|
|
29597
|
+
Popup.prototype.setHTML = function (html) {
|
|
29598
|
+
var doc = (this._map && this._map._doc) || document;
|
|
29599
|
+
this._ensure(doc);
|
|
29600
|
+
this._content.innerHTML = html;
|
|
29601
|
+
if (this._closeButton) { this._content.appendChild(this._closeButton); }
|
|
29602
|
+
this._update();
|
|
29603
|
+
return this;
|
|
29604
|
+
};
|
|
29605
|
+
Popup.prototype.setDOMContent = function (node) {
|
|
29606
|
+
var doc = (this._map && this._map._doc) || node.ownerDocument || document;
|
|
29607
|
+
this._ensure(doc);
|
|
29608
|
+
while (this._content.firstChild) { this._content.removeChild(this._content.firstChild); }
|
|
29609
|
+
this._content.appendChild(node);
|
|
29610
|
+
if (this._closeButton) { this._content.appendChild(this._closeButton); }
|
|
29611
|
+
this._update();
|
|
29612
|
+
return this;
|
|
29613
|
+
};
|
|
29614
|
+
Popup.prototype.setText = function (t) { return this.setHTML(String(t).replace(/[&<>"']/g, function (c) { return '&#' + c.charCodeAt(0) + ';'; })); };
|
|
29615
|
+
Popup.prototype.getElement = function () { return this._el || null; };
|
|
29616
|
+
Popup.prototype.isOpen = function () { return !!this._map; };
|
|
29617
|
+
Popup.prototype.addTo = function (map) {
|
|
29618
|
+
var self = this;
|
|
29619
|
+
this._map = map;
|
|
29620
|
+
this._ensure(map._doc);
|
|
29621
|
+
map.getCanvasContainer().appendChild(this._el);
|
|
29622
|
+
this._onMove = function () { self._update(); };
|
|
29623
|
+
map.on('move', this._onMove);
|
|
29624
|
+
map.on('resize', this._onMove);
|
|
29625
|
+
if (this.options.closeOnClick) {
|
|
29626
|
+
this._onClick = function () { self.remove(); };
|
|
29627
|
+
setTimeout(function () { if (self._map) { map.on('click', self._onClick); } }, 0);
|
|
29628
|
+
}
|
|
29629
|
+
this._update();
|
|
29630
|
+
this._fire('open');
|
|
29631
|
+
return this;
|
|
29632
|
+
};
|
|
29633
|
+
Popup.prototype.remove = function () {
|
|
29634
|
+
var map = this._map;
|
|
29635
|
+
if (!map) { return this; }
|
|
29636
|
+
if (this._el && this._el.parentNode) { this._el.parentNode.removeChild(this._el); }
|
|
29637
|
+
map.off('move', this._onMove);
|
|
29638
|
+
map.off('resize', this._onMove);
|
|
29639
|
+
if (this._onClick) { map.off('click', this._onClick); }
|
|
29640
|
+
this._map = null;
|
|
29641
|
+
this._fire('close');
|
|
29642
|
+
return this;
|
|
29643
|
+
};
|
|
29644
|
+
Popup.prototype.on = function (type, fn) { (this._events[type] = this._events[type] || []).push(fn); return this; };
|
|
29645
|
+
Popup.prototype.off = function (type, fn) { this._events[type] = (this._events[type] || []).filter(function (f) { return f !== fn; }); return this; };
|
|
29646
|
+
Popup.prototype._fire = function (type) { var self = this; (this._events[type] || []).slice().forEach(function (fn) { fn({ type: type, target: self }); }); };
|
|
29647
|
+
Popup.prototype._update = function () {
|
|
29648
|
+
if (!this._map || !this._el || !this._lngLat) { return; }
|
|
29649
|
+
var p = this._map.project(this._lngLat);
|
|
29650
|
+
var anchor = this.options.anchor || (p.y < this._el.offsetHeight + 20 ? 'top' : 'bottom');
|
|
29651
|
+
var off = this.options.offset, o = typeof off === 'number' ? { top: [0, off], bottom: [0, -off], left: [off, 0], right: [-off, 0] }[anchor.split('-')[0]] || [0, 0] : (Array.isArray(off) ? off : (off && off[anchor]) || [0, 0]);
|
|
29652
|
+
var shift = { center: [-50, -50], top: [-50, 0], bottom: [-50, -100], left: [0, -50], right: [-100, -50], 'top-left': [0, 0], 'top-right': [-100, 0], 'bottom-left': [0, -100], 'bottom-right': [-100, -100] }[anchor] || [-50, -100];
|
|
29653
|
+
var cls = this._el.className.replace(/\s*maplibregl-popup-anchor-[a-z-]+/g, '');
|
|
29654
|
+
this._el.className = cls + ' maplibregl-popup-anchor-' + anchor;
|
|
29655
|
+
this._el.style.transform = 'translate(' + shift[0] + '%, ' + shift[1] + '%) translate(' + Math.round(p.x + o[0]) + 'px, ' + Math.round(p.y + o[1]) + 'px)';
|
|
29656
|
+
};
|
|
29657
|
+
|
|
29658
|
+
// ------------------------------------------------------------------ controls (the engine's class names, so its stylesheet applies)
|
|
29659
|
+
|
|
29660
|
+
function NavigationControl(options) { this.options = Object.assign({ showZoom: true, showCompass: true }, options || {}); }
|
|
29661
|
+
NavigationControl.prototype.onAdd = function (map) {
|
|
29662
|
+
var doc = map._doc, el = this._el = doc.createElement('div');
|
|
29663
|
+
el.className = 'maplibregl-ctrl maplibregl-ctrl-group';
|
|
29664
|
+
var button = function (cls, label, fn) {
|
|
29665
|
+
var b = doc.createElement('button');
|
|
29666
|
+
b.type = 'button'; b.className = cls; b.title = label; b.setAttribute('aria-label', label);
|
|
29667
|
+
var icon = doc.createElement('span'); icon.className = 'maplibregl-ctrl-icon'; icon.setAttribute('aria-hidden', 'true');
|
|
29668
|
+
b.appendChild(icon);
|
|
29669
|
+
b.addEventListener('click', fn);
|
|
29670
|
+
el.appendChild(b);
|
|
29671
|
+
};
|
|
29672
|
+
if (this.options.showZoom) {
|
|
29673
|
+
button('maplibregl-ctrl-zoom-in', 'Zoom in', function () { map.zoomIn(); });
|
|
29674
|
+
button('maplibregl-ctrl-zoom-out', 'Zoom out', function () { map.zoomOut(); });
|
|
29675
|
+
}
|
|
29676
|
+
// The map never turns: no compass.
|
|
29677
|
+
return el;
|
|
29678
|
+
};
|
|
29679
|
+
NavigationControl.prototype.onRemove = function () { if (this._el && this._el.parentNode) { this._el.parentNode.removeChild(this._el); } };
|
|
29680
|
+
|
|
29681
|
+
function ScaleControl(options) { this.options = Object.assign({ maxWidth: 100, unit: 'metric' }, options || {}); }
|
|
29682
|
+
ScaleControl.prototype.onAdd = function (map) {
|
|
29683
|
+
var self = this, el = this._el = map._doc.createElement('div');
|
|
29684
|
+
el.className = 'maplibregl-ctrl maplibregl-ctrl-scale';
|
|
29685
|
+
this._map = map;
|
|
29686
|
+
this._update = function () {
|
|
29687
|
+
var y = map._h / 2, a = map.unproject([0, y]), b = map.unproject([self.options.maxWidth, y]);
|
|
29688
|
+
var R = 6371008.8, rad = Math.PI / 180;
|
|
29689
|
+
var m = 2 * R * Math.asin(Math.sqrt(Math.pow(Math.sin((b.lat - a.lat) * rad / 2), 2) + Math.cos(a.lat * rad) * Math.cos(b.lat * rad) * Math.pow(Math.sin((b.lng - a.lng) * rad / 2), 2)));
|
|
29690
|
+
var imperial = self.options.unit === 'imperial', v = imperial ? m * 3.28084 : m, unit = imperial ? 'ft' : 'm';
|
|
29691
|
+
if (imperial && v > 5280) { v /= 5280; unit = 'mi'; } else if (!imperial && v >= 1000) { v /= 1000; unit = 'km'; }
|
|
29692
|
+
var pow = Math.pow(10, Math.floor(Math.log10(v))), d = v / pow, nice = (d >= 10 ? 10 : d >= 5 ? 5 : d >= 3 ? 3 : d >= 2 ? 2 : 1) * pow;
|
|
29693
|
+
el.style.width = Math.round(self.options.maxWidth * nice / v) + 'px';
|
|
29694
|
+
el.textContent = nice + ' ' + unit;
|
|
29695
|
+
};
|
|
29696
|
+
map.on('move', this._update);
|
|
29697
|
+
this._update();
|
|
29698
|
+
return el;
|
|
29699
|
+
};
|
|
29700
|
+
ScaleControl.prototype.onRemove = function () { if (this._map) { this._map.off('move', this._update); } if (this._el && this._el.parentNode) { this._el.parentNode.removeChild(this._el); } };
|
|
29701
|
+
|
|
29702
|
+
function FullscreenControl(options) { this.options = options || {}; }
|
|
29703
|
+
FullscreenControl.prototype.onAdd = function (map) {
|
|
29704
|
+
var doc = map._doc, self = this, el = this._el = doc.createElement('div');
|
|
29705
|
+
el.className = 'maplibregl-ctrl maplibregl-ctrl-group';
|
|
29706
|
+
var b = doc.createElement('button');
|
|
29707
|
+
b.type = 'button'; b.className = 'maplibregl-ctrl-fullscreen'; b.title = 'Enter fullscreen'; b.setAttribute('aria-label', 'Enter fullscreen');
|
|
29708
|
+
var icon = doc.createElement('span'); icon.className = 'maplibregl-ctrl-icon'; icon.setAttribute('aria-hidden', 'true');
|
|
29709
|
+
b.appendChild(icon);
|
|
29710
|
+
var target = this.options.container || map.getContainer();
|
|
29711
|
+
b.addEventListener('click', function () {
|
|
29712
|
+
if (doc.fullscreenElement) { doc.exitFullscreen(); } else if (target.requestFullscreen) { target.requestFullscreen(); }
|
|
29713
|
+
});
|
|
29714
|
+
this._change = function () {
|
|
29715
|
+
var on = doc.fullscreenElement === target;
|
|
29716
|
+
b.className = on ? 'maplibregl-ctrl-shrink' : 'maplibregl-ctrl-fullscreen';
|
|
29717
|
+
b.title = on ? 'Exit fullscreen' : 'Enter fullscreen';
|
|
29718
|
+
setTimeout(function () { map.resize(); }, 50);
|
|
29719
|
+
};
|
|
29720
|
+
doc.addEventListener('fullscreenchange', this._change);
|
|
29721
|
+
el.appendChild(b);
|
|
29722
|
+
self._doc = doc;
|
|
29723
|
+
return el;
|
|
29724
|
+
};
|
|
29725
|
+
FullscreenControl.prototype.onRemove = function () { if (this._doc) { this._doc.removeEventListener('fullscreenchange', this._change); } if (this._el && this._el.parentNode) { this._el.parentNode.removeChild(this._el); } };
|
|
29726
|
+
|
|
29727
|
+
function GeolocateControl(options) { this.options = options || {}; }
|
|
29728
|
+
GeolocateControl.prototype.onAdd = function (map) {
|
|
29729
|
+
var doc = map._doc, el = this._el = doc.createElement('div');
|
|
29730
|
+
el.className = 'maplibregl-ctrl maplibregl-ctrl-group';
|
|
29731
|
+
var b = doc.createElement('button');
|
|
29732
|
+
b.type = 'button'; b.className = 'maplibregl-ctrl-geolocate'; b.title = 'Find my location'; b.setAttribute('aria-label', 'Find my location');
|
|
29733
|
+
var icon = doc.createElement('span'); icon.className = 'maplibregl-ctrl-icon'; icon.setAttribute('aria-hidden', 'true');
|
|
29734
|
+
b.appendChild(icon);
|
|
29735
|
+
var geo = doc.defaultView && doc.defaultView.navigator && doc.defaultView.navigator.geolocation;
|
|
29736
|
+
if (!geo) { b.disabled = true; }
|
|
29737
|
+
b.addEventListener('click', function () {
|
|
29738
|
+
if (!geo) { return; }
|
|
29739
|
+
geo.getCurrentPosition(function (pos) {
|
|
29740
|
+
map.flyTo({ center: [pos.coords.longitude, pos.coords.latitude], zoom: Math.max(map.getZoom(), 15) });
|
|
29741
|
+
map._fire('geolocate', { coords: pos.coords });
|
|
29742
|
+
}, function (err) { map._fire('error', { error: err }); }, { enableHighAccuracy: true, timeout: 10000 });
|
|
29743
|
+
});
|
|
29744
|
+
el.appendChild(b);
|
|
29745
|
+
return el;
|
|
29746
|
+
};
|
|
29747
|
+
GeolocateControl.prototype.onRemove = function () { if (this._el && this._el.parentNode) { this._el.parentNode.removeChild(this._el); } };
|
|
29748
|
+
|
|
29749
|
+
function AttributionControl(options) { this.options = options || {}; }
|
|
29750
|
+
AttributionControl.prototype.onAdd = function (map) {
|
|
29751
|
+
var el = this._el = map._doc.createElement('details');
|
|
29752
|
+
el.className = 'maplibregl-ctrl maplibregl-ctrl-attrib maplibregl-compact';
|
|
29753
|
+
var summary = map._doc.createElement('summary');
|
|
29754
|
+
summary.className = 'maplibregl-ctrl-attrib-button';
|
|
29755
|
+
summary.title = 'Toggle attribution';
|
|
29756
|
+
summary.setAttribute('aria-label', 'Toggle attribution');
|
|
29757
|
+
var inner = this._inner = map._doc.createElement('div');
|
|
29758
|
+
inner.className = 'maplibregl-ctrl-attrib-inner';
|
|
29759
|
+
el.appendChild(summary);
|
|
29760
|
+
el.appendChild(inner);
|
|
29761
|
+
this._map = map;
|
|
29762
|
+
// Open where there is room, as the engine's own control.
|
|
29763
|
+
if (map._w >= 640) { el.open = true; }
|
|
29764
|
+
this._update();
|
|
29765
|
+
return el;
|
|
29766
|
+
};
|
|
29767
|
+
AttributionControl.prototype._update = function () {
|
|
29768
|
+
if (!this._inner || !this._map) { return; }
|
|
29769
|
+
var parts = [this._map._attribution, this.options.customAttribution].filter(Boolean);
|
|
29770
|
+
this._inner.innerHTML = parts.join(' | ');
|
|
29771
|
+
};
|
|
29772
|
+
AttributionControl.prototype.onRemove = function () { if (this._el && this._el.parentNode) { this._el.parentNode.removeChild(this._el); } };
|
|
29773
|
+
|
|
29774
|
+
function ErrorEvent(error) { this.type = 'error'; this.error = error; }
|
|
29775
|
+
|
|
29776
|
+
/** Whether this browser can draw the WebGL map (WebGL 2). */
|
|
29777
|
+
function webgl2Available(doc) {
|
|
29778
|
+
try {
|
|
29779
|
+
var c = (doc || document).createElement('canvas');
|
|
29780
|
+
return !!(c.getContext && c.getContext('webgl2'));
|
|
29781
|
+
} catch (e) { return false; }
|
|
29782
|
+
}
|
|
29783
|
+
|
|
29784
|
+
return {
|
|
29785
|
+
Map: CanvasMap, Popup: Popup, LngLat: LngLat, LngLatBounds: LngLatBounds,
|
|
29786
|
+
NavigationControl: NavigationControl, ScaleControl: ScaleControl, FullscreenControl: FullscreenControl,
|
|
29787
|
+
GeolocateControl: GeolocateControl, AttributionControl: AttributionControl, ErrorEvent: ErrorEvent,
|
|
29788
|
+
addProtocol: addProtocol, removeProtocol: removeProtocol, webgl2Available: webgl2Available,
|
|
29789
|
+
evaluate: evaluate, clusterAt: clusterAt,
|
|
29790
|
+
};
|
|
29791
|
+
});
|
|
29792
|
+
|
|
29793
|
+
return module.exports;
|
|
29794
|
+
}
|
|
28331
29795
|
function smapCreatePlan() {
|
|
28332
29796
|
var module = { exports: {} };
|
|
28333
29797
|
var exports = module.exports;
|
|
@@ -30099,12 +31563,12 @@ function smapCreatePlan() {
|
|
|
30099
31563
|
|
|
30100
31564
|
return module.exports;
|
|
30101
31565
|
}
|
|
30102
|
-
function smapCreateSdk(engine, interop, plan) {
|
|
31566
|
+
function smapCreateSdk(engine, interop, canvas, plan) {
|
|
30103
31567
|
var module = { exports: {} };
|
|
30104
31568
|
var exports = module.exports;
|
|
30105
31569
|
var define;
|
|
30106
31570
|
var require = function (name) { throw new Error('sMap: unexpected require of ' + name); };
|
|
30107
|
-
var self = { maplibregl: engine, smapInterop: interop, smapPlan: plan };
|
|
31571
|
+
var self = { maplibregl: engine, smapInterop: interop, smapCanvas: canvas, smapPlan: plan };
|
|
30108
31572
|
/*!
|
|
30109
31573
|
* sMap Web SDK — maps from Superroute's sMap service.
|
|
30110
31574
|
*
|
|
@@ -30118,14 +31582,15 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
30118
31582
|
if (typeof module === 'object' && module.exports) {
|
|
30119
31583
|
module.exports = factory(root.maplibregl !== undefined ? root.maplibregl : require('maplibre-gl'),
|
|
30120
31584
|
root.smapInterop !== undefined ? root.smapInterop : require('./smap-interop.js'),
|
|
31585
|
+
root.smapCanvas !== undefined ? root.smapCanvas : (typeof require === 'function' ? require('./smap-canvas.js') : null),
|
|
30121
31586
|
root.smapPlan !== undefined ? root.smapPlan : (typeof require === 'function' ? require('./smap-plan.js') : null));
|
|
30122
31587
|
} else {
|
|
30123
|
-
root.sMap = factory(root.maplibregl, root.smapInterop, root.smapPlan);
|
|
31588
|
+
root.sMap = factory(root.maplibregl, root.smapInterop, root.smapCanvas, root.smapPlan);
|
|
30124
31589
|
}
|
|
30125
|
-
})(typeof self !== 'undefined' ? self : this, function (engine, interop, plan) {
|
|
31590
|
+
})(typeof self !== 'undefined' ? self : this, function (engine, interop, canvasLib, plan) {
|
|
30126
31591
|
'use strict';
|
|
30127
31592
|
|
|
30128
|
-
var VERSION = '1.
|
|
31593
|
+
var VERSION = '1.2.0';
|
|
30129
31594
|
var FLAVORS = ['light', 'white', 'grayscale', 'dark', 'black'];
|
|
30130
31595
|
var BASEMAP_SOURCE = 'basemap';
|
|
30131
31596
|
var BUILDINGS_LAYER = 'smap-buildings-3d';
|
|
@@ -30536,12 +32001,14 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
30536
32001
|
* @param {Object} [options.popup] default popup options of every layer and openPopup (className, maxWidth,
|
|
30537
32002
|
* minWidth, offset, anchor, closeButton, closeOnClick, focusAfterOpen)
|
|
30538
32003
|
* @param {string|Node} [options.popupLoading] shown while a popup's content is a pending promise (default: dots)
|
|
32004
|
+
* @param {'auto'|'webgl'|'canvas'} [options.renderer] 'auto' (default): the WebGL map, or the canvas map (raster
|
|
32005
|
+
* tiles, layers drawn in 2D) where the browser has no WebGL 2 (M61)
|
|
30539
32006
|
* Every other option (center, zoom, pitch, bearing, minZoom, maxZoom, maxBounds, hash, …) goes to the engine.
|
|
30540
32007
|
*/
|
|
30541
32008
|
function SMap(options) {
|
|
30542
32009
|
if (!(this instanceof SMap)) { return new SMap(options); }
|
|
30543
32010
|
options = options || {};
|
|
30544
|
-
if (!engine) { throw new Error('sMap: the rendering engine is not loaded'); }
|
|
32011
|
+
if (!engine && !canvasLib) { throw new Error('sMap: the rendering engine is not loaded'); }
|
|
30545
32012
|
this._base = (options.baseUrl || DEFAULT_BASE).replace(/\/$/, '');
|
|
30546
32013
|
this._token = options.token || null;
|
|
30547
32014
|
this._getToken = options.getToken || null;
|
|
@@ -30591,9 +32058,16 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
30591
32058
|
this._toolHandlers = [];
|
|
30592
32059
|
|
|
30593
32060
|
var self = this;
|
|
32061
|
+
// The renderer (M61): the WebGL engine, or the canvas map where there is no WebGL 2 (or when asked for).
|
|
32062
|
+
var want = options.renderer || 'auto';
|
|
32063
|
+
// Asked of the page itself; without a page (a server, a test) the WebGL engine is kept.
|
|
32064
|
+
var hasPage = typeof document !== 'undefined' && !!document && typeof document.createElement === 'function';
|
|
32065
|
+
var useCanvas = !!canvasLib && (want === 'canvas' || (want === 'auto' && (!engine || (hasPage && !canvasLib.webgl2Available(document)))));
|
|
32066
|
+
this._lib = useCanvas ? canvasLib : engine;
|
|
32067
|
+
this.renderer = useCanvas ? 'canvas' : 'webgl';
|
|
30594
32068
|
var engineOptions = {};
|
|
30595
32069
|
for (var k in options) {
|
|
30596
|
-
if (Object.prototype.hasOwnProperty.call(options, k) && ['token', 'getToken', 'apiKey', 'style', 'language', 'baseUrl', 'projection', 'nearest', 'nodes', 'popup', 'popupLoading', 'autoZoom', 'keyboard3D', 'tools'].indexOf(k) < 0) {
|
|
32070
|
+
if (Object.prototype.hasOwnProperty.call(options, k) && ['token', 'getToken', 'apiKey', 'style', 'language', 'baseUrl', 'projection', 'nearest', 'nodes', 'popup', 'popupLoading', 'autoZoom', 'keyboard3D', 'tools', 'renderer'].indexOf(k) < 0) {
|
|
30597
32071
|
engineOptions[k] = options[k];
|
|
30598
32072
|
}
|
|
30599
32073
|
}
|
|
@@ -30601,6 +32075,11 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
30601
32075
|
// The page's origin is its identity: no app header is sent from a browser. The key travels in the style URL
|
|
30602
32076
|
// (the service carries it into the tile URLs) and, for a style given as a URL or an object, as a header.
|
|
30603
32077
|
engineOptions.transformRequest = function (url) {
|
|
32078
|
+
// Tiles of the canvas map are images: the token rides in the query (sMap reads `token` there too).
|
|
32079
|
+
if (self.renderer === 'canvas') {
|
|
32080
|
+
if (self._token && url.indexOf(self._base) === 0 && !/[?&]token=/.test(url)) { url += (url.indexOf('?') >= 0 ? '&' : '?') + 'token=' + encodeURIComponent(self._token); }
|
|
32081
|
+
return { url: url };
|
|
32082
|
+
}
|
|
30604
32083
|
if ((self._token || self._apiKey) && url.indexOf(self._base) === 0) {
|
|
30605
32084
|
var headers = {};
|
|
30606
32085
|
if (self._token) { headers.Authorization = 'Bearer ' + self._token; }
|
|
@@ -30618,8 +32097,17 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
30618
32097
|
options.nearest === false ? Promise.resolve() : nearestNode(this._base, options.nodes).then(function (base) { self._base = base; })
|
|
30619
32098
|
]);
|
|
30620
32099
|
this.ready = start.then(function () {
|
|
30621
|
-
engineOptions.style = self.
|
|
30622
|
-
|
|
32100
|
+
engineOptions.style = self._engineStyle();
|
|
32101
|
+
try {
|
|
32102
|
+
self.engine = new self._lib.Map(engineOptions);
|
|
32103
|
+
} catch (error) {
|
|
32104
|
+
// A WebGL context the browser refuses (a blocked GPU, too many maps): the canvas map instead, when allowed.
|
|
32105
|
+
if (self.renderer !== 'webgl' || want !== 'auto' || !canvasLib) { throw error; }
|
|
32106
|
+
self._lib = canvasLib;
|
|
32107
|
+
self.renderer = 'canvas';
|
|
32108
|
+
engineOptions.style = self._engineStyle();
|
|
32109
|
+
self.engine = new canvasLib.Map(engineOptions);
|
|
32110
|
+
}
|
|
30623
32111
|
deckCompat(self.engine);
|
|
30624
32112
|
if (self.engine.addControl) {
|
|
30625
32113
|
var badge = badgeControl();
|
|
@@ -30648,6 +32136,20 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
30648
32136
|
else if (style) { this._customStyle = style; this._serverStyle = null; }
|
|
30649
32137
|
};
|
|
30650
32138
|
|
|
32139
|
+
// The style handed to the renderer: the vector style, or for the canvas map sMap's raster tiles of the same scheme.
|
|
32140
|
+
SMap.prototype._engineStyle = function () {
|
|
32141
|
+
if (this.renderer !== 'canvas') { return this._styleUrl(); }
|
|
32142
|
+
if (this._customStyle && typeof this._customStyle === 'object') { return this._customStyle; }
|
|
32143
|
+
var name = this._serverStyle || this._flavor;
|
|
32144
|
+
var win = typeof window !== 'undefined' ? window : {};
|
|
32145
|
+
var tiles = interop.rasterUrl(name, { baseUrl: this._base, lang: this._language, apiKey: this._apiKey, retina: (win.devicePixelRatio || 1) > 1 });
|
|
32146
|
+
return {
|
|
32147
|
+
version: 8,
|
|
32148
|
+
sources: { 'smap-raster': { type: 'raster', tiles: [tiles], tileSize: 256, maxzoom: 19, attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>' } },
|
|
32149
|
+
layers: [{ id: 'smap-raster', type: 'raster', source: 'smap-raster' }],
|
|
32150
|
+
};
|
|
32151
|
+
};
|
|
32152
|
+
|
|
30651
32153
|
SMap.prototype._styleUrl = function () {
|
|
30652
32154
|
if (this._customStyle) { return this._customStyle; }
|
|
30653
32155
|
if (this._serverStyle) { return interop.styleUrl(this._serverStyle, { baseUrl: this._base, lang: this._language, apiKey: this._apiKey }); }
|
|
@@ -30678,7 +32180,7 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
30678
32180
|
SMap.prototype.setStyle = function (style) {
|
|
30679
32181
|
this._takeStyle(style);
|
|
30680
32182
|
this._styleReady = false;
|
|
30681
|
-
this.engine.setStyle(this.
|
|
32183
|
+
this.engine.setStyle(this._engineStyle());
|
|
30682
32184
|
this._styleClass();
|
|
30683
32185
|
return this;
|
|
30684
32186
|
};
|
|
@@ -30755,7 +32257,7 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
30755
32257
|
var k = POPUP_OPTIONS[i];
|
|
30756
32258
|
if (k !== 'className' && k !== 'minWidth' && k !== 'autoPan' && o[k] !== undefined) { engineOptions[k] = k === 'maxWidth' ? cssSize(o[k]) : o[k]; }
|
|
30757
32259
|
}
|
|
30758
|
-
var popup = new
|
|
32260
|
+
var popup = new this._lib.Popup(engineOptions).setLngLat(lngLat);
|
|
30759
32261
|
this._popup = popup;
|
|
30760
32262
|
var pending = isThenable(content);
|
|
30761
32263
|
var loading = o.loading !== undefined ? o.loading : (this._popupLoading !== undefined ? this._popupLoading : POPUP_LOADING);
|
|
@@ -30807,12 +32309,14 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
30807
32309
|
|
|
30808
32310
|
SMap.prototype._reportError = function (error) {
|
|
30809
32311
|
var m = this.engine;
|
|
30810
|
-
if (m &&
|
|
32312
|
+
if (m && this._lib.ErrorEvent && m.fire) { m.fire(new this._lib.ErrorEvent(error)); } else if (typeof console !== 'undefined') { console.error(error); }
|
|
30811
32313
|
};
|
|
30812
32314
|
|
|
30813
32315
|
// A click on the map: the top feature of the layers with a popup, if any, opens its layer's popup.
|
|
30814
32316
|
SMap.prototype._popupClick = function (e) {
|
|
30815
32317
|
var m = this.engine;
|
|
32318
|
+
// The release of a drag is not a click.
|
|
32319
|
+
if (this._dragEndedAt && Date.now() - this._dragEndedAt < 300) { return; }
|
|
30816
32320
|
var ids = [];
|
|
30817
32321
|
var byEngineLayer = {};
|
|
30818
32322
|
for (var i = 0; i < this._order.length; i++) {
|
|
@@ -30939,7 +32443,7 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
30939
32443
|
SMap.prototype.setLanguage = function (language) {
|
|
30940
32444
|
this._language = language;
|
|
30941
32445
|
// A scheme or a custom style of sMap is served per language; a style of your own keeps its labels.
|
|
30942
|
-
if (!this._customStyle) { this._styleReady = false; this.engine.setStyle(this.
|
|
32446
|
+
if (!this._customStyle) { this._styleReady = false; this.engine.setStyle(this._engineStyle()); }
|
|
30943
32447
|
return this;
|
|
30944
32448
|
};
|
|
30945
32449
|
|
|
@@ -31054,6 +32558,8 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
31054
32558
|
var def = this._layers[id];
|
|
31055
32559
|
if (!def) { throw new Error('sMap: no layer ' + id); }
|
|
31056
32560
|
def.data = toFeatureCollection(data);
|
|
32561
|
+
if (def.kind === 'custom') { this._customData(id, def); return this; }
|
|
32562
|
+
if (def.kind === 'models') { if (this.engine) { this.engine.triggerRepaint(); } return this; }
|
|
31057
32563
|
if (def.hovered) { this._setHover(id, def, null); }
|
|
31058
32564
|
var src = this.engine.getSource('smap-' + id);
|
|
31059
32565
|
if (src) { src.setData(def.data); }
|
|
@@ -31065,6 +32571,8 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
31065
32571
|
if (!def) { return this; }
|
|
31066
32572
|
def.hidden = !visible;
|
|
31067
32573
|
var m = this.engine;
|
|
32574
|
+
if (def.kind === 'custom') { if (!visible && this._tooltipFor && this._tooltipFor.id === id) { this._hideTooltip(); } this._layoutMarkers(); return this; }
|
|
32575
|
+
if (def.clusterSet) { this._layoutMarkers(); }
|
|
31068
32576
|
if (!m) { return this; }
|
|
31069
32577
|
def.engineLayers.forEach(function (l) { if (m.getLayer(l)) { m.setLayoutProperty(l, 'visibility', visible ? 'visible' : 'none'); } });
|
|
31070
32578
|
return this;
|
|
@@ -31097,7 +32605,7 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
31097
32605
|
SMap.prototype.fitLayer = function (id, options) {
|
|
31098
32606
|
var def = this._layers[id];
|
|
31099
32607
|
if (!def || !def.data.features.length) { return this; }
|
|
31100
|
-
var b = new
|
|
32608
|
+
var b = new this._lib.LngLatBounds();
|
|
31101
32609
|
def.data.features.forEach(function extend(f) {
|
|
31102
32610
|
(function walk(c) { if (typeof c[0] === 'number') { b.extend(c); } else { c.forEach(walk); } })(f.geometry.coordinates);
|
|
31103
32611
|
});
|
|
@@ -31111,7 +32619,612 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
31111
32619
|
SMap.prototype.flyTo = function (o) { this.engine.flyTo(o); return this; };
|
|
31112
32620
|
SMap.prototype.remove = function () { clearTimeout(this._refreshTimer); if (this.engine) { this.engine.remove(); } };
|
|
31113
32621
|
|
|
31114
|
-
// ------------------------------------------------------------------
|
|
32622
|
+
// ------------------------------------------------------------------ custom markers (G1), dragging (G2), tooltips (G5)
|
|
32623
|
+
|
|
32624
|
+
// A value that may differ per feature, read in JavaScript (custom markers, tooltips): a constant, a function of the
|
|
32625
|
+
// properties, or { property, default }. Engine expressions (arrays) are not read here and give the fallback.
|
|
32626
|
+
function plainValue(v, props, feature, fallback) {
|
|
32627
|
+
if (v === undefined || v === null) { return fallback; }
|
|
32628
|
+
if (typeof v === 'function') { var r = v(props || {}, feature); return r === undefined ? fallback : r; }
|
|
32629
|
+
if (Array.isArray(v)) { return fallback; }
|
|
32630
|
+
if (typeof v === 'object' && v.property) {
|
|
32631
|
+
var x = props ? props[v.property] : undefined;
|
|
32632
|
+
return x === undefined || x === null ? (v['default'] !== undefined ? v['default'] : fallback) : x;
|
|
32633
|
+
}
|
|
32634
|
+
return v;
|
|
32635
|
+
}
|
|
32636
|
+
|
|
32637
|
+
// Where an element's anchor sits, as the share of its own width and height to move it back by (CSS translate).
|
|
32638
|
+
var ANCHORS = {
|
|
32639
|
+
center: [-50, -50], top: [-50, 0], bottom: [-50, -100], left: [0, -50], right: [-100, -50],
|
|
32640
|
+
'top-left': [0, 0], 'top-right': [-100, 0], 'bottom-left': [0, -100], 'bottom-right': [-100, -100],
|
|
32641
|
+
};
|
|
32642
|
+
// Pixels outside the map an element is still laid out in, so one entering the view is already there.
|
|
32643
|
+
var MARKER_MARGIN = 64;
|
|
32644
|
+
var DRAG_THRESHOLD = 3;
|
|
32645
|
+
|
|
32646
|
+
function featureKey(def, f, i) {
|
|
32647
|
+
var idp = def.o.idProperty;
|
|
32648
|
+
if (idp && f.properties && f.properties[idp] !== undefined && f.properties[idp] !== null) { return String(f.properties[idp]); }
|
|
32649
|
+
if (f.id !== undefined && f.id !== null) { return String(f.id); }
|
|
32650
|
+
return '#' + i;
|
|
32651
|
+
}
|
|
32652
|
+
|
|
32653
|
+
function pointOf(f) {
|
|
32654
|
+
var g = f && f.geometry;
|
|
32655
|
+
return g && g.type === 'Point' && Array.isArray(g.coordinates) && typeof g.coordinates[0] === 'number' ? g.coordinates : null;
|
|
32656
|
+
}
|
|
32657
|
+
|
|
32658
|
+
function offsetOf(v) {
|
|
32659
|
+
if (Array.isArray(v)) { return [+v[0] || 0, +v[1] || 0]; }
|
|
32660
|
+
if (typeof v === 'number') { return [0, -v]; }
|
|
32661
|
+
return [0, 0];
|
|
32662
|
+
}
|
|
32663
|
+
|
|
32664
|
+
// A feature's properties without its position (data given as {lng, lat, …} carries the position in them too), so a
|
|
32665
|
+
// marker that only moved is not rendered again.
|
|
32666
|
+
var POSITION_KEYS = { lng: 1, lat: 1, lon: 1, longitude: 1, latitude: 1, coordinates: 1 };
|
|
32667
|
+
function propsSignature(f) {
|
|
32668
|
+
var p = (f && f.properties) || {}, out = {};
|
|
32669
|
+
Object.keys(p).forEach(function (k) { if (!POSITION_KEYS[k]) { out[k] = p[k]; } });
|
|
32670
|
+
return JSON.stringify(out);
|
|
32671
|
+
}
|
|
32672
|
+
|
|
32673
|
+
/** A copy of a feature collection with one feature's point moved (the caller's own objects are never changed). */
|
|
32674
|
+
function movedCollection(fc, index, lngLat) {
|
|
32675
|
+
var features = fc.features.slice();
|
|
32676
|
+
var f = features[index];
|
|
32677
|
+
features[index] = Object.assign({}, f, { geometry: { type: 'Point', coordinates: [lngLat[0], lngLat[1]] } });
|
|
32678
|
+
return { type: 'FeatureCollection', features: features };
|
|
32679
|
+
}
|
|
32680
|
+
|
|
32681
|
+
/**
|
|
32682
|
+
* Custom markers: the caller's own element at each point — any HTML, laid out above the map's drawn layers. One
|
|
32683
|
+
* element per feature, made the first time the feature comes into view and moved while the map moves; features out
|
|
32684
|
+
* of view are not laid out, so thousands stay smooth.
|
|
32685
|
+
* @param {string} id
|
|
32686
|
+
* @param {Object|Array} data GeoJSON points, or an array of {lng, lat, …properties}
|
|
32687
|
+
* @param {Object} o
|
|
32688
|
+
* render(properties, feature) → an element or trusted HTML (inserted as is, never sanitized); required
|
|
32689
|
+
* anchor: 'center' (default), 'top', 'bottom', 'left', 'right', 'top-left', 'top-right', 'bottom-left', 'bottom-right'
|
|
32690
|
+
* offset: [x, y] pixels; className; opacity, zIndex (constants, functions of the properties or { property });
|
|
32691
|
+
* idProperty (the property naming each feature, else the feature's id, else its place in the data);
|
|
32692
|
+
* riseOnHover (the element under the pointer comes to the front); minzoom; maxzoom;
|
|
32693
|
+
* onClick(feature, event); popup (as on addPoints); tooltip, tooltipOptions (see addPoints);
|
|
32694
|
+
* draggable (true, or per feature), onDragStart / onDrag / onDragEnd(feature, { lngLat, point, originalEvent })
|
|
32695
|
+
*/
|
|
32696
|
+
SMap.prototype.addCustomMarkers = function (id, data, o) {
|
|
32697
|
+
o = o || {};
|
|
32698
|
+
if (typeof o.render !== 'function') { throw new Error('sMap: custom markers need render(properties, feature)'); }
|
|
32699
|
+
if (o.anchor !== undefined && !ANCHORS[o.anchor]) { throw new Error('sMap: unknown anchor "' + o.anchor + '"'); }
|
|
32700
|
+
this._define(id, { kind: 'custom', data: toFeatureCollection(data), o: o });
|
|
32701
|
+
return this;
|
|
32702
|
+
};
|
|
32703
|
+
|
|
32704
|
+
/**
|
|
32705
|
+
* The element of one custom marker (made now if it was not in view yet), or null.
|
|
32706
|
+
* @param {string} id the layer
|
|
32707
|
+
* @param {string|number} key the feature's idProperty value, its id, or '#<index>'
|
|
32708
|
+
*/
|
|
32709
|
+
SMap.prototype.getMarker = function (id, key) {
|
|
32710
|
+
var def = this._layers[id];
|
|
32711
|
+
if (!def || def.kind !== 'custom' || !def.custom) { return null; }
|
|
32712
|
+
var item = def.custom.byKey[String(key)];
|
|
32713
|
+
if (!item) { return null; }
|
|
32714
|
+
if (!item.el) { this._renderMarker(id, def, item); }
|
|
32715
|
+
return item.el;
|
|
32716
|
+
};
|
|
32717
|
+
|
|
32718
|
+
SMap.prototype._markerOverlay = function () {
|
|
32719
|
+
if (this._overlay && this._overlay.parentNode) { return this._overlay; }
|
|
32720
|
+
var m = this.engine;
|
|
32721
|
+
var host = m.getCanvasContainer ? m.getCanvasContainer() : m.getContainer();
|
|
32722
|
+
var doc = host.ownerDocument;
|
|
32723
|
+
var overlay = doc.createElement('div');
|
|
32724
|
+
overlay.className = 'smap-markers';
|
|
32725
|
+
// First in the canvas container after the canvas, so the engine's popups (added after it) stay on top.
|
|
32726
|
+
var canvas = m.getCanvas ? m.getCanvas() : null;
|
|
32727
|
+
if (canvas && canvas.parentNode === host) { host.insertBefore(overlay, canvas.nextSibling); } else { host.appendChild(overlay); }
|
|
32728
|
+
this._overlay = overlay;
|
|
32729
|
+
var self = this;
|
|
32730
|
+
if (!this._overlayBound) {
|
|
32731
|
+
this._overlayBound = true;
|
|
32732
|
+
var frame = null;
|
|
32733
|
+
var schedule = function () {
|
|
32734
|
+
if (frame !== null) { return; }
|
|
32735
|
+
var raf = typeof requestAnimationFrame === 'function' ? requestAnimationFrame : function (fn) { return setTimeout(fn, 16); };
|
|
32736
|
+
frame = raf(function () { frame = null; self._layoutMarkers(); });
|
|
32737
|
+
};
|
|
32738
|
+
this._scheduleLayout = schedule;
|
|
32739
|
+
m.on('move', function () { self._layoutMarkers(); });
|
|
32740
|
+
m.on('resize', schedule);
|
|
32741
|
+
m.on('zoomend', schedule);
|
|
32742
|
+
}
|
|
32743
|
+
return overlay;
|
|
32744
|
+
};
|
|
32745
|
+
|
|
32746
|
+
SMap.prototype._addCustom = function (id, def) {
|
|
32747
|
+
this._dropCustom(id, def);
|
|
32748
|
+
var items = [];
|
|
32749
|
+
var byKey = {};
|
|
32750
|
+
def.data.features.forEach(function (f, i) {
|
|
32751
|
+
var at = pointOf(f);
|
|
32752
|
+
if (!at) { return; }
|
|
32753
|
+
var item = { key: featureKey(def, f, i), index: i, f: f, el: null, shown: false, x: NaN, y: NaN, sig: null };
|
|
32754
|
+
items.push(item);
|
|
32755
|
+
byKey[item.key] = item;
|
|
32756
|
+
});
|
|
32757
|
+
def.custom = { items: items, byKey: byKey };
|
|
32758
|
+
this._markerOverlay();
|
|
32759
|
+
this._layoutMarkers();
|
|
32760
|
+
};
|
|
32761
|
+
|
|
32762
|
+
SMap.prototype._dropCustom = function (id, def) {
|
|
32763
|
+
var c = def.custom;
|
|
32764
|
+
if (!c) { return; }
|
|
32765
|
+
c.items.forEach(function (item) { if (item.el && item.el.parentNode) { item.el.parentNode.removeChild(item.el); } });
|
|
32766
|
+
def.custom = null;
|
|
32767
|
+
if (this._tooltipFor && this._tooltipFor.id === id) { this._hideTooltip(); }
|
|
32768
|
+
};
|
|
32769
|
+
|
|
32770
|
+
// New data for a custom marker layer: an element whose feature (by key) is still there is kept — moved when its point
|
|
32771
|
+
// moved, made again when its properties changed — so a marker being dragged or with its popup open stays the same.
|
|
32772
|
+
SMap.prototype._customData = function (id, def) {
|
|
32773
|
+
var old = def.custom;
|
|
32774
|
+
if (!old || !this.engine) { if (this.engine && this._styleReady) { this._addCustom(id, def); } return; }
|
|
32775
|
+
var items = [];
|
|
32776
|
+
var byKey = {};
|
|
32777
|
+
def.data.features.forEach(function (f, i) {
|
|
32778
|
+
if (!pointOf(f)) { return; }
|
|
32779
|
+
var key = featureKey(def, f, i);
|
|
32780
|
+
var item = old.byKey[key];
|
|
32781
|
+
if (item) {
|
|
32782
|
+
delete old.byKey[key];
|
|
32783
|
+
if (item.el && propsSignature(item.f) !== propsSignature(f)) { item.sig = null; }
|
|
32784
|
+
item.f = f;
|
|
32785
|
+
item.index = i;
|
|
32786
|
+
item.x = NaN;
|
|
32787
|
+
} else {
|
|
32788
|
+
item = { key: key, index: i, f: f, el: null, shown: false, x: NaN, y: NaN, sig: null };
|
|
32789
|
+
}
|
|
32790
|
+
items.push(item);
|
|
32791
|
+
byKey[key] = item;
|
|
32792
|
+
});
|
|
32793
|
+
Object.keys(old.byKey).forEach(function (k) { var gone = old.byKey[k]; if (gone.el && gone.el.parentNode) { gone.el.parentNode.removeChild(gone.el); } });
|
|
32794
|
+
def.custom = { items: items, byKey: byKey };
|
|
32795
|
+
this._layoutMarkers();
|
|
32796
|
+
};
|
|
32797
|
+
|
|
32798
|
+
SMap.prototype._renderMarker = function (id, def, item) {
|
|
32799
|
+
var self = this, o = def.o, doc = this._markerOverlay().ownerDocument;
|
|
32800
|
+
var el = item.el;
|
|
32801
|
+
if (!el) {
|
|
32802
|
+
el = doc.createElement('div');
|
|
32803
|
+
el.setAttribute('data-smap-layer', id);
|
|
32804
|
+
el.setAttribute('data-smap-key', item.key);
|
|
32805
|
+
item.el = el;
|
|
32806
|
+
this._bindMarker(id, def, item);
|
|
32807
|
+
}
|
|
32808
|
+
var props = item.f.properties || {};
|
|
32809
|
+
var content = o.render(props, item.f);
|
|
32810
|
+
while (el.firstChild) { el.removeChild(el.firstChild); }
|
|
32811
|
+
if (isElement(content)) { el.appendChild(content); } else if (content !== undefined && content !== null) { el.innerHTML = String(content); }
|
|
32812
|
+
el.className = 'smap-marker' + (o.className ? ' ' + o.className : '') + (o.onClick || o.popup || o.draggable || o.tooltip || o.riseOnHover ? ' smap-marker-interactive' : '');
|
|
32813
|
+
var opacity = plainValue(o.opacity, props, item.f, 1);
|
|
32814
|
+
el.style.opacity = opacity === 1 ? '' : String(opacity);
|
|
32815
|
+
item.z = +plainValue(o.zIndex, props, item.f, 0) || 0;
|
|
32816
|
+
item.sig = true;
|
|
32817
|
+
return el;
|
|
32818
|
+
};
|
|
32819
|
+
|
|
32820
|
+
// The anchor and offset of a layer's markers as a CSS transform tail.
|
|
32821
|
+
function anchorTransform(o) {
|
|
32822
|
+
var a = ANCHORS[o.anchor || 'center'], off = offsetOf(o.offset);
|
|
32823
|
+
return ' translate(' + a[0] + '%, ' + a[1] + '%)' + (off[0] || off[1] ? ' translate(' + off[0] + 'px, ' + off[1] + 'px)' : '');
|
|
32824
|
+
}
|
|
32825
|
+
|
|
32826
|
+
// Places every custom marker in view (and takes the ones that left the view out of the page).
|
|
32827
|
+
SMap.prototype._layoutMarkers = function () {
|
|
32828
|
+
var m = this.engine;
|
|
32829
|
+
if (!m || !this._overlay) { return; }
|
|
32830
|
+
var host = this._overlay;
|
|
32831
|
+
var w = host.clientWidth || (m.getContainer && m.getContainer().clientWidth) || 0;
|
|
32832
|
+
var h = host.clientHeight || (m.getContainer && m.getContainer().clientHeight) || 0;
|
|
32833
|
+
var zoom = m.getZoom ? m.getZoom() : 0;
|
|
32834
|
+
var rank = 0;
|
|
32835
|
+
for (var n = 0; n < this._order.length; n++) {
|
|
32836
|
+
var id = this._order[n], layer = this._layers[id];
|
|
32837
|
+
// A custom marker layer, or the HTML clusters of a point layer (cluster.html / cluster.ring).
|
|
32838
|
+
var def = layer.kind === 'custom' ? layer : layer.clusterSet;
|
|
32839
|
+
if (!def || !def.custom) { continue; }
|
|
32840
|
+
rank += 1;
|
|
32841
|
+
var o = def.o;
|
|
32842
|
+
var inRange = !layer.hidden && (o.minzoom === undefined || zoom >= o.minzoom) && (o.maxzoom === undefined || zoom < o.maxzoom);
|
|
32843
|
+
var tail = anchorTransform(o);
|
|
32844
|
+
var items = def.custom.items;
|
|
32845
|
+
for (var i = 0; i < items.length; i++) {
|
|
32846
|
+
var item = items[i];
|
|
32847
|
+
var at = item.dragAt || pointOf(item.f);
|
|
32848
|
+
var p = inRange && at ? m.project(at) : null;
|
|
32849
|
+
var visible = !!p && p.x >= -MARKER_MARGIN && p.y >= -MARKER_MARGIN && p.x <= w + MARKER_MARGIN && p.y <= h + MARKER_MARGIN;
|
|
32850
|
+
if (!visible) {
|
|
32851
|
+
if (item.shown && item.el && item.el.parentNode) { item.el.parentNode.removeChild(item.el); }
|
|
32852
|
+
item.shown = false;
|
|
32853
|
+
continue;
|
|
32854
|
+
}
|
|
32855
|
+
if (!item.el || !item.sig) { this._renderMarker(id, def, item); }
|
|
32856
|
+
var x = Math.round(p.x * 10) / 10, y = Math.round(p.y * 10) / 10;
|
|
32857
|
+
if (x !== item.x || y !== item.y) {
|
|
32858
|
+
item.el.style.transform = 'translate(' + x + 'px, ' + y + 'px)' + tail;
|
|
32859
|
+
item.x = x; item.y = y;
|
|
32860
|
+
}
|
|
32861
|
+
var z = rank * 100000 + (item.raised ? 99999 : Math.max(-99999, Math.min(99998, item.z)));
|
|
32862
|
+
if (item.zSet !== z) { item.el.style.zIndex = String(z); item.zSet = z; }
|
|
32863
|
+
if (!item.shown || item.el.parentNode !== host) { host.appendChild(item.el); item.shown = true; }
|
|
32864
|
+
}
|
|
32865
|
+
}
|
|
32866
|
+
};
|
|
32867
|
+
|
|
32868
|
+
// Click, popup, hover raise, tooltip and dragging of one custom marker.
|
|
32869
|
+
SMap.prototype._bindMarker = function (id, def, item) {
|
|
32870
|
+
var self = this, el = item.el;
|
|
32871
|
+
var o = function () { return def.o; };
|
|
32872
|
+
// The click a drag's release makes is no tap: stopped on the way down, before anything inside the marker (the
|
|
32873
|
+
// app's own buttons, a framework listening on its own element) sees it.
|
|
32874
|
+
el.addEventListener('click', function (e) {
|
|
32875
|
+
if (self._dragEndedAt && Date.now() - self._dragEndedAt < 300) { e.stopPropagation(); if (e.preventDefault) { e.preventDefault(); } }
|
|
32876
|
+
}, true);
|
|
32877
|
+
el.addEventListener('click', function (e) {
|
|
32878
|
+
if (self._dragEndedAt && Date.now() - self._dragEndedAt < 300) { e.stopPropagation(); return; }
|
|
32879
|
+
var lo = o();
|
|
32880
|
+
if (!lo.onClick && !lo.popup) { return; }
|
|
32881
|
+
e.stopPropagation();
|
|
32882
|
+
if (lo.onClick) { lo.onClick(item.f, { originalEvent: e, lngLat: pointOf(item.f) }); }
|
|
32883
|
+
if (lo.popup) { self._markerPopup(id, def, item); }
|
|
32884
|
+
});
|
|
32885
|
+
el.addEventListener('mouseenter', function (e) {
|
|
32886
|
+
var lo = o();
|
|
32887
|
+
if (lo.riseOnHover) { item.raised = true; self._layoutMarkers(); }
|
|
32888
|
+
if (lo.tooltip) { self._showTooltip(id, def, item.f, { element: el }); }
|
|
32889
|
+
});
|
|
32890
|
+
el.addEventListener('mouseleave', function () {
|
|
32891
|
+
if (item.raised) { item.raised = false; self._layoutMarkers(); }
|
|
32892
|
+
if (self._tooltipFor && self._tooltipFor.f === item.f) { self._hideTooltip(); }
|
|
32893
|
+
});
|
|
32894
|
+
var start = function (e) {
|
|
32895
|
+
var lo = o();
|
|
32896
|
+
if (!plainValue(lo.draggable, item.f.properties || {}, item.f, false)) { return; }
|
|
32897
|
+
if (e.button !== undefined && e.button !== 0) { return; }
|
|
32898
|
+
self._startMarkerDrag(id, item, e);
|
|
32899
|
+
};
|
|
32900
|
+
if (typeof el.addEventListener === 'function') {
|
|
32901
|
+
el.addEventListener(typeof window !== 'undefined' && window.PointerEvent ? 'pointerdown' : 'mousedown', start);
|
|
32902
|
+
}
|
|
32903
|
+
};
|
|
32904
|
+
|
|
32905
|
+
SMap.prototype._markerPopup = function (id, def, item) {
|
|
32906
|
+
var o = def.o, fn = def.popupFn || o.popup, content;
|
|
32907
|
+
try { content = fn(item.f.properties || {}, item.f); } catch (error) { this._reportError(error); return; }
|
|
32908
|
+
var options = Object.assign({}, o.popupOptions || {});
|
|
32909
|
+
if (options.offset === undefined && this._popupDefaults.offset === undefined && item.el) {
|
|
32910
|
+
// Above the element as it is drawn: from its anchor to its top edge.
|
|
32911
|
+
var hgt = item.el.offsetHeight || 0, a = ANCHORS[o.anchor || 'center'];
|
|
32912
|
+
options.offset = Math.round(hgt * (-a[1] / 100) - offsetOf(o.offset)[1] + 4);
|
|
32913
|
+
}
|
|
32914
|
+
if (options.anchor === undefined && this._popupDefaults.anchor === undefined) { options.anchor = 'bottom'; }
|
|
32915
|
+
this.openPopup(pointOf(item.f), content, options);
|
|
32916
|
+
};
|
|
32917
|
+
|
|
32918
|
+
// Dragging a custom marker: it follows the pointer from the first move of DRAG_THRESHOLD pixels; the map does not
|
|
32919
|
+
// pan meanwhile. Its layer's data then holds the new position (a copy: the caller's objects are not changed).
|
|
32920
|
+
SMap.prototype._startMarkerDrag = function (id, item, e) {
|
|
32921
|
+
var self = this, m = this.engine, el = item.el;
|
|
32922
|
+
var doc = el.ownerDocument, win = doc.defaultView || {};
|
|
32923
|
+
var box = m.getContainer().getBoundingClientRect();
|
|
32924
|
+
var p0 = m.project(pointOf(item.f));
|
|
32925
|
+
var grab = { x: e.clientX - box.left - p0.x, y: e.clientY - box.top - p0.y };
|
|
32926
|
+
var sx = e.clientX, sy = e.clientY, dragging = false;
|
|
32927
|
+
// The map must not pan under a marker that can be dragged: off from the press, back on at the release.
|
|
32928
|
+
var panWasOn = !!(m.dragPan && m.dragPan.isEnabled && m.dragPan.isEnabled());
|
|
32929
|
+
if (panWasOn) { m.dragPan.disable(); }
|
|
32930
|
+
var lngLatAt = function (ev) {
|
|
32931
|
+
var r = m.getContainer().getBoundingClientRect();
|
|
32932
|
+
var pt = { x: ev.clientX - r.left - grab.x, y: ev.clientY - r.top - grab.y };
|
|
32933
|
+
var ll = m.unproject([pt.x, pt.y]);
|
|
32934
|
+
return { lngLat: [ll.lng, ll.lat], point: pt };
|
|
32935
|
+
};
|
|
32936
|
+
var evt = function (ev, at) { return { lngLat: at.lngLat, point: at.point, originalEvent: ev }; };
|
|
32937
|
+
var move = function (ev) {
|
|
32938
|
+
if (!dragging) {
|
|
32939
|
+
if (Math.abs(ev.clientX - sx) < DRAG_THRESHOLD && Math.abs(ev.clientY - sy) < DRAG_THRESHOLD) { return; }
|
|
32940
|
+
dragging = true;
|
|
32941
|
+
el.classList.add('smap-marker-dragging');
|
|
32942
|
+
item.raised = true;
|
|
32943
|
+
self._hideTooltip();
|
|
32944
|
+
var o0 = self._layers[id].o;
|
|
32945
|
+
if (o0.onDragStart) { o0.onDragStart(item.f, evt(ev, lngLatAt(ev))); }
|
|
32946
|
+
}
|
|
32947
|
+
if (ev.preventDefault) { ev.preventDefault(); }
|
|
32948
|
+
var at = lngLatAt(ev);
|
|
32949
|
+
item.dragAt = at.lngLat;
|
|
32950
|
+
self._layoutMarkers();
|
|
32951
|
+
var o1 = self._layers[id] && self._layers[id].o;
|
|
32952
|
+
if (o1 && o1.onDrag) { o1.onDrag(item.f, evt(ev, at)); }
|
|
32953
|
+
};
|
|
32954
|
+
var up = function (ev) {
|
|
32955
|
+
doc.removeEventListener(upType, up);
|
|
32956
|
+
doc.removeEventListener(moveType, move);
|
|
32957
|
+
if (panWasOn) { m.dragPan.enable(); }
|
|
32958
|
+
if (!dragging) { return; }
|
|
32959
|
+
var at = lngLatAt(ev);
|
|
32960
|
+
item.dragAt = null;
|
|
32961
|
+
item.raised = false;
|
|
32962
|
+
el.classList.remove('smap-marker-dragging');
|
|
32963
|
+
self._dragEndedAt = Date.now();
|
|
32964
|
+
var def = self._layers[id];
|
|
32965
|
+
if (!def) { return; }
|
|
32966
|
+
def.data = movedCollection(def.data, item.index, at.lngLat);
|
|
32967
|
+
item.f = def.data.features[item.index];
|
|
32968
|
+
self._layoutMarkers();
|
|
32969
|
+
if (def.o.onDragEnd) { def.o.onDragEnd(item.f, evt(ev, at)); }
|
|
32970
|
+
};
|
|
32971
|
+
var pointer = !!win.PointerEvent && e.type === 'pointerdown';
|
|
32972
|
+
var moveType = pointer ? 'pointermove' : 'mousemove';
|
|
32973
|
+
var upType = pointer ? 'pointerup' : 'mouseup';
|
|
32974
|
+
doc.addEventListener(moveType, move);
|
|
32975
|
+
doc.addEventListener(upType, up);
|
|
32976
|
+
if (e.stopPropagation) { e.stopPropagation(); }
|
|
32977
|
+
};
|
|
32978
|
+
|
|
32979
|
+
// Dragging a drawn point (addPoints with draggable): the point is taken out of its layer and drawn in a copy of the
|
|
32980
|
+
// layer's own look that follows the pointer, then put back at the new position. Two data updates per drag, whatever
|
|
32981
|
+
// the size of the layer.
|
|
32982
|
+
SMap.prototype._dragPoints = function (id, def, main) {
|
|
32983
|
+
var self = this, m = this.engine;
|
|
32984
|
+
var begin = function (e) {
|
|
32985
|
+
var f = e.features && e.features[0];
|
|
32986
|
+
if (!f || def.hidden) { return; }
|
|
32987
|
+
var index = self._featureIndex(def, f);
|
|
32988
|
+
if (index < 0) { return; }
|
|
32989
|
+
var original = def.data.features[index];
|
|
32990
|
+
if (!plainValue(def.o.draggable, original.properties || {}, original, false)) { return; }
|
|
32991
|
+
if (e.originalEvent && e.originalEvent.button !== undefined && e.originalEvent.button !== 0) { return; }
|
|
32992
|
+
e.preventDefault();
|
|
32993
|
+
var start = e.point, dragging = false, ghost = null;
|
|
32994
|
+
var at = function (ev) { return { lngLat: [ev.lngLat.lng, ev.lngLat.lat], point: { x: ev.point.x, y: ev.point.y }, originalEvent: ev.originalEvent }; };
|
|
32995
|
+
var move = function (ev) {
|
|
32996
|
+
if (!dragging) {
|
|
32997
|
+
if (Math.abs(ev.point.x - start.x) < DRAG_THRESHOLD && Math.abs(ev.point.y - start.y) < DRAG_THRESHOLD) { return; }
|
|
32998
|
+
dragging = true;
|
|
32999
|
+
if (m.dragPan && m.dragPan.disable) { m.dragPan.disable(); }
|
|
33000
|
+
ghost = self._dragGhost(id, def);
|
|
33001
|
+
var without = def.data.features.slice();
|
|
33002
|
+
without.splice(index, 1);
|
|
33003
|
+
var src = m.getSource('smap-' + id);
|
|
33004
|
+
if (src) { src.setData({ type: 'FeatureCollection', features: without }); }
|
|
33005
|
+
if (def.hovered) { self._setHover(id, def, null); }
|
|
33006
|
+
self._hideTooltip();
|
|
33007
|
+
if (def.o.onDragStart) { def.o.onDragStart(original, at(ev)); }
|
|
33008
|
+
}
|
|
33009
|
+
var a = at(ev);
|
|
33010
|
+
if (ghost) { ghost.setData({ type: 'FeatureCollection', features: [Object.assign({}, original, { geometry: { type: 'Point', coordinates: a.lngLat } })] }); }
|
|
33011
|
+
if (def.o.onDrag) { def.o.onDrag(original, a); }
|
|
33012
|
+
};
|
|
33013
|
+
var end = function (ev) {
|
|
33014
|
+
m.off('mousemove', move); m.off('touchmove', move);
|
|
33015
|
+
m.off('mouseup', end); m.off('touchend', end);
|
|
33016
|
+
if (!dragging) { return; }
|
|
33017
|
+
if (m.dragPan && m.dragPan.enable) { m.dragPan.enable(); }
|
|
33018
|
+
self._dropGhost(id);
|
|
33019
|
+
self._dragEndedAt = Date.now();
|
|
33020
|
+
var a = at(ev);
|
|
33021
|
+
def.data = movedCollection(def.data, index, a.lngLat);
|
|
33022
|
+
var src = m.getSource('smap-' + id);
|
|
33023
|
+
if (src) { src.setData(def.data); }
|
|
33024
|
+
if (def.o.onDragEnd) { def.o.onDragEnd(def.data.features[index], a); }
|
|
33025
|
+
};
|
|
33026
|
+
m.on('mousemove', move); m.on('touchmove', move);
|
|
33027
|
+
m.on('mouseup', end); m.on('touchend', end);
|
|
33028
|
+
};
|
|
33029
|
+
this._listen(id, 'mousedown', main, begin);
|
|
33030
|
+
this._listen(id, 'touchstart', main, begin);
|
|
33031
|
+
};
|
|
33032
|
+
|
|
33033
|
+
// The index in a layer's data of a feature the engine answered (by idProperty, else by the generated id).
|
|
33034
|
+
SMap.prototype._featureIndex = function (def, f) {
|
|
33035
|
+
var feats = def.data.features, idp = def.o.idProperty;
|
|
33036
|
+
if (idp) {
|
|
33037
|
+
var want = f.properties ? f.properties[idp] : undefined;
|
|
33038
|
+
for (var i = 0; i < feats.length; i++) { if (feats[i].properties && feats[i].properties[idp] === want) { return i; } }
|
|
33039
|
+
return -1;
|
|
33040
|
+
}
|
|
33041
|
+
return typeof f.id === 'number' && f.id >= 0 && f.id < feats.length ? f.id : -1;
|
|
33042
|
+
};
|
|
33043
|
+
|
|
33044
|
+
// A copy of a layer's engine layers drawing from a one-feature source on top of the map: the point being dragged.
|
|
33045
|
+
SMap.prototype._dragGhost = function (id, def) {
|
|
33046
|
+
var m = this.engine, src = 'smap-' + id, ghostSrc = src + '-drag';
|
|
33047
|
+
this._dropGhost(id);
|
|
33048
|
+
m.addSource(ghostSrc, { type: 'geojson', data: { type: 'FeatureCollection', features: [] } });
|
|
33049
|
+
var st = m.getStyle ? m.getStyle() : null;
|
|
33050
|
+
var ids = [];
|
|
33051
|
+
(st && st.layers ? st.layers : []).forEach(function (l) {
|
|
33052
|
+
if (l.source !== src || def.engineLayers.indexOf(l.id) < 0 || /-(clusters|count)$/.test(l.id)) { return; }
|
|
33053
|
+
var copy = JSON.parse(JSON.stringify(l));
|
|
33054
|
+
copy.id = l.id + '-drag';
|
|
33055
|
+
copy.source = ghostSrc;
|
|
33056
|
+
delete copy.minzoom; delete copy.maxzoom;
|
|
33057
|
+
copy.layout = Object.assign({}, copy.layout || {}, { visibility: 'visible' });
|
|
33058
|
+
m.addLayer(copy);
|
|
33059
|
+
ids.push(copy.id);
|
|
33060
|
+
});
|
|
33061
|
+
this._ghost = { id: id, source: ghostSrc, layers: ids };
|
|
33062
|
+
return m.getSource(ghostSrc);
|
|
33063
|
+
};
|
|
33064
|
+
|
|
33065
|
+
SMap.prototype._dropGhost = function (id) {
|
|
33066
|
+
var g = this._ghost, m = this.engine;
|
|
33067
|
+
if (!g || (id && g.id !== id)) { return; }
|
|
33068
|
+
g.layers.forEach(function (l) { if (m.getLayer(l)) { m.removeLayer(l); } });
|
|
33069
|
+
if (m.getSource(g.source)) { m.removeSource(g.source); }
|
|
33070
|
+
this._ghost = null;
|
|
33071
|
+
};
|
|
33072
|
+
|
|
33073
|
+
// ---- tooltips (G5)
|
|
33074
|
+
|
|
33075
|
+
/**
|
|
33076
|
+
* The map's one tooltip: shown while the pointer is over a feature of a layer with `tooltip`, beside the point (or
|
|
33077
|
+
* the custom marker), or at the pointer on lines and areas. `tooltipOptions`: direction ('top' default, 'bottom',
|
|
33078
|
+
* 'left', 'right'), offset ([x, y] pixels), className.
|
|
33079
|
+
*/
|
|
33080
|
+
SMap.prototype._showTooltip = function (id, def, f, at) {
|
|
33081
|
+
var o = def.o, content;
|
|
33082
|
+
var fn = typeof o.tooltip === 'string' ? (def.tooltipFn = def.tooltipFn || popupTemplate(o.tooltip)) : o.tooltip;
|
|
33083
|
+
try { content = fn(f.properties || {}, f); } catch (error) { this._reportError(error); return; }
|
|
33084
|
+
if (content === undefined || content === null || content === '') { this._hideTooltip(); return; }
|
|
33085
|
+
var m = this.engine, overlay = this._markerOverlay(), doc = overlay.ownerDocument;
|
|
33086
|
+
var tip = this._tooltip;
|
|
33087
|
+
if (!tip || tip.parentNode !== overlay) {
|
|
33088
|
+
tip = this._tooltip = doc.createElement('div');
|
|
33089
|
+
overlay.appendChild(tip);
|
|
33090
|
+
}
|
|
33091
|
+
var to = o.tooltipOptions || {};
|
|
33092
|
+
var dir = ['top', 'bottom', 'left', 'right'].indexOf(to.direction) >= 0 ? to.direction : 'top';
|
|
33093
|
+
tip.className = 'smap-tooltip smap-tooltip-' + dir + (to.className ? ' ' + to.className : '');
|
|
33094
|
+
while (tip.firstChild) { tip.removeChild(tip.firstChild); }
|
|
33095
|
+
if (isElement(content)) { tip.appendChild(content); } else { tip.innerHTML = String(content); }
|
|
33096
|
+
var x, y, gap = 6;
|
|
33097
|
+
if (at.element) {
|
|
33098
|
+
var hb = overlay.getBoundingClientRect(), eb = at.element.getBoundingClientRect();
|
|
33099
|
+
x = eb.left - hb.left + eb.width / 2; y = eb.top - hb.top + eb.height / 2;
|
|
33100
|
+
if (dir === 'top') { y = eb.top - hb.top; } else if (dir === 'bottom') { y = eb.bottom - hb.top; }
|
|
33101
|
+
else if (dir === 'left') { x = eb.left - hb.left; } else { x = eb.right - hb.left; }
|
|
33102
|
+
} else {
|
|
33103
|
+
var p = at.point || m.project(at.lngLat);
|
|
33104
|
+
x = p.x; y = p.y;
|
|
33105
|
+
gap = at.gap !== undefined ? at.gap : 10;
|
|
33106
|
+
}
|
|
33107
|
+
var off = offsetOf(to.offset);
|
|
33108
|
+
var shift = { top: [-50, -100, 0, -gap], bottom: [-50, 0, 0, gap], left: [-100, -50, -gap, 0], right: [0, -50, gap, 0] }[dir];
|
|
33109
|
+
tip.style.transform = 'translate(' + Math.round(x + shift[2] + off[0]) + 'px, ' + Math.round(y + shift[3] + off[1]) + 'px) translate(' + shift[0] + '%, ' + shift[1] + '%)';
|
|
33110
|
+
this._tooltipFor = { id: id, f: f };
|
|
33111
|
+
};
|
|
33112
|
+
|
|
33113
|
+
SMap.prototype._hideTooltip = function () {
|
|
33114
|
+
if (this._tooltip && this._tooltip.parentNode) { this._tooltip.parentNode.removeChild(this._tooltip); }
|
|
33115
|
+
this._tooltipFor = null;
|
|
33116
|
+
};
|
|
33117
|
+
|
|
33118
|
+
// Tooltips of a drawn layer: points show theirs beside the point, lines and areas at the pointer.
|
|
33119
|
+
SMap.prototype._tooltips = function (id, def, main) {
|
|
33120
|
+
var self = this, m = this.engine;
|
|
33121
|
+
this._listen(id, 'mousemove', main, function (e) {
|
|
33122
|
+
var f = e.features && e.features[0];
|
|
33123
|
+
if (!f || def.hidden) { return; }
|
|
33124
|
+
if (def.kind === 'points' && pointOf(f) && !(f.properties && f.properties.point_count)) {
|
|
33125
|
+
var mk = def.o.marker, k = mk ? scaleAt(mk.scale, m.getZoom()) : 1;
|
|
33126
|
+
var size = mk ? (mk.size || 32) * k : (def.o.radius || 6) * 2;
|
|
33127
|
+
var gap = mk && (mk.shape === undefined || mk.shape === 'pin') ? Math.round(size * 1.25) : Math.round(size / 2 + 4);
|
|
33128
|
+
self._showTooltip(id, def, f, { lngLat: pointOf(f), gap: gap });
|
|
33129
|
+
} else {
|
|
33130
|
+
self._showTooltip(id, def, f, { point: e.point, gap: 12 });
|
|
33131
|
+
}
|
|
33132
|
+
});
|
|
33133
|
+
this._listen(id, 'mouseleave', main, function () { if (self._tooltipFor && self._tooltipFor.id === id) { self._hideTooltip(); } });
|
|
33134
|
+
};
|
|
33135
|
+
|
|
33136
|
+
// ---- clusters drawn as HTML (G6)
|
|
33137
|
+
|
|
33138
|
+
// Colours of a ring: each value's share of the points, largest first; up to six slices, the rest in the last.
|
|
33139
|
+
function ringBackground(counts, fallback) {
|
|
33140
|
+
var keys = Object.keys(counts).sort(function (a, b) { return counts[b] - counts[a]; });
|
|
33141
|
+
var total = keys.reduce(function (t, k) { return t + counts[k]; }, 0);
|
|
33142
|
+
if (!total) { return fallback; }
|
|
33143
|
+
if (keys.length === 1) { return keys[0]; }
|
|
33144
|
+
var shown = keys.slice(0, 6), at = 0;
|
|
33145
|
+
return 'conic-gradient(' + shown.map(function (c, i) {
|
|
33146
|
+
var from = at;
|
|
33147
|
+
at = i === shown.length - 1 ? 100 : Math.min(100, at + Math.round(counts[c] / total * 100));
|
|
33148
|
+
return c + ' ' + from + '% ' + at + '%';
|
|
33149
|
+
}).join(', ') + ')';
|
|
33150
|
+
}
|
|
33151
|
+
|
|
33152
|
+
// The cluster as the caller's html() sees it.
|
|
33153
|
+
SMap.prototype._clusterInfo = function (src, f) {
|
|
33154
|
+
var m = this.engine, p = f.properties || {};
|
|
33155
|
+
return {
|
|
33156
|
+
id: p.cluster_id, count: p.point_count, properties: p, lngLat: pointOf(f),
|
|
33157
|
+
leaves: function (limit) {
|
|
33158
|
+
var source = m.getSource(src);
|
|
33159
|
+
return Promise.resolve(source.getClusterLeaves(p.cluster_id, limit === undefined ? Infinity : limit, 0));
|
|
33160
|
+
},
|
|
33161
|
+
};
|
|
33162
|
+
};
|
|
33163
|
+
|
|
33164
|
+
// HTML clusters of a point layer: the clusters the engine made are read after each change of view or data and laid
|
|
33165
|
+
// out as custom markers (kept by cluster id, so an unchanged cluster keeps its element).
|
|
33166
|
+
SMap.prototype._htmlClusters = function (id, def, src) {
|
|
33167
|
+
var self = this, m = this.engine, co = def.o.cluster;
|
|
33168
|
+
var ring = co.ring ? (typeof co.ring === 'string' ? { property: co.ring } : co.ring) : null;
|
|
33169
|
+
var render = function (props, f) {
|
|
33170
|
+
var info = self._clusterInfo(src, f);
|
|
33171
|
+
if (co.html) { return co.html(info); }
|
|
33172
|
+
var size = info.count >= 100 ? 44 : (info.count >= 10 ? 38 : 32);
|
|
33173
|
+
var doc = self._markerOverlay().ownerDocument;
|
|
33174
|
+
var el = doc.createElement('div');
|
|
33175
|
+
el.className = 'smap-cluster';
|
|
33176
|
+
el.style.width = el.style.height = size + 'px';
|
|
33177
|
+
el.style.background = ring.color || def.o.clusterColor || def.o.color || '#4f46e5';
|
|
33178
|
+
var b = doc.createElement('b');
|
|
33179
|
+
b.textContent = String(info.count);
|
|
33180
|
+
el.appendChild(b);
|
|
33181
|
+
// The ring: the share of each value among the cluster's points (read from at most ring.limit of them).
|
|
33182
|
+
info.leaves(ring.limit || 500).then(function (leaves) {
|
|
33183
|
+
var counts = {};
|
|
33184
|
+
(leaves || []).forEach(function (leaf) {
|
|
33185
|
+
var c = leaf.properties ? leaf.properties[ring.property] : undefined;
|
|
33186
|
+
if (typeof c === 'string' && c) { counts[c] = (counts[c] || 0) + 1; }
|
|
33187
|
+
});
|
|
33188
|
+
el.style.background = ringBackground(counts, el.style.background);
|
|
33189
|
+
}, function () {});
|
|
33190
|
+
return el;
|
|
33191
|
+
};
|
|
33192
|
+
def.clusterSet = {
|
|
33193
|
+
o: {
|
|
33194
|
+
render: render, anchor: 'center', className: co.className, riseOnHover: true,
|
|
33195
|
+
// A click zooms in to where the cluster splits (cluster.zoomOnClick: false turns it off).
|
|
33196
|
+
onClick: co.zoomOnClick === false ? co.onClick : function (f, e) {
|
|
33197
|
+
if (co.onClick) { co.onClick(f, e); }
|
|
33198
|
+
var source = m.getSource(src);
|
|
33199
|
+
Promise.resolve(source.getClusterExpansionZoom(f.properties.cluster_id)).then(function (zoom) {
|
|
33200
|
+
m.easeTo({ center: pointOf(f), zoom: zoom });
|
|
33201
|
+
}, function () {});
|
|
33202
|
+
},
|
|
33203
|
+
tooltip: co.tooltip ? function (p, f) { return co.tooltip(self._clusterInfo(src, f)); } : undefined,
|
|
33204
|
+
},
|
|
33205
|
+
data: { type: 'FeatureCollection', features: [] },
|
|
33206
|
+
custom: null,
|
|
33207
|
+
};
|
|
33208
|
+
var refresh = function () {
|
|
33209
|
+
var set = def.clusterSet;
|
|
33210
|
+
if (!set || !m.getSource(src)) { return; }
|
|
33211
|
+
var seen = {}, features = [];
|
|
33212
|
+
(m.querySourceFeatures(src, { filter: ['has', 'point_count'] }) || []).forEach(function (f) {
|
|
33213
|
+
var cid = f.properties && f.properties.cluster_id;
|
|
33214
|
+
if (cid === undefined || seen[cid]) { return; }
|
|
33215
|
+
seen[cid] = true;
|
|
33216
|
+
features.push({ type: 'Feature', id: 'c' + cid, properties: f.properties, geometry: { type: 'Point', coordinates: pointOf(f) } });
|
|
33217
|
+
});
|
|
33218
|
+
set.data = { type: 'FeatureCollection', features: features };
|
|
33219
|
+
self._markerOverlay();
|
|
33220
|
+
self._customData(id + ':clusters', set);
|
|
33221
|
+
};
|
|
33222
|
+
this._listen(id, 'moveend', null, refresh);
|
|
33223
|
+
this._listen(id, 'sourcedata', null, function (e) { if (e && e.sourceId === src && e.isSourceLoaded) { refresh(); } });
|
|
33224
|
+
refresh();
|
|
33225
|
+
};
|
|
33226
|
+
|
|
33227
|
+
// ------------------------------------------------------------------ map tools (M49-M57)
|
|
31115
33228
|
|
|
31116
33229
|
var TOOL_ID = /^[A-Za-z0-9._-]{1,64}$/;
|
|
31117
33230
|
var TOOL_CORNERS = ['top-left', 'top-right', 'bottom-left', 'bottom-right'];
|
|
@@ -31125,6 +33238,8 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
31125
33238
|
zoom: { control: 'NavigationControl', label: 'Zoom', position: 'top-left', options: { showCompass: false } },
|
|
31126
33239
|
compass: { control: 'NavigationControl', label: 'Compass', position: 'top-left', options: { showZoom: false } },
|
|
31127
33240
|
locate: { control: 'GeolocateControl', label: 'Locate', position: 'top-left' },
|
|
33241
|
+
// Made by the SDK itself: the base map and overlay switcher (G8).
|
|
33242
|
+
layers: { sdk: 'layers', label: 'Layers', position: 'top-right' },
|
|
31128
33243
|
};
|
|
31129
33244
|
var TOOLS_ICON = '<svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true" focusable="false" fill="none" ' +
|
|
31130
33245
|
'stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M4 7h10M18 7h2M4 17h2M10 17h10"/>' +
|
|
@@ -31195,7 +33310,7 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
31195
33310
|
}
|
|
31196
33311
|
if (typeof target === 'string') {
|
|
31197
33312
|
if (!kind) { throw new Error('sMap: unknown tool kind "' + target + '"'); }
|
|
31198
|
-
if (!
|
|
33313
|
+
if (!kind.sdk && !this._lib[kind.control]) { throw new Error('sMap: the rendering engine has no ' + kind.control); }
|
|
31199
33314
|
} else if (!isElement(target) && !(target && typeof target.onAdd === 'function')) {
|
|
31200
33315
|
throw new Error('sMap: a tool is an element, an engine control or a tool kind');
|
|
31201
33316
|
}
|
|
@@ -31204,7 +33319,8 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
31204
33319
|
if (position === 'none' && !isElement(target)) { throw new Error('sMap: only an element may stay out of the map\'s corners'); }
|
|
31205
33320
|
if (this._toolIndex(id) >= 0) { this.removeTool(id); }
|
|
31206
33321
|
var tool = { id: id, label: String(o.label || (kind ? kind.label : id)), position: position, el: null, control: null };
|
|
31207
|
-
if (kind) { tool.control = toolControl(tool,
|
|
33322
|
+
if (kind && kind.sdk === 'layers') { tool.control = toolControl(tool, layersControl(this, o.options || {})); }
|
|
33323
|
+
else if (kind) { tool.control = toolControl(tool, new this._lib[kind.control](o.options || kind.options)); }
|
|
31208
33324
|
else if (!isElement(target)) { tool.control = toolControl(tool, target); }
|
|
31209
33325
|
else if (position !== 'none') { tool.control = elementControl(tool, target); }
|
|
31210
33326
|
else { tool.el = target; }
|
|
@@ -31457,6 +33573,1119 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
31457
33573
|
return control;
|
|
31458
33574
|
}
|
|
31459
33575
|
|
|
33576
|
+
// ------------------------------------------------------------------ heat maps (G7), terrain (G11), synced maps (G13), drawn tiles (G14)
|
|
33577
|
+
|
|
33578
|
+
/**
|
|
33579
|
+
* A heat map of points: weight (a number, or per feature: { property } or an expression; default 1), radius
|
|
33580
|
+
* (pixels, default 20, or an expression of the zoom), intensity (default 1), opacity (default 0.8), colors (from low
|
|
33581
|
+
* to high, or [[density 0–1, colour], …]), minzoom, maxzoom, slot, before, zIndex.
|
|
33582
|
+
*/
|
|
33583
|
+
SMap.prototype.addHeatmap = function (id, data, o) {
|
|
33584
|
+
this._define(id, { kind: 'heatmap', data: toFeatureCollection(data), o: o || {} });
|
|
33585
|
+
return this;
|
|
33586
|
+
};
|
|
33587
|
+
|
|
33588
|
+
var TERRAIN_SOURCE = 'smap-terrain';
|
|
33589
|
+
var HILLSHADE_LAYER = 'smap-hillshade';
|
|
33590
|
+
|
|
33591
|
+
/**
|
|
33592
|
+
* Relief: the map drawn over elevation tiles (seen when the map is tilted), with optional hillshading.
|
|
33593
|
+
* @param {Object|null} terrain { tiles: [url with {z}/{x}/{y}], encoding: 'terrarium' | 'mapbox' (default terrarium),
|
|
33594
|
+
* tileSize (256), maxzoom (15), attribution, exaggeration (1), hillshade (true, or { exaggeration, shadow,
|
|
33595
|
+
* highlight }) }; null takes it off
|
|
33596
|
+
*/
|
|
33597
|
+
SMap.prototype.setTerrain = function (terrain) {
|
|
33598
|
+
if (terrain && (!Array.isArray(terrain.tiles) || !terrain.tiles.length)) { throw new Error('sMap: terrain needs tiles'); }
|
|
33599
|
+
this._terrain = terrain ? Object.assign({}, terrain) : null;
|
|
33600
|
+
if (this.engine && this._styleReady) { this._applyTerrain(); }
|
|
33601
|
+
return this;
|
|
33602
|
+
};
|
|
33603
|
+
|
|
33604
|
+
SMap.prototype._applyTerrain = function () {
|
|
33605
|
+
var m = this.engine, t = this._terrain;
|
|
33606
|
+
if (m.setTerrain) { m.setTerrain(null); }
|
|
33607
|
+
if (m.getLayer(HILLSHADE_LAYER)) { m.removeLayer(HILLSHADE_LAYER); }
|
|
33608
|
+
if (m.getSource(TERRAIN_SOURCE)) { m.removeSource(TERRAIN_SOURCE); }
|
|
33609
|
+
if (m.getSource(TERRAIN_SOURCE + '-shade')) { m.removeSource(TERRAIN_SOURCE + '-shade'); }
|
|
33610
|
+
if (!t) { return; }
|
|
33611
|
+
var source = { type: 'raster-dem', tiles: t.tiles.slice(), tileSize: t.tileSize || 256, encoding: t.encoding || 'terrarium', maxzoom: t.maxzoom || 15 };
|
|
33612
|
+
if (t.attribution) { source.attribution = t.attribution; }
|
|
33613
|
+
m.addSource(TERRAIN_SOURCE, source);
|
|
33614
|
+
if (t.hillshade !== false) {
|
|
33615
|
+
var hs = t.hillshade && typeof t.hillshade === 'object' ? t.hillshade : {};
|
|
33616
|
+
var paint = { 'hillshade-exaggeration': hs.exaggeration !== undefined ? hs.exaggeration : 0.35 };
|
|
33617
|
+
if (hs.shadow) { paint['hillshade-shadow-color'] = hs.shadow; }
|
|
33618
|
+
if (hs.highlight) { paint['hillshade-highlight-color'] = hs.highlight; }
|
|
33619
|
+
// Over land and water, under the roads; from a source of its own (the engine draws the relief and the shading
|
|
33620
|
+
// better from two sources than from one).
|
|
33621
|
+
m.addSource(TERRAIN_SOURCE + '-shade', Object.assign({}, source, { tiles: source.tiles.slice() }));
|
|
33622
|
+
m.addLayer({ id: HILLSHADE_LAYER, type: 'hillshade', source: TERRAIN_SOURCE + '-shade', paint: paint }, this.slots().roads || undefined);
|
|
33623
|
+
}
|
|
33624
|
+
if (m.setTerrain) { m.setTerrain({ source: TERRAIN_SOURCE, exaggeration: t.exaggeration || 1 }); }
|
|
33625
|
+
};
|
|
33626
|
+
|
|
33627
|
+
/**
|
|
33628
|
+
* Keeps another sMap map on this map's view, both ways (a picture in picture, a before / after pair). `offsetZoom`
|
|
33629
|
+
* is added to this map's zoom on the other (e.g. -3 for an overview). Returns a function that ends it.
|
|
33630
|
+
*/
|
|
33631
|
+
SMap.prototype.syncWith = function (other, options) {
|
|
33632
|
+
var self = this, offset = (options && options.offsetZoom) || 0, busy = false;
|
|
33633
|
+
if (!other || !(other instanceof SMap)) { throw new Error('sMap: syncWith takes another sMap map'); }
|
|
33634
|
+
var copy = function (from, to, dz) {
|
|
33635
|
+
return function () {
|
|
33636
|
+
if (busy || !from.engine || !to.engine) { return; }
|
|
33637
|
+
busy = true;
|
|
33638
|
+
try {
|
|
33639
|
+
var c = from.engine.getCenter();
|
|
33640
|
+
to.engine.jumpTo({ center: [c.lng, c.lat], zoom: from.engine.getZoom() + dz, bearing: from.engine.getBearing(), pitch: from.engine.getPitch() });
|
|
33641
|
+
} finally { busy = false; }
|
|
33642
|
+
};
|
|
33643
|
+
};
|
|
33644
|
+
var a = copy(this, other, offset), b = copy(other, this, -offset);
|
|
33645
|
+
Promise.all([this.ready, other.ready]).then(function () {
|
|
33646
|
+
if (!a) { return; }
|
|
33647
|
+
self.engine.on('move', a);
|
|
33648
|
+
other.engine.on('move', b);
|
|
33649
|
+
a();
|
|
33650
|
+
});
|
|
33651
|
+
return function () {
|
|
33652
|
+
if (self.engine) { self.engine.off('move', a); }
|
|
33653
|
+
if (other.engine) { other.engine.off('move', b); }
|
|
33654
|
+
a = null;
|
|
33655
|
+
};
|
|
33656
|
+
};
|
|
33657
|
+
|
|
33658
|
+
// Tiles drawn by the page: one protocol for every map, each layer its own key.
|
|
33659
|
+
var canvasTiles = {};
|
|
33660
|
+
var canvasSeq = 0;
|
|
33661
|
+
var canvasRegistered = false;
|
|
33662
|
+
function registerCanvasProtocol(doc) {
|
|
33663
|
+
if (canvasRegistered) { return; }
|
|
33664
|
+
canvasRegistered = true;
|
|
33665
|
+
var handler = function (params) {
|
|
33666
|
+
var m = /^smapcanvas:\/\/([^/]+)\/(\d+)\/(\d+)\/(\d+)/.exec(params.url);
|
|
33667
|
+
var entry = m && canvasTiles[m[1]];
|
|
33668
|
+
if (!entry) { return Promise.reject(new Error('sMap: no drawn tiles for ' + params.url)); }
|
|
33669
|
+
var size = entry.tileSize * entry.ratio;
|
|
33670
|
+
var canvas = entry.doc.createElement('canvas');
|
|
33671
|
+
canvas.width = size; canvas.height = size;
|
|
33672
|
+
var ctx = canvas.getContext('2d');
|
|
33673
|
+
ctx.scale(entry.ratio, entry.ratio);
|
|
33674
|
+
return Promise.resolve(entry.draw(ctx, +m[2], +m[3], +m[4], { size: entry.tileSize, canvas: canvas })).then(function () {
|
|
33675
|
+
return new Promise(function (resolve, reject) {
|
|
33676
|
+
canvas.toBlob(function (blob) {
|
|
33677
|
+
if (!blob) { reject(new Error('sMap: a drawn tile could not be encoded')); return; }
|
|
33678
|
+
blob.arrayBuffer().then(function (data) { resolve({ data: data }); }, reject);
|
|
33679
|
+
}, 'image/png');
|
|
33680
|
+
});
|
|
33681
|
+
});
|
|
33682
|
+
};
|
|
33683
|
+
if (engine && engine.addProtocol) { engine.addProtocol('smapcanvas', handler); }
|
|
33684
|
+
if (canvasLib) { canvasLib.addProtocol('smapcanvas', handler); }
|
|
33685
|
+
}
|
|
33686
|
+
|
|
33687
|
+
/**
|
|
33688
|
+
* Raster tiles drawn by the page: draw(ctx, z, x, y, { size, canvas }) paints one tile (a 2D context already scaled
|
|
33689
|
+
* to `size` × `size` CSS pixels; may return a promise). Options as addRasterLayer (tileSize 256, minzoom, maxzoom,
|
|
33690
|
+
* opacity, attribution, slot, before, zIndex).
|
|
33691
|
+
*/
|
|
33692
|
+
SMap.prototype.addCanvasTiles = function (id, draw, o) {
|
|
33693
|
+
if (typeof draw !== 'function') { throw new Error('sMap: addCanvasTiles needs draw(ctx, z, x, y)'); }
|
|
33694
|
+
o = o || {};
|
|
33695
|
+
var doc = (this.engine && this.engine.getContainer().ownerDocument) || (typeof document !== 'undefined' ? document : null);
|
|
33696
|
+
if (!doc) { throw new Error('sMap: addCanvasTiles needs a page'); }
|
|
33697
|
+
registerCanvasProtocol(doc);
|
|
33698
|
+
var key = 'c' + (++canvasSeq);
|
|
33699
|
+
var win = doc.defaultView || {};
|
|
33700
|
+
canvasTiles[key] = { draw: draw, tileSize: o.tileSize || 256, ratio: Math.min(2, Math.max(1, Math.ceil(win.devicePixelRatio || 1))), doc: doc };
|
|
33701
|
+
var source = { type: 'raster', tiles: ['smapcanvas://' + key + '/{z}/{x}/{y}'], tileSize: o.tileSize || 256 };
|
|
33702
|
+
copyKeys(o, source, ['attribution', 'minzoom', 'maxzoom', 'bounds']);
|
|
33703
|
+
return this._addRaster(id, source, o);
|
|
33704
|
+
};
|
|
33705
|
+
|
|
33706
|
+
// ------------------------------------------------------------------ drawing and editing shapes (G3), area selection (G4)
|
|
33707
|
+
|
|
33708
|
+
var DRAW_TEXT = {
|
|
33709
|
+
polygon: 'Polygon', rectangle: 'Rectangle', circle: 'Circle', line: 'Line', point: 'Point', hole: 'Hole',
|
|
33710
|
+
delete: 'Delete', undo: 'Undo', redo: 'Redo', finish: 'Finish', cancel: 'Cancel',
|
|
33711
|
+
};
|
|
33712
|
+
var DRAW_MODES = ['polygon', 'rectangle', 'circle', 'line', 'point'];
|
|
33713
|
+
var SNAP_PX = 10;
|
|
33714
|
+
var drawSeq = 0;
|
|
33715
|
+
|
|
33716
|
+
function closeRing(ring) {
|
|
33717
|
+
var r = ring.slice();
|
|
33718
|
+
if (r.length && (r[0][0] !== r[r.length - 1][0] || r[0][1] !== r[r.length - 1][1])) { r.push(r[0].slice()); }
|
|
33719
|
+
return r;
|
|
33720
|
+
}
|
|
33721
|
+
function openRing(ring) { return ring.length > 1 && ring[0][0] === ring[ring.length - 1][0] && ring[0][1] === ring[ring.length - 1][1] ? ring.slice(0, -1) : ring.slice(); }
|
|
33722
|
+
|
|
33723
|
+
// A circle of `radius` metres as a ring of 64 points.
|
|
33724
|
+
function circleRing(center, radius) {
|
|
33725
|
+
// The same Earth radius as sMap.geo.distance, so the radius handle measures what was drawn.
|
|
33726
|
+
var ring = [], R = 6371008.8, lat = center[1] * Math.PI / 180;
|
|
33727
|
+
for (var i = 0; i <= 64; i++) {
|
|
33728
|
+
var a = (i % 64) / 64 * 2 * Math.PI;
|
|
33729
|
+
ring.push([center[0] + (radius * Math.sin(a)) / (R * Math.cos(lat)) * 180 / Math.PI, center[1] + (radius * Math.cos(a)) / R * 180 / Math.PI]);
|
|
33730
|
+
}
|
|
33731
|
+
return ring;
|
|
33732
|
+
}
|
|
33733
|
+
|
|
33734
|
+
// The editable rings of a geometry: [{ path: [polygon, ring] or [ring], coords (open), closed }].
|
|
33735
|
+
function ringsOf(g) {
|
|
33736
|
+
if (!g) { return []; }
|
|
33737
|
+
if (g.type === 'LineString') { return [{ path: [], coords: g.coordinates, closed: false }]; }
|
|
33738
|
+
if (g.type === 'Polygon') { return g.coordinates.map(function (r, i) { return { path: [i], coords: openRing(r), closed: true }; }); }
|
|
33739
|
+
if (g.type === 'MultiPolygon') {
|
|
33740
|
+
var out = [];
|
|
33741
|
+
g.coordinates.forEach(function (poly, p) { poly.forEach(function (r, i) { out.push({ path: [p, i], coords: openRing(r), closed: true }); }); });
|
|
33742
|
+
return out;
|
|
33743
|
+
}
|
|
33744
|
+
return [];
|
|
33745
|
+
}
|
|
33746
|
+
|
|
33747
|
+
function setRing(g, path, coords) {
|
|
33748
|
+
if (g.type === 'LineString') { g.coordinates = coords; return; }
|
|
33749
|
+
if (g.type === 'Polygon') { g.coordinates[path[0]] = closeRing(coords); return; }
|
|
33750
|
+
if (g.type === 'MultiPolygon') { g.coordinates[path[0]][path[1]] = closeRing(coords); }
|
|
33751
|
+
}
|
|
33752
|
+
|
|
33753
|
+
function moveGeometry(g, dx, dy) {
|
|
33754
|
+
(function walk(c) { if (typeof c[0] === 'number') { c[0] += dx; c[1] += dy; } else { c.forEach(walk); } })(g.coordinates);
|
|
33755
|
+
}
|
|
33756
|
+
|
|
33757
|
+
/**
|
|
33758
|
+
* A drawing and editing tool on this map. Shapes are kept by the tool as GeoJSON features (each with an id) and drawn
|
|
33759
|
+
* in its own layers; your page reads them with getAll() or from the events.
|
|
33760
|
+
* @param {Object} [o]
|
|
33761
|
+
* features (GeoJSON to start from), modes (the shapes offered, default all: polygon, rectangle, circle, line, point),
|
|
33762
|
+
* snap (true: a new or moved vertex within 10 px of another shape's vertex takes its place; default true),
|
|
33763
|
+
* toolbar (false, or a corner; default 'top-left'), labels (the toolbar's texts), color (default '#2563eb'),
|
|
33764
|
+
* onChange({ type: 'created' | 'edited' | 'deleted', feature, features }) after every change
|
|
33765
|
+
*/
|
|
33766
|
+
SMap.prototype.drawTool = function (o) {
|
|
33767
|
+
return new DrawTool(this, o || {});
|
|
33768
|
+
};
|
|
33769
|
+
|
|
33770
|
+
function DrawTool(map, o) {
|
|
33771
|
+
this.map = map;
|
|
33772
|
+
this.o = o;
|
|
33773
|
+
this.id = 'smap-draw-' + (++drawSeq);
|
|
33774
|
+
this.color = o.color || '#2563eb';
|
|
33775
|
+
this.modes = (o.modes || DRAW_MODES).filter(function (k) { return DRAW_MODES.indexOf(k) >= 0; });
|
|
33776
|
+
this.snap = o.snap !== false;
|
|
33777
|
+
this.text = Object.assign({}, DRAW_TEXT, o.labels || {});
|
|
33778
|
+
this.features = [];
|
|
33779
|
+
this.seq = 0;
|
|
33780
|
+
this.selected = null; // feature id
|
|
33781
|
+
this.vertex = null; // { ring, index } of the selected vertex
|
|
33782
|
+
this.mode = null; // the shape being drawn
|
|
33783
|
+
this.sketch = null; // coordinates of the shape being drawn
|
|
33784
|
+
this.hover = null;
|
|
33785
|
+
this.undoStack = [];
|
|
33786
|
+
this.redoStack = [];
|
|
33787
|
+
this.handlers = {};
|
|
33788
|
+
var self = this;
|
|
33789
|
+
(o.features ? toFeatureCollection(o.features).features : []).forEach(function (f) { self.features.push(self._own(f)); });
|
|
33790
|
+
map.ready.then(function () { if (!self.destroyed) { self._mount(); } });
|
|
33791
|
+
}
|
|
33792
|
+
|
|
33793
|
+
DrawTool.prototype._own = function (f) {
|
|
33794
|
+
var copy = JSON.parse(JSON.stringify(f));
|
|
33795
|
+
if (copy.id === undefined || copy.id === null) { copy.id = 'f' + (++this.seq); }
|
|
33796
|
+
copy.properties = copy.properties || {};
|
|
33797
|
+
return copy;
|
|
33798
|
+
};
|
|
33799
|
+
|
|
33800
|
+
DrawTool.prototype.on = function (type, fn) { (this.handlers[type] = this.handlers[type] || []).push(fn); return this; };
|
|
33801
|
+
DrawTool.prototype.off = function (type, fn) { this.handlers[type] = (this.handlers[type] || []).filter(function (h) { return h !== fn; }); return this; };
|
|
33802
|
+
DrawTool.prototype._emit = function (type, feature) {
|
|
33803
|
+
var e = { type: type, feature: feature ? JSON.parse(JSON.stringify(feature)) : null, features: this.getAll() };
|
|
33804
|
+
(this.handlers[type] || []).slice().forEach(function (h) { h(e); });
|
|
33805
|
+
(this.handlers.change || []).slice().forEach(function (h) { h(e); });
|
|
33806
|
+
if (this.o.onChange) { this.o.onChange(e); }
|
|
33807
|
+
};
|
|
33808
|
+
|
|
33809
|
+
/** Every shape as a FeatureCollection (a copy). */
|
|
33810
|
+
DrawTool.prototype.getAll = function () { return JSON.parse(JSON.stringify({ type: 'FeatureCollection', features: this.features })); };
|
|
33811
|
+
DrawTool.prototype.get = function (id) { var f = this._find(id); return f ? JSON.parse(JSON.stringify(f)) : null; };
|
|
33812
|
+
/** Replaces every shape (not recorded for undo). */
|
|
33813
|
+
DrawTool.prototype.set = function (data) {
|
|
33814
|
+
var self = this;
|
|
33815
|
+
this.features = toFeatureCollection(data).features.map(function (f) { return self._own(f); });
|
|
33816
|
+
this.selected = null; this.vertex = null;
|
|
33817
|
+
this.undoStack = []; this.redoStack = [];
|
|
33818
|
+
this._render();
|
|
33819
|
+
return this;
|
|
33820
|
+
};
|
|
33821
|
+
DrawTool.prototype._find = function (id) { for (var i = 0; i < this.features.length; i++) { if (this.features[i].id === id) { return this.features[i]; } } return null; };
|
|
33822
|
+
DrawTool.prototype._record = function () {
|
|
33823
|
+
this.undoStack.push(JSON.stringify(this.features));
|
|
33824
|
+
if (this.undoStack.length > 100) { this.undoStack.shift(); }
|
|
33825
|
+
this.redoStack = [];
|
|
33826
|
+
};
|
|
33827
|
+
DrawTool.prototype.undo = function () {
|
|
33828
|
+
if (!this.undoStack.length) { return this; }
|
|
33829
|
+
this.redoStack.push(JSON.stringify(this.features));
|
|
33830
|
+
this.features = JSON.parse(this.undoStack.pop());
|
|
33831
|
+
if (this.selected && !this._find(this.selected)) { this.selected = null; }
|
|
33832
|
+
this.vertex = null;
|
|
33833
|
+
this._render();
|
|
33834
|
+
this._emit('edited', this.selected ? this._find(this.selected) : null);
|
|
33835
|
+
return this;
|
|
33836
|
+
};
|
|
33837
|
+
DrawTool.prototype.redo = function () {
|
|
33838
|
+
if (!this.redoStack.length) { return this; }
|
|
33839
|
+
this.undoStack.push(JSON.stringify(this.features));
|
|
33840
|
+
this.features = JSON.parse(this.redoStack.pop());
|
|
33841
|
+
if (this.selected && !this._find(this.selected)) { this.selected = null; }
|
|
33842
|
+
this.vertex = null;
|
|
33843
|
+
this._render();
|
|
33844
|
+
this._emit('edited', this.selected ? this._find(this.selected) : null);
|
|
33845
|
+
return this;
|
|
33846
|
+
};
|
|
33847
|
+
|
|
33848
|
+
/** Starts drawing a shape: 'polygon', 'rectangle', 'circle', 'line', 'point', or 'hole' (in the selected polygon). */
|
|
33849
|
+
DrawTool.prototype.start = function (mode) {
|
|
33850
|
+
if (mode === 'hole') {
|
|
33851
|
+
var f = this.selected && this._find(this.selected);
|
|
33852
|
+
if (!f || !f.geometry || f.geometry.type !== 'Polygon') { throw new Error('sMap: select a polygon to draw a hole in'); }
|
|
33853
|
+
} else if (DRAW_MODES.indexOf(mode) < 0) { throw new Error('sMap: unknown shape "' + mode + '"'); }
|
|
33854
|
+
this.mode = mode;
|
|
33855
|
+
this.sketch = [];
|
|
33856
|
+
this.vertex = null;
|
|
33857
|
+
if (mode !== 'hole') { this.selected = null; }
|
|
33858
|
+
this._render();
|
|
33859
|
+
this._renderToolbar();
|
|
33860
|
+
return this;
|
|
33861
|
+
};
|
|
33862
|
+
|
|
33863
|
+
/** Stops drawing without keeping the shape being drawn. */
|
|
33864
|
+
DrawTool.prototype.cancel = function () { this.mode = null; this.sketch = null; this._render(); this._renderToolbar(); return this; };
|
|
33865
|
+
|
|
33866
|
+
/** Selects a shape for editing (null to deselect). */
|
|
33867
|
+
DrawTool.prototype.select = function (id) { this.selected = id && this._find(id) ? id : null; this.vertex = null; this._render(); this._renderToolbar(); return this; };
|
|
33868
|
+
DrawTool.prototype.selection = function () { return this.selected; };
|
|
33869
|
+
|
|
33870
|
+
/** Deletes a shape (default: the selected one); with a vertex selected, deletes that vertex instead. */
|
|
33871
|
+
DrawTool.prototype.delete = function (id) {
|
|
33872
|
+
if (id === undefined && this.vertex && this.selected) { return this._deleteVertex(); }
|
|
33873
|
+
id = id === undefined ? this.selected : id;
|
|
33874
|
+
var f = this._find(id);
|
|
33875
|
+
if (!f) { return this; }
|
|
33876
|
+
this._record();
|
|
33877
|
+
this.features = this.features.filter(function (x) { return x !== f; });
|
|
33878
|
+
if (this.selected === id) { this.selected = null; this.vertex = null; }
|
|
33879
|
+
this._render();
|
|
33880
|
+
this._renderToolbar();
|
|
33881
|
+
this._emit('deleted', f);
|
|
33882
|
+
return this;
|
|
33883
|
+
};
|
|
33884
|
+
|
|
33885
|
+
DrawTool.prototype._deleteVertex = function () {
|
|
33886
|
+
var f = this._find(this.selected), v = this.vertex;
|
|
33887
|
+
var rings = ringsOf(f.geometry), r = rings[v.ring];
|
|
33888
|
+
if (!r || r.coords.length <= (r.closed ? 3 : 2)) { return this; }
|
|
33889
|
+
this._record();
|
|
33890
|
+
var coords = r.coords.slice();
|
|
33891
|
+
coords.splice(v.index, 1);
|
|
33892
|
+
setRing(f.geometry, r.path, coords);
|
|
33893
|
+
this.vertex = null;
|
|
33894
|
+
this._render();
|
|
33895
|
+
this._emit('edited', f);
|
|
33896
|
+
return this;
|
|
33897
|
+
};
|
|
33898
|
+
|
|
33899
|
+
DrawTool.prototype.destroy = function () {
|
|
33900
|
+
this.destroyed = true;
|
|
33901
|
+
var m = this.map.engine;
|
|
33902
|
+
if (!m) { return; }
|
|
33903
|
+
var self = this;
|
|
33904
|
+
(this.listeners || []).forEach(function (l) { if (l[1]) { m.off(l[0], l[1], l[2]); } else { m.off(l[0], l[2]); } });
|
|
33905
|
+
[this.id + '-fill', this.id + '-line', this.id + '-sketch', this.id + '-vertex', this.id + '-mid'].forEach(function (l) { if (m.getLayer(l)) { m.removeLayer(l); } });
|
|
33906
|
+
if (m.getSource(this.id)) { m.removeSource(this.id); }
|
|
33907
|
+
if (this.toolbar) { m.removeControl(this.toolbar); }
|
|
33908
|
+
if (this.keyListener) { m.getContainer().removeEventListener('keydown', this.keyListener); }
|
|
33909
|
+
if (this.panOff && m.dragPan) { m.dragPan.enable(); }
|
|
33910
|
+
if (m.doubleClickZoom && this.dblWasOn) { m.doubleClickZoom.enable(); }
|
|
33911
|
+
self.map._drawTools = (self.map._drawTools || []).filter(function (t) { return t !== self; });
|
|
33912
|
+
};
|
|
33913
|
+
|
|
33914
|
+
DrawTool.prototype._listen = function (type, layer, fn) {
|
|
33915
|
+
var m = this.map.engine;
|
|
33916
|
+
if (layer) { m.on(type, layer, fn); } else { m.on(type, fn); }
|
|
33917
|
+
(this.listeners = this.listeners || []).push([type, layer, fn]);
|
|
33918
|
+
};
|
|
33919
|
+
|
|
33920
|
+
DrawTool.prototype._mount = function () {
|
|
33921
|
+
var self = this, m = this.map.engine;
|
|
33922
|
+
this.map._drawTools = (this.map._drawTools || []).concat([this]);
|
|
33923
|
+
this._addLayers();
|
|
33924
|
+
// The map's style changes drop every layer: the tool's come back with the map's own.
|
|
33925
|
+
this._listen('style.load', null, function () { self._addLayers(); });
|
|
33926
|
+
this._listen('mousedown', null, function (e) { self._down(e); });
|
|
33927
|
+
this._listen('touchstart', null, function (e) { if (e.points && e.points.length === 1) { self._down(e); } });
|
|
33928
|
+
this._listen('mousemove', null, function (e) { self._move(e); });
|
|
33929
|
+
this._listen('touchmove', null, function (e) { self._move(e); });
|
|
33930
|
+
this._listen('mouseup', null, function (e) { self._up(e); });
|
|
33931
|
+
this._listen('touchend', null, function (e) { self._up(e); });
|
|
33932
|
+
this._listen('click', null, function (e) { self._click(e); });
|
|
33933
|
+
this._listen('dblclick', null, function (e) { if (self.mode === 'polygon' || self.mode === 'line' || self.mode === 'hole') { e.preventDefault(); self._finish(); } });
|
|
33934
|
+
if (m.doubleClickZoom && m.doubleClickZoom.isEnabled && m.doubleClickZoom.isEnabled()) { this.dblWasOn = true; }
|
|
33935
|
+
var el = m.getContainer();
|
|
33936
|
+
this.keyListener = function (ev) {
|
|
33937
|
+
if (ev.key === 'Escape') { if (self.mode) { self.cancel(); } else { self.select(null); } }
|
|
33938
|
+
else if (ev.key === 'Enter' && self.mode) { self._finish(); }
|
|
33939
|
+
else if ((ev.key === 'Backspace' || ev.key === 'Delete') && self.mode && self.sketch && self.sketch.length) { self.sketch.pop(); self._render(); ev.preventDefault(); }
|
|
33940
|
+
else if ((ev.key === 'Backspace' || ev.key === 'Delete') && self.selected) { self.delete(); ev.preventDefault(); }
|
|
33941
|
+
else if ((ev.ctrlKey || ev.metaKey) && ev.key.toLowerCase() === 'z') { if (ev.shiftKey) { self.redo(); } else { self.undo(); } ev.preventDefault(); }
|
|
33942
|
+
};
|
|
33943
|
+
el.addEventListener('keydown', this.keyListener);
|
|
33944
|
+
if (this.o.toolbar !== false) {
|
|
33945
|
+
this.toolbar = this._toolbarControl();
|
|
33946
|
+
m.addControl(this.toolbar, typeof this.o.toolbar === 'string' ? this.o.toolbar : 'top-left');
|
|
33947
|
+
}
|
|
33948
|
+
this._render();
|
|
33949
|
+
};
|
|
33950
|
+
|
|
33951
|
+
DrawTool.prototype._addLayers = function () {
|
|
33952
|
+
var m = this.map.engine, id = this.id, c = this.color;
|
|
33953
|
+
if (m.getSource(id)) { return; }
|
|
33954
|
+
m.addSource(id, { type: 'geojson', data: { type: 'FeatureCollection', features: [] } });
|
|
33955
|
+
var role = function (r) { return ['==', ['get', '_role'], r]; };
|
|
33956
|
+
m.addLayer({ id: id + '-fill', type: 'fill', source: id, filter: ['all', ['==', ['geometry-type'], 'Polygon'], ['!', role('sketch')]],
|
|
33957
|
+
paint: { 'fill-color': ['coalesce', ['get', '_fill'], c], 'fill-opacity': ['case', ['==', ['get', '_selected'], true], 0.3, 0.18] } });
|
|
33958
|
+
m.addLayer({ id: id + '-line', type: 'line', source: id, filter: ['all', ['!=', ['geometry-type'], 'Point'], ['!', role('sketch')]],
|
|
33959
|
+
layout: { 'line-join': 'round', 'line-cap': 'round' },
|
|
33960
|
+
paint: { 'line-color': ['coalesce', ['get', '_stroke'], c], 'line-width': ['case', ['==', ['get', '_selected'], true], 3, 2] } });
|
|
33961
|
+
m.addLayer({ id: id + '-sketch', type: 'line', source: id, filter: role('sketch'),
|
|
33962
|
+
layout: { 'line-join': 'round', 'line-cap': 'round' }, paint: { 'line-color': c, 'line-width': 2, 'line-dasharray': [2, 1.5] } });
|
|
33963
|
+
m.addLayer({ id: id + '-mid', type: 'circle', source: id, filter: role('mid'),
|
|
33964
|
+
paint: { 'circle-radius': 4, 'circle-color': '#ffffff', 'circle-opacity': 0.9, 'circle-stroke-width': 1.5, 'circle-stroke-color': c } });
|
|
33965
|
+
m.addLayer({ id: id + '-vertex', type: 'circle', source: id, filter: ['any', role('vertex'), role('point'), role('sketch-vertex')],
|
|
33966
|
+
paint: { 'circle-radius': ['case', ['==', ['get', '_active'], true], 7, ['==', ['get', '_role'], 'point'], 7, 5],
|
|
33967
|
+
'circle-color': ['case', ['==', ['get', '_active'], true], c, '#ffffff'], 'circle-stroke-width': 2, 'circle-stroke-color': c } });
|
|
33968
|
+
};
|
|
33969
|
+
|
|
33970
|
+
// What is drawn: every shape, the selected one's vertices and midpoints, and the shape being drawn.
|
|
33971
|
+
DrawTool.prototype._render = function () {
|
|
33972
|
+
var m = this.map.engine;
|
|
33973
|
+
if (!m || this.destroyed) { return; }
|
|
33974
|
+
var out = [], self = this;
|
|
33975
|
+
this.features.forEach(function (f) {
|
|
33976
|
+
var sel = f.id === self.selected;
|
|
33977
|
+
var props = { _id: f.id, _selected: sel };
|
|
33978
|
+
if (f.properties && f.properties.fill) { props._fill = f.properties.fill; }
|
|
33979
|
+
if (f.properties && f.properties.stroke) { props._stroke = f.properties.stroke; }
|
|
33980
|
+
if (f.geometry.type === 'Point') { props._role = 'point'; props._active = sel; }
|
|
33981
|
+
out.push({ type: 'Feature', properties: props, geometry: f.geometry });
|
|
33982
|
+
if (!sel || f.geometry.type === 'Point') { return; }
|
|
33983
|
+
if (f.properties && f.properties.shape === 'circle' && f.properties.center) {
|
|
33984
|
+
// A circle: one handle on its edge (east) sets the radius.
|
|
33985
|
+
out.push({ type: 'Feature', properties: { _role: 'vertex', _id: f.id, _radius: true, _active: false }, geometry: { type: 'Point', coordinates: circleRing(f.properties.center, f.properties.radius)[16] } });
|
|
33986
|
+
return;
|
|
33987
|
+
}
|
|
33988
|
+
ringsOf(f.geometry).forEach(function (r, ri) {
|
|
33989
|
+
r.coords.forEach(function (c, i) {
|
|
33990
|
+
var active = !!self.vertex && self.vertex.ring === ri && self.vertex.index === i;
|
|
33991
|
+
out.push({ type: 'Feature', properties: { _role: 'vertex', _id: f.id, _ring: ri, _index: i, _active: active }, geometry: { type: 'Point', coordinates: c } });
|
|
33992
|
+
var next = r.coords[i + 1] || (r.closed ? r.coords[0] : null);
|
|
33993
|
+
if (next && !(f.properties && f.properties.shape === 'rectangle')) {
|
|
33994
|
+
out.push({ type: 'Feature', properties: { _role: 'mid', _id: f.id, _ring: ri, _index: i + 1 }, geometry: { type: 'Point', coordinates: [(c[0] + next[0]) / 2, (c[1] + next[1]) / 2] } });
|
|
33995
|
+
}
|
|
33996
|
+
});
|
|
33997
|
+
});
|
|
33998
|
+
});
|
|
33999
|
+
if (this.mode && this.sketch) {
|
|
34000
|
+
var pts = this.sketch.concat(this.hover ? [this.hover] : []);
|
|
34001
|
+
if (pts.length >= 2) {
|
|
34002
|
+
var closed = this.mode !== 'line' && pts.length >= 3;
|
|
34003
|
+
out.push({ type: 'Feature', properties: { _role: 'sketch' }, geometry: { type: 'LineString', coordinates: closed ? pts.concat([pts[0]]) : pts } });
|
|
34004
|
+
}
|
|
34005
|
+
this.sketch.forEach(function (c) { out.push({ type: 'Feature', properties: { _role: 'sketch-vertex' }, geometry: { type: 'Point', coordinates: c } }); });
|
|
34006
|
+
}
|
|
34007
|
+
var src = m.getSource(this.id);
|
|
34008
|
+
if (src) { src.setData({ type: 'FeatureCollection', features: out }); }
|
|
34009
|
+
};
|
|
34010
|
+
|
|
34011
|
+
DrawTool.prototype._lngLat = function (e) { return [e.lngLat.lng, e.lngLat.lat]; };
|
|
34012
|
+
|
|
34013
|
+
// A position taken over by a vertex of another shape within SNAP_PX pixels (when snapping is on).
|
|
34014
|
+
DrawTool.prototype._snapped = function (e, except) {
|
|
34015
|
+
if (!this.snap) { return this._lngLat(e); }
|
|
34016
|
+
var m = this.map.engine, best = null, bestD = SNAP_PX;
|
|
34017
|
+
this.features.forEach(function (f) {
|
|
34018
|
+
if (f.id === except) { return; }
|
|
34019
|
+
var pts = f.geometry.type === 'Point' ? [f.geometry.coordinates] : [];
|
|
34020
|
+
ringsOf(f.geometry).forEach(function (r) { pts = pts.concat(r.coords); });
|
|
34021
|
+
pts.forEach(function (c) {
|
|
34022
|
+
var p = m.project(c), d = Math.sqrt(Math.pow(p.x - e.point.x, 2) + Math.pow(p.y - e.point.y, 2));
|
|
34023
|
+
if (d <= bestD) { bestD = d; best = c; }
|
|
34024
|
+
});
|
|
34025
|
+
});
|
|
34026
|
+
return best ? best.slice() : this._lngLat(e);
|
|
34027
|
+
};
|
|
34028
|
+
|
|
34029
|
+
DrawTool.prototype._hit = function (e, layers) {
|
|
34030
|
+
var m = this.map.engine, self = this;
|
|
34031
|
+
var ids = layers.map(function (l) { return self.id + '-' + l; }).filter(function (l) { return m.getLayer(l); });
|
|
34032
|
+
if (!ids.length) { return null; }
|
|
34033
|
+
var r = 4, box = [[e.point.x - r, e.point.y - r], [e.point.x + r, e.point.y + r]];
|
|
34034
|
+
var hits = m.queryRenderedFeatures(box, { layers: ids }) || [];
|
|
34035
|
+
return hits[0] || null;
|
|
34036
|
+
};
|
|
34037
|
+
|
|
34038
|
+
DrawTool.prototype._pan = function (on) {
|
|
34039
|
+
var m = this.map.engine;
|
|
34040
|
+
if (!m.dragPan) { return; }
|
|
34041
|
+
if (on && this.panOff) { m.dragPan.enable(); this.panOff = false; }
|
|
34042
|
+
else if (!on && !this.panOff && (!m.dragPan.isEnabled || m.dragPan.isEnabled())) { m.dragPan.disable(); this.panOff = true; }
|
|
34043
|
+
};
|
|
34044
|
+
|
|
34045
|
+
DrawTool.prototype._down = function (e) {
|
|
34046
|
+
if (e.originalEvent && e.originalEvent.button !== undefined && e.originalEvent.button !== 0) { return; }
|
|
34047
|
+
var self = this;
|
|
34048
|
+
if (this.mode === 'rectangle' || this.mode === 'circle') {
|
|
34049
|
+
e.preventDefault();
|
|
34050
|
+
this._pan(false);
|
|
34051
|
+
this.drag = { kind: this.mode, start: this._snapped(e) };
|
|
34052
|
+
return;
|
|
34053
|
+
}
|
|
34054
|
+
if (this.mode) { return; }
|
|
34055
|
+
if (!this.selected) { return; }
|
|
34056
|
+
var f = this._find(this.selected);
|
|
34057
|
+
var hit = this._hit(e, ['vertex', 'mid']);
|
|
34058
|
+
if (hit && hit.properties._id === this.selected) {
|
|
34059
|
+
e.preventDefault();
|
|
34060
|
+
this._pan(false);
|
|
34061
|
+
var p = hit.properties;
|
|
34062
|
+
if (p._radius) {
|
|
34063
|
+
this._record();
|
|
34064
|
+
this.drag = { kind: 'radius', moved: false };
|
|
34065
|
+
return;
|
|
34066
|
+
}
|
|
34067
|
+
if (p._role === 'mid') {
|
|
34068
|
+
// A midpoint dragged becomes a new vertex.
|
|
34069
|
+
this._record();
|
|
34070
|
+
var rings = ringsOf(f.geometry), r = rings[p._ring], coords = r.coords.slice();
|
|
34071
|
+
coords.splice(p._index, 0, hit.geometry.coordinates.slice());
|
|
34072
|
+
setRing(f.geometry, r.path, coords);
|
|
34073
|
+
this.vertex = { ring: p._ring, index: p._index };
|
|
34074
|
+
} else {
|
|
34075
|
+
this._record();
|
|
34076
|
+
this.vertex = { ring: p._ring, index: p._index };
|
|
34077
|
+
}
|
|
34078
|
+
this.drag = { kind: 'vertex', moved: false };
|
|
34079
|
+
this._render();
|
|
34080
|
+
return;
|
|
34081
|
+
}
|
|
34082
|
+
var body = this._hit(e, ['fill', 'line', 'vertex']);
|
|
34083
|
+
if (body && body.properties._id === this.selected) {
|
|
34084
|
+
e.preventDefault();
|
|
34085
|
+
this._pan(false);
|
|
34086
|
+
this._record();
|
|
34087
|
+
this.drag = { kind: 'move', last: this._lngLat(e), moved: false };
|
|
34088
|
+
self.vertex = null;
|
|
34089
|
+
}
|
|
34090
|
+
};
|
|
34091
|
+
|
|
34092
|
+
DrawTool.prototype._move = function (e) {
|
|
34093
|
+
if (this.mode && this.sketch && !this.drag) { this.hover = this._snapped(e); this._render(); }
|
|
34094
|
+
var d = this.drag;
|
|
34095
|
+
if (!d) { return; }
|
|
34096
|
+
var f = this.selected && this._find(this.selected);
|
|
34097
|
+
if (d.kind === 'rectangle' || d.kind === 'circle') {
|
|
34098
|
+
d.end = this._lngLat(e);
|
|
34099
|
+
var shape = this._dragShape(d);
|
|
34100
|
+
this.sketch = shape ? openRing(shape) : [];
|
|
34101
|
+
this.hover = null;
|
|
34102
|
+
this._render();
|
|
34103
|
+
return;
|
|
34104
|
+
}
|
|
34105
|
+
if (!f) { return; }
|
|
34106
|
+
d.moved = true;
|
|
34107
|
+
if (d.kind === 'radius') {
|
|
34108
|
+
f.properties.radius = interop.geo.distance(f.properties.center, this._lngLat(e));
|
|
34109
|
+
f.geometry.coordinates = [circleRing(f.properties.center, f.properties.radius)];
|
|
34110
|
+
} else if (d.kind === 'vertex' && f.properties && f.properties.shape === 'rectangle') {
|
|
34111
|
+
// A rectangle stays a rectangle: the opposite corner stays, the dragged one follows the pointer.
|
|
34112
|
+
var cs = ringsOf(f.geometry)[0].coords, opp = cs[(this.vertex.index + 2) % 4], at = this._snapped(e, f.id);
|
|
34113
|
+
var rect = [[opp[0], opp[1]], [at[0], opp[1]], [at[0], at[1]], [opp[0], at[1]]];
|
|
34114
|
+
// Keep the dragged corner at its index.
|
|
34115
|
+
var shift = (this.vertex.index + 2) % 4, ordered = [];
|
|
34116
|
+
for (var q = 0; q < 4; q++) { ordered[(shift + q) % 4] = rect[q]; }
|
|
34117
|
+
f.geometry.coordinates = [closeRing(ordered)];
|
|
34118
|
+
} else if (d.kind === 'vertex') {
|
|
34119
|
+
var rings = ringsOf(f.geometry), r = rings[this.vertex.ring], coords = r.coords.slice();
|
|
34120
|
+
coords[this.vertex.index] = this._snapped(e, f.id);
|
|
34121
|
+
setRing(f.geometry, r.path, coords);
|
|
34122
|
+
} else if (d.kind === 'move') {
|
|
34123
|
+
var now = this._lngLat(e);
|
|
34124
|
+
if (f.properties && f.properties.shape === 'circle' && f.properties.center) {
|
|
34125
|
+
f.properties.center = [f.properties.center[0] + now[0] - d.last[0], f.properties.center[1] + now[1] - d.last[1]];
|
|
34126
|
+
}
|
|
34127
|
+
moveGeometry(f.geometry, now[0] - d.last[0], now[1] - d.last[1]);
|
|
34128
|
+
d.last = now;
|
|
34129
|
+
}
|
|
34130
|
+
this._render();
|
|
34131
|
+
};
|
|
34132
|
+
|
|
34133
|
+
DrawTool.prototype._dragShape = function (d) {
|
|
34134
|
+
if (!d.end) { return null; }
|
|
34135
|
+
if (d.kind === 'rectangle') {
|
|
34136
|
+
var a = d.start, b = d.end;
|
|
34137
|
+
return [[a[0], a[1]], [b[0], a[1]], [b[0], b[1]], [a[0], b[1]], [a[0], a[1]]];
|
|
34138
|
+
}
|
|
34139
|
+
var radius = interop.geo.distance(d.start, d.end);
|
|
34140
|
+
return radius > 0 ? circleRing(d.start, radius) : null;
|
|
34141
|
+
};
|
|
34142
|
+
|
|
34143
|
+
DrawTool.prototype._up = function (e) {
|
|
34144
|
+
var d = this.drag;
|
|
34145
|
+
if (!d) { return; }
|
|
34146
|
+
this.drag = null;
|
|
34147
|
+
this._pan(true);
|
|
34148
|
+
this._suppressClick = true;
|
|
34149
|
+
var self = this;
|
|
34150
|
+
setTimeout(function () { self._suppressClick = false; }, 0);
|
|
34151
|
+
if (d.kind === 'rectangle' || d.kind === 'circle') {
|
|
34152
|
+
d.end = this._lngLat(e);
|
|
34153
|
+
var ring = this._dragShape(d);
|
|
34154
|
+
var mode = this.mode;
|
|
34155
|
+
this.mode = null; this.sketch = null; this.hover = null;
|
|
34156
|
+
if (ring && Math.abs(ring[0][0] - ring[2][0]) + Math.abs(ring[0][1] - ring[2][1]) > 0) {
|
|
34157
|
+
var props = mode === 'circle' ? { shape: 'circle', center: d.start, radius: interop.geo.distance(d.start, d.end) } : { shape: 'rectangle' };
|
|
34158
|
+
this._add({ type: 'Feature', properties: props, geometry: { type: 'Polygon', coordinates: [ring] } });
|
|
34159
|
+
} else { this._render(); this._renderToolbar(); }
|
|
34160
|
+
return;
|
|
34161
|
+
}
|
|
34162
|
+
if (d.moved) { this._emit('edited', this._find(this.selected)); } else { this.undoStack.pop(); }
|
|
34163
|
+
this._render();
|
|
34164
|
+
};
|
|
34165
|
+
|
|
34166
|
+
DrawTool.prototype._click = function (e) {
|
|
34167
|
+
if (this._suppressClick) { return; }
|
|
34168
|
+
var self = this;
|
|
34169
|
+
if (this.mode === 'point') { this.mode = null; this.sketch = null; this._add({ type: 'Feature', properties: {}, geometry: { type: 'Point', coordinates: this._snapped(e) } }); return; }
|
|
34170
|
+
if (this.mode === 'polygon' || this.mode === 'line' || this.mode === 'hole') {
|
|
34171
|
+
var at = this._snapped(e);
|
|
34172
|
+
// A click on the first vertex closes a polygon.
|
|
34173
|
+
if (this.mode !== 'line' && this.sketch.length >= 3) {
|
|
34174
|
+
var first = this.map.engine.project(this.sketch[0]);
|
|
34175
|
+
if (Math.abs(first.x - e.point.x) <= SNAP_PX && Math.abs(first.y - e.point.y) <= SNAP_PX) { this._finish(); return; }
|
|
34176
|
+
}
|
|
34177
|
+
var last = this.sketch[this.sketch.length - 1];
|
|
34178
|
+
if (!last || last[0] !== at[0] || last[1] !== at[1]) { this.sketch.push(at); }
|
|
34179
|
+
this._render();
|
|
34180
|
+
return;
|
|
34181
|
+
}
|
|
34182
|
+
if (this.mode) { return; }
|
|
34183
|
+
// Not drawing: a click selects the shape under it (or a vertex of the selected one), elsewhere deselects.
|
|
34184
|
+
var v = this.selected && this._hit(e, ['vertex']);
|
|
34185
|
+
if (v && v.properties._id === this.selected && v.properties._role === 'vertex') {
|
|
34186
|
+
this.vertex = { ring: v.properties._ring, index: v.properties._index };
|
|
34187
|
+
this._render();
|
|
34188
|
+
return;
|
|
34189
|
+
}
|
|
34190
|
+
var hit = this._hit(e, ['vertex', 'line', 'fill']);
|
|
34191
|
+
self.select(hit ? hit.properties._id : null);
|
|
34192
|
+
};
|
|
34193
|
+
|
|
34194
|
+
DrawTool.prototype._finish = function () {
|
|
34195
|
+
var mode = this.mode, pts = (this.sketch || []).slice();
|
|
34196
|
+
this.mode = null; this.sketch = null; this.hover = null;
|
|
34197
|
+
if (mode === 'line' && pts.length >= 2) { this._add({ type: 'Feature', properties: {}, geometry: { type: 'LineString', coordinates: pts } }); return; }
|
|
34198
|
+
if (mode === 'polygon' && pts.length >= 3) { this._add({ type: 'Feature', properties: {}, geometry: { type: 'Polygon', coordinates: [closeRing(pts)] } }); return; }
|
|
34199
|
+
if (mode === 'hole' && pts.length >= 3) {
|
|
34200
|
+
var f = this._find(this.selected);
|
|
34201
|
+
if (f && f.geometry.type === 'Polygon') {
|
|
34202
|
+
this._record();
|
|
34203
|
+
f.geometry.coordinates.push(closeRing(pts));
|
|
34204
|
+
this._render(); this._renderToolbar();
|
|
34205
|
+
this._emit('edited', f);
|
|
34206
|
+
return;
|
|
34207
|
+
}
|
|
34208
|
+
}
|
|
34209
|
+
this._render();
|
|
34210
|
+
this._renderToolbar();
|
|
34211
|
+
};
|
|
34212
|
+
|
|
34213
|
+
DrawTool.prototype._add = function (feature) {
|
|
34214
|
+
this._record();
|
|
34215
|
+
var f = this._own(feature);
|
|
34216
|
+
this.features.push(f);
|
|
34217
|
+
this.selected = f.geometry.type === 'Point' ? null : f.id;
|
|
34218
|
+
this.vertex = null;
|
|
34219
|
+
this._render();
|
|
34220
|
+
this._renderToolbar();
|
|
34221
|
+
this._emit('created', f);
|
|
34222
|
+
};
|
|
34223
|
+
|
|
34224
|
+
DrawTool.prototype._toolbarControl = function () {
|
|
34225
|
+
var self = this;
|
|
34226
|
+
return {
|
|
34227
|
+
onAdd: function (m) {
|
|
34228
|
+
var doc = m.getContainer().ownerDocument;
|
|
34229
|
+
var el = doc.createElement('div');
|
|
34230
|
+
el.className = 'maplibregl-ctrl maplibregl-ctrl-group smap-draw';
|
|
34231
|
+
self.toolbarEl = el;
|
|
34232
|
+
self._renderToolbar();
|
|
34233
|
+
return el;
|
|
34234
|
+
},
|
|
34235
|
+
onRemove: function () { if (self.toolbarEl && self.toolbarEl.parentNode) { self.toolbarEl.parentNode.removeChild(self.toolbarEl); } self.toolbarEl = null; },
|
|
34236
|
+
};
|
|
34237
|
+
};
|
|
34238
|
+
|
|
34239
|
+
var DRAW_ICONS = {
|
|
34240
|
+
polygon: '<path d="M5 7l7-4 7 5-2 10H7z"/>', rectangle: '<rect x="4" y="6" width="16" height="12"/>',
|
|
34241
|
+
circle: '<circle cx="12" cy="12" r="8"/>', line: '<path d="M4 18 10 8l4 6 6-10"/>', point: '<circle cx="12" cy="12" r="3"/><path d="M12 3v3M12 18v3M3 12h3M18 12h3"/>',
|
|
34242
|
+
hole: '<path d="M5 7l7-4 7 5-2 10H7z"/><circle cx="12" cy="11" r="2.5"/>', delete: '<path d="M5 7h14M9 7V4h6v3M7 7l1 13h8l1-13"/>',
|
|
34243
|
+
undo: '<path d="M9 14 4 9l5-5"/><path d="M4 9h10a6 6 0 0 1 0 12h-3"/>', redo: '<path d="m15 14 5-5-5-5"/><path d="M20 9H10a6 6 0 0 0 0 12h3"/>',
|
|
34244
|
+
};
|
|
34245
|
+
|
|
34246
|
+
DrawTool.prototype._renderToolbar = function () {
|
|
34247
|
+
var el = this.toolbarEl;
|
|
34248
|
+
if (!el) { return; }
|
|
34249
|
+
var self = this, doc = el.ownerDocument;
|
|
34250
|
+
while (el.firstChild) { el.removeChild(el.firstChild); }
|
|
34251
|
+
var selectedPolygon = this.selected && this._find(this.selected) && this._find(this.selected).geometry.type === 'Polygon' && !(this._find(this.selected).properties.shape);
|
|
34252
|
+
var buttons = this.modes.slice();
|
|
34253
|
+
if (selectedPolygon) { buttons.push('hole'); }
|
|
34254
|
+
buttons = buttons.concat(['delete', 'undo', 'redo']);
|
|
34255
|
+
buttons.forEach(function (k) {
|
|
34256
|
+
var b = doc.createElement('button');
|
|
34257
|
+
b.type = 'button';
|
|
34258
|
+
b.className = 'smap-draw-' + k;
|
|
34259
|
+
b.title = self.text[k];
|
|
34260
|
+
b.setAttribute('aria-label', self.text[k]);
|
|
34261
|
+
b.innerHTML = '<svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true" focusable="false" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' + DRAW_ICONS[k] + '</svg>';
|
|
34262
|
+
if (DRAW_MODES.indexOf(k) >= 0 || k === 'hole') {
|
|
34263
|
+
b.setAttribute('aria-pressed', self.mode === k ? 'true' : 'false');
|
|
34264
|
+
b.addEventListener('click', function () { if (self.mode === k) { self.cancel(); } else { self.start(k); } });
|
|
34265
|
+
} else if (k === 'delete') {
|
|
34266
|
+
b.disabled = !self.selected;
|
|
34267
|
+
b.addEventListener('click', function () { self.delete(); });
|
|
34268
|
+
} else {
|
|
34269
|
+
b.disabled = k === 'undo' ? !self.undoStack.length : !self.redoStack.length;
|
|
34270
|
+
b.addEventListener('click', function () { self[k](); });
|
|
34271
|
+
}
|
|
34272
|
+
el.appendChild(b);
|
|
34273
|
+
});
|
|
34274
|
+
};
|
|
34275
|
+
|
|
34276
|
+
/**
|
|
34277
|
+
* Selects features by drawing on the map: 'box' (press and drag) or 'lasso' (press and draw a free shape). Resolves
|
|
34278
|
+
* with { features: { layerId: [features] }, geometry } once the shape is drawn, or null when Escape cancels it.
|
|
34279
|
+
* A point (or custom marker) counts when it is inside; a line or area when any of its vertices is.
|
|
34280
|
+
* @param {Object} [o] mode ('box' default, 'lasso'), layers (default every point, custom marker, line and area layer)
|
|
34281
|
+
*/
|
|
34282
|
+
SMap.prototype.selectArea = function (o) {
|
|
34283
|
+
o = o || {};
|
|
34284
|
+
var self = this;
|
|
34285
|
+
var mode = o.mode === 'lasso' ? 'lasso' : 'box';
|
|
34286
|
+
return this.ready.then(function () {
|
|
34287
|
+
return new Promise(function (resolve) {
|
|
34288
|
+
var m = self.engine;
|
|
34289
|
+
var overlay = self._markerOverlay(), doc = overlay.ownerDocument;
|
|
34290
|
+
var svgNs = 'http://www.w3.org/2000/svg';
|
|
34291
|
+
var svg = doc.createElementNS(svgNs, 'svg');
|
|
34292
|
+
svg.setAttribute('class', 'smap-select');
|
|
34293
|
+
var shape = doc.createElementNS(svgNs, mode === 'box' ? 'rect' : 'polyline');
|
|
34294
|
+
svg.appendChild(shape);
|
|
34295
|
+
var points = null, panWasOn = !!(m.dragPan && (!m.dragPan.isEnabled || m.dragPan.isEnabled()));
|
|
34296
|
+
if (panWasOn) { m.dragPan.disable(); }
|
|
34297
|
+
if (m.boxZoom && m.boxZoom.disable) { m.boxZoom.disable(); }
|
|
34298
|
+
m.getCanvas().style.cursor = 'crosshair';
|
|
34299
|
+
var finish = function (result) {
|
|
34300
|
+
m.off('mousedown', down); m.off('mousemove', move); m.off('mouseup', up);
|
|
34301
|
+
m.off('touchstart', down); m.off('touchmove', move); m.off('touchend', up);
|
|
34302
|
+
m.getContainer().removeEventListener('keydown', key);
|
|
34303
|
+
if (svg.parentNode) { svg.parentNode.removeChild(svg); }
|
|
34304
|
+
if (panWasOn) { m.dragPan.enable(); }
|
|
34305
|
+
if (m.boxZoom && m.boxZoom.enable) { m.boxZoom.enable(); }
|
|
34306
|
+
m.getCanvas().style.cursor = '';
|
|
34307
|
+
resolve(result);
|
|
34308
|
+
};
|
|
34309
|
+
var draw = function () {
|
|
34310
|
+
if (mode === 'box') {
|
|
34311
|
+
var a = points[0], b = points[points.length - 1];
|
|
34312
|
+
shape.setAttribute('x', Math.min(a.x, b.x)); shape.setAttribute('y', Math.min(a.y, b.y));
|
|
34313
|
+
shape.setAttribute('width', Math.abs(a.x - b.x)); shape.setAttribute('height', Math.abs(a.y - b.y));
|
|
34314
|
+
} else {
|
|
34315
|
+
shape.setAttribute('points', points.map(function (p) { return p.x + ',' + p.y; }).join(' '));
|
|
34316
|
+
}
|
|
34317
|
+
};
|
|
34318
|
+
var down = function (e) { e.preventDefault(); points = [e.point]; overlay.appendChild(svg); draw(); };
|
|
34319
|
+
var move = function (e) { if (!points) { return; } if (mode === 'box') { points = [points[0], e.point]; } else { points.push(e.point); } draw(); };
|
|
34320
|
+
var up = function (e) {
|
|
34321
|
+
if (!points) { return; }
|
|
34322
|
+
if (mode === 'box') { points = [points[0], e.point]; }
|
|
34323
|
+
var ring;
|
|
34324
|
+
if (mode === 'box') {
|
|
34325
|
+
var a = points[0], b = points[1];
|
|
34326
|
+
ring = [[a.x, a.y], [b.x, a.y], [b.x, b.y], [a.x, b.y]];
|
|
34327
|
+
} else {
|
|
34328
|
+
ring = points.map(function (p) { return [p.x, p.y]; });
|
|
34329
|
+
}
|
|
34330
|
+
var geoRing = ring.map(function (p) { var ll = m.unproject(p); return [ll.lng, ll.lat]; });
|
|
34331
|
+
var geometry = { type: 'Polygon', coordinates: [closeRing(geoRing)] };
|
|
34332
|
+
finish({ features: self._featuresIn(ring, o.layers), geometry: geometry });
|
|
34333
|
+
};
|
|
34334
|
+
var key = function (ev) { if (ev.key === 'Escape') { finish(null); } };
|
|
34335
|
+
m.on('mousedown', down); m.on('mousemove', move); m.on('mouseup', up);
|
|
34336
|
+
m.on('touchstart', down); m.on('touchmove', move); m.on('touchend', up);
|
|
34337
|
+
m.getContainer().addEventListener('keydown', key);
|
|
34338
|
+
});
|
|
34339
|
+
});
|
|
34340
|
+
};
|
|
34341
|
+
|
|
34342
|
+
// The features of the map's layers with a vertex inside a ring of screen points (projected now).
|
|
34343
|
+
SMap.prototype._featuresIn = function (ring, layers) {
|
|
34344
|
+
var self = this, m = this.engine, out = {};
|
|
34345
|
+
var xs = ring.map(function (p) { return p[0]; }), ys = ring.map(function (p) { return p[1]; });
|
|
34346
|
+
var minX = Math.min.apply(null, xs), maxX = Math.max.apply(null, xs), minY = Math.min.apply(null, ys), maxY = Math.max.apply(null, ys);
|
|
34347
|
+
var inside = function (c) {
|
|
34348
|
+
var p = m.project(c);
|
|
34349
|
+
if (p.x < minX || p.x > maxX || p.y < minY || p.y > maxY) { return false; }
|
|
34350
|
+
return interop.geo.pointInPolygon([p.x, p.y], ring);
|
|
34351
|
+
};
|
|
34352
|
+
(layers || this._order).forEach(function (id) {
|
|
34353
|
+
var def = self._layers[id];
|
|
34354
|
+
if (!def || def.hidden || !def.data || ['points', 'custom', 'lines', 'polygons', 'heatmap'].indexOf(def.kind) < 0) { return; }
|
|
34355
|
+
var hits = def.data.features.filter(function (f) {
|
|
34356
|
+
var found = false;
|
|
34357
|
+
(function walk(c) { if (found || !c) { return; } if (typeof c[0] === 'number') { found = inside(c); } else { c.forEach(walk); } })(f.geometry && f.geometry.coordinates);
|
|
34358
|
+
return found;
|
|
34359
|
+
});
|
|
34360
|
+
if (hits.length) { out[id] = hits; }
|
|
34361
|
+
});
|
|
34362
|
+
return out;
|
|
34363
|
+
};
|
|
34364
|
+
|
|
34365
|
+
// ------------------------------------------------------------------ controls (G8): own controls, base imagery, layer switcher
|
|
34366
|
+
|
|
34367
|
+
/**
|
|
34368
|
+
* Puts an element or an engine control in a corner of the map without making it a tool (it is never hidden by the
|
|
34369
|
+
* tools control). An element returns where the page had it when removed.
|
|
34370
|
+
* @param {Element|Object} target
|
|
34371
|
+
* @param {string} [position] 'top-left', 'top-right' (default), 'bottom-left', 'bottom-right'
|
|
34372
|
+
*/
|
|
34373
|
+
SMap.prototype.addControl = function (target, position) {
|
|
34374
|
+
position = position || 'top-right';
|
|
34375
|
+
if (TOOL_CORNERS.indexOf(position) < 0) { throw new Error('sMap: unknown control position "' + position + '"'); }
|
|
34376
|
+
if (!isElement(target) && !(target && typeof target.onAdd === 'function')) { throw new Error('sMap: a control is an element or an engine control'); }
|
|
34377
|
+
this._controls = this._controls || [];
|
|
34378
|
+
var holder = { position: position, el: null };
|
|
34379
|
+
var control = isElement(target) ? elementControl(holder, target) : target;
|
|
34380
|
+
this._controls.push({ target: target, control: control, position: position });
|
|
34381
|
+
var self = this;
|
|
34382
|
+
if (this.engine) { this.engine.addControl(control, position); } else { this.ready.then(function () { self.engine.addControl(control, position); }); }
|
|
34383
|
+
return this;
|
|
34384
|
+
};
|
|
34385
|
+
|
|
34386
|
+
SMap.prototype.removeControl = function (target) {
|
|
34387
|
+
var list = this._controls || [];
|
|
34388
|
+
for (var i = 0; i < list.length; i++) {
|
|
34389
|
+
if (list[i].target !== target) { continue; }
|
|
34390
|
+
if (this.engine) { this.engine.removeControl(list[i].control); }
|
|
34391
|
+
list.splice(i, 1);
|
|
34392
|
+
break;
|
|
34393
|
+
}
|
|
34394
|
+
return this;
|
|
34395
|
+
};
|
|
34396
|
+
|
|
34397
|
+
var BASE_RASTER = 'smap-base-imagery';
|
|
34398
|
+
|
|
34399
|
+
/**
|
|
34400
|
+
* Imagery (satellite, aerial, a scanned map, …) in place of the street map: raster tiles drawn over the basemap's
|
|
34401
|
+
* land, water and roads, under its labels — which stay with `labels: true` (a hybrid map) and are hidden otherwise.
|
|
34402
|
+
* Your layers stay on top, in their slots. null goes back to the street map.
|
|
34403
|
+
* @param {Object|null} imagery { tiles: [url with {z}/{x}/{y}], tileSize (256), minzoom, maxzoom, attribution,
|
|
34404
|
+
* scheme ('xyz' | 'tms'), labels (default false) }
|
|
34405
|
+
*/
|
|
34406
|
+
SMap.prototype.setBaseImagery = function (imagery) {
|
|
34407
|
+
if (imagery && (!Array.isArray(imagery.tiles) || !imagery.tiles.length)) { throw new Error('sMap: base imagery needs tiles'); }
|
|
34408
|
+
// Google's and Bing's tiles only through their own terms: Google's with setGoogleBase (M31, M66).
|
|
34409
|
+
if (imagery) { interop.refuseForbidden(imagery.tiles); }
|
|
34410
|
+
return this._setBaseImagery(imagery);
|
|
34411
|
+
};
|
|
34412
|
+
|
|
34413
|
+
SMap.prototype._setBaseImagery = function (imagery) {
|
|
34414
|
+
this._baseImagery = imagery ? Object.assign({}, imagery) : null;
|
|
34415
|
+
if (this.engine && this._styleReady) { this._applyBaseImagery(); }
|
|
34416
|
+
return this;
|
|
34417
|
+
};
|
|
34418
|
+
|
|
34419
|
+
// Whether a style (a scheme, 'custom:<id>', a URL or an object) is the one shown now.
|
|
34420
|
+
SMap.prototype._isStyle = function (style) {
|
|
34421
|
+
if (typeof style === 'string' && FLAVORS.indexOf(style) >= 0) { return !this._customStyle && !this._serverStyle && this._flavor === style; }
|
|
34422
|
+
if (typeof style === 'string' && style.indexOf(CUSTOM_PREFIX) === 0) { return this._serverStyle === style; }
|
|
34423
|
+
return this._customStyle === style;
|
|
34424
|
+
};
|
|
34425
|
+
|
|
34426
|
+
SMap.prototype.baseImagery = function () { return this._baseImagery ? Object.assign({}, this._baseImagery) : null; };
|
|
34427
|
+
|
|
34428
|
+
SMap.prototype._applyBaseImagery = function () {
|
|
34429
|
+
var m = this.engine, b = this._baseImagery;
|
|
34430
|
+
if (m.getLayer(BASE_RASTER)) { m.removeLayer(BASE_RASTER); }
|
|
34431
|
+
if (m.getSource(BASE_RASTER)) { m.removeSource(BASE_RASTER); }
|
|
34432
|
+
var anchors = this.slots();
|
|
34433
|
+
var labelsAt = anchors.labels;
|
|
34434
|
+
// The basemap's labels: every basemap layer from where the labels slot begins.
|
|
34435
|
+
var ids = this._basemapIds.filter(function (x) { return m.getLayer(x); });
|
|
34436
|
+
var from = labelsAt ? ids.indexOf(labelsAt) : -1;
|
|
34437
|
+
var labelIds = from >= 0 ? ids.slice(from) : [];
|
|
34438
|
+
var hidden = this._imageryHid || [];
|
|
34439
|
+
hidden.forEach(function (x) { if (m.getLayer(x)) { m.setLayoutProperty(x, 'visibility', 'visible'); } });
|
|
34440
|
+
this._imageryHid = [];
|
|
34441
|
+
if (!b) { return; }
|
|
34442
|
+
var source = { type: 'raster', tiles: b.tiles.slice(), tileSize: b.tileSize || 256 };
|
|
34443
|
+
if (b.minzoom !== undefined) { source.minzoom = b.minzoom; }
|
|
34444
|
+
if (b.maxzoom !== undefined) { source.maxzoom = b.maxzoom; }
|
|
34445
|
+
if (b.attribution) { source.attribution = b.attribution; }
|
|
34446
|
+
if (b.scheme) { source.scheme = b.scheme; }
|
|
34447
|
+
m.addSource(BASE_RASTER, source);
|
|
34448
|
+
m.addLayer({ id: BASE_RASTER, type: 'raster', source: BASE_RASTER }, labelsAt || undefined);
|
|
34449
|
+
if (!b.labels) {
|
|
34450
|
+
var self = this;
|
|
34451
|
+
labelIds.forEach(function (x) {
|
|
34452
|
+
if (m.getLayoutProperty && m.getLayoutProperty(x, 'visibility') === 'none') { return; }
|
|
34453
|
+
m.setLayoutProperty(x, 'visibility', 'none');
|
|
34454
|
+
self._imageryHid.push(x);
|
|
34455
|
+
});
|
|
34456
|
+
}
|
|
34457
|
+
};
|
|
34458
|
+
|
|
34459
|
+
// ---- 3D models (G12): glTF models at points, drawn with three.js (handed over once with sMap.useThree)
|
|
34460
|
+
|
|
34461
|
+
|
|
34462
|
+
/**
|
|
34463
|
+
* glTF models at points (vehicles, buildings, equipment), drawn in the map's 3D view and turned to each feature's
|
|
34464
|
+
* heading. Needs three.js: call sMap.useThree(THREE, GLTFLoader) once first.
|
|
34465
|
+
* @param {Object} o model (a .glb / .gltf URL), size (metres per model unit, default 1; or per feature),
|
|
34466
|
+
* heading (degrees clockwise from north, default 0; or per feature, e.g. { property: 'bearing' }),
|
|
34467
|
+
* altitude (metres above the ground, default 0), rotate ([x, y, z] degrees turning the model upright and to
|
|
34468
|
+
* face north before the heading; default [90, 0, 180], right for a model whose up is +Y and front +Z),
|
|
34469
|
+
* idProperty, minzoom, slot, before, zIndex
|
|
34470
|
+
*/
|
|
34471
|
+
SMap.prototype.addModels = function (id, data, o) {
|
|
34472
|
+
if (!interop.getThree()) { throw new Error('sMap: call sMap.useThree(THREE, GLTFLoader) before addModels'); }
|
|
34473
|
+
if (!o || !o.model) { throw new Error('sMap: addModels needs a model URL'); }
|
|
34474
|
+
this._define(id, { kind: 'models', data: toFeatureCollection(data), o: o });
|
|
34475
|
+
return this;
|
|
34476
|
+
};
|
|
34477
|
+
|
|
34478
|
+
SMap.prototype._modelLayer = function (id, def) {
|
|
34479
|
+
var self = this, three = interop.getThree(), T = three.THREE, o = def.o;
|
|
34480
|
+
var deg = Math.PI / 180;
|
|
34481
|
+
var base = o.rotate || [90, 0, 180];
|
|
34482
|
+
var baseMatrix = new T.Matrix4().makeRotationFromEuler(new T.Euler(base[0] * deg, base[1] * deg, base[2] * deg, 'ZYX'));
|
|
34483
|
+
var layer = {
|
|
34484
|
+
id: 'smap-' + id + '-models', type: 'custom', renderingMode: '3d',
|
|
34485
|
+
onAdd: function (map, gl) {
|
|
34486
|
+
this.map = map;
|
|
34487
|
+
this.scene = new T.Scene();
|
|
34488
|
+
this.camera = new T.Camera();
|
|
34489
|
+
this.renderer = new T.WebGLRenderer({ canvas: map.getCanvas(), context: gl, antialias: true });
|
|
34490
|
+
this.renderer.autoClear = false;
|
|
34491
|
+
this.scene.add(new T.AmbientLight(0xffffff, 0.8));
|
|
34492
|
+
var sun = new T.DirectionalLight(0xffffff, 0.6);
|
|
34493
|
+
sun.position.set(0.3, -0.5, 1);
|
|
34494
|
+
this.scene.add(sun);
|
|
34495
|
+
this.objects = {};
|
|
34496
|
+
var me = this;
|
|
34497
|
+
new three.GLTFLoader().load(o.model, function (gltf) {
|
|
34498
|
+
me.prototypeObject = gltf.scene;
|
|
34499
|
+
map.triggerRepaint();
|
|
34500
|
+
}, undefined, function (error) { self._reportError(error); });
|
|
34501
|
+
},
|
|
34502
|
+
render: function (gl, args) {
|
|
34503
|
+
if (!this.prototypeObject) { return; }
|
|
34504
|
+
var matrix = args && args.defaultProjectionData ? args.defaultProjectionData.mainMatrix : (args && args.modelViewProjectionMatrix) || args;
|
|
34505
|
+
var seen = {}, me = this, cur = self._layers[id];
|
|
34506
|
+
if (!cur) { return; }
|
|
34507
|
+
cur.data.features.forEach(function (f, i) {
|
|
34508
|
+
var at = pointOf(f);
|
|
34509
|
+
if (!at) { return; }
|
|
34510
|
+
var key = featureKey(cur, f, i);
|
|
34511
|
+
seen[key] = true;
|
|
34512
|
+
var obj = me.objects[key];
|
|
34513
|
+
if (!obj) { obj = me.objects[key] = me.prototypeObject.clone(true); obj.matrixAutoUpdate = false; me.scene.add(obj); }
|
|
34514
|
+
var props = f.properties || {};
|
|
34515
|
+
var mc = engine.MercatorCoordinate.fromLngLat(at, +plainValue(o.altitude, props, f, 0) || 0);
|
|
34516
|
+
var k = mc.meterInMercatorCoordinateUnits() * (+plainValue(o.size, props, f, 1) || 1);
|
|
34517
|
+
var heading = (+plainValue(o.heading, props, f, 0) || 0) * deg;
|
|
34518
|
+
obj.matrix.copy(new T.Matrix4().makeTranslation(mc.x, mc.y, mc.z)
|
|
34519
|
+
.multiply(new T.Matrix4().makeScale(k, -k, k))
|
|
34520
|
+
.multiply(new T.Matrix4().makeRotationZ(heading))
|
|
34521
|
+
.multiply(baseMatrix));
|
|
34522
|
+
obj.matrixWorldNeedsUpdate = true;
|
|
34523
|
+
});
|
|
34524
|
+
Object.keys(this.objects).forEach(function (key) { if (!seen[key]) { me.scene.remove(me.objects[key]); delete me.objects[key]; } });
|
|
34525
|
+
this.camera.projectionMatrix = new T.Matrix4().fromArray(matrix);
|
|
34526
|
+
this.renderer.resetState();
|
|
34527
|
+
this.renderer.render(this.scene, this.camera);
|
|
34528
|
+
},
|
|
34529
|
+
onRemove: function () { if (this.renderer && this.renderer.dispose) { this.renderer.dispose(); } },
|
|
34530
|
+
};
|
|
34531
|
+
return layer;
|
|
34532
|
+
};
|
|
34533
|
+
|
|
34534
|
+
var GOOGLE_LOGO = 'https://maps.gstatic.com/mapfiles/api-3/images/google_white5.png';
|
|
34535
|
+
|
|
34536
|
+
/**
|
|
34537
|
+
* Google's base maps from the Map Tiles API (G10), read straight from Google with a session your server made with
|
|
34538
|
+
* your own Google key; shown in place of the street map like setBaseImagery. As Google's terms ask, the Google logo
|
|
34539
|
+
* is shown and the copyright of the area in view is asked of Google after each move. null takes it off.
|
|
34540
|
+
* @param {Object|null} g { session, key } (or `tiles`: the tile URL template, and `viewport`: the viewport address up
|
|
34541
|
+
* to its query, both with session and key), type ('roadmap' | 'satellite', for the attribution only),
|
|
34542
|
+
* maxzoom (22), labels (keep the street map's labels over Google's imagery; default false),
|
|
34543
|
+
* onFail() (called when Google's first tiles fail and none loads: show your own map instead)
|
|
34544
|
+
*/
|
|
34545
|
+
SMap.prototype.setGoogleBase = function (g) {
|
|
34546
|
+
var self = this;
|
|
34547
|
+
var run = function () {
|
|
34548
|
+
var m = self.engine;
|
|
34549
|
+
if (self._google) {
|
|
34550
|
+
if (self._google.control) { m.removeControl(self._google.control); }
|
|
34551
|
+
self._google.off.forEach(function (x) { m.off(x[0], x[1]); });
|
|
34552
|
+
self._google = null;
|
|
34553
|
+
}
|
|
34554
|
+
if (!g) { self._setBaseImagery(null); return; }
|
|
34555
|
+
if (!g.tiles && !(g.session && g.key)) { throw new Error('sMap: a Google layer needs session and key, or tiles'); }
|
|
34556
|
+
var q = g.session ? 'session=' + encodeURIComponent(g.session) + '&key=' + encodeURIComponent(g.key) : '';
|
|
34557
|
+
var tiles = g.tiles || 'https://tile.googleapis.com/v1/2dtiles/{z}/{x}/{y}?' + q;
|
|
34558
|
+
var viewport = g.viewport || (q ? 'https://tile.googleapis.com/tile/v1/viewport?' + q : null);
|
|
34559
|
+
var state = { off: [], control: null, text: null, errors: 0, loaded: 0, failed: false };
|
|
34560
|
+
self._google = state;
|
|
34561
|
+
self._setBaseImagery({ tiles: [tiles], tileSize: 256, maxzoom: g.maxzoom || 22, labels: !!g.labels });
|
|
34562
|
+
// The logo and the copyright of the area in view, bottom left.
|
|
34563
|
+
var doc = m.getContainer().ownerDocument;
|
|
34564
|
+
var box = doc.createElement('div');
|
|
34565
|
+
box.className = 'smap-google';
|
|
34566
|
+
var logo = doc.createElement('img');
|
|
34567
|
+
logo.src = GOOGLE_LOGO;
|
|
34568
|
+
logo.alt = 'Google';
|
|
34569
|
+
var text = doc.createElement('span');
|
|
34570
|
+
text.className = 'smap-google-copyright';
|
|
34571
|
+
text.textContent = 'Google';
|
|
34572
|
+
box.appendChild(logo);
|
|
34573
|
+
box.appendChild(text);
|
|
34574
|
+
state.text = text;
|
|
34575
|
+
state.control = { onAdd: function () { return box; }, onRemove: function () { if (box.parentNode) { box.parentNode.removeChild(box); } } };
|
|
34576
|
+
m.addControl(state.control, 'bottom-left');
|
|
34577
|
+
var seq = 0;
|
|
34578
|
+
var copyright = function () {
|
|
34579
|
+
if (!viewport || self._google !== state || typeof fetch !== 'function') { return; }
|
|
34580
|
+
var b = m.getBounds(), mine = ++seq;
|
|
34581
|
+
var url = viewport + (viewport.indexOf('?') >= 0 ? '&' : '?') + 'zoom=' + Math.round(m.getZoom() + 1) +
|
|
34582
|
+
'&north=' + b.getNorth() + '&south=' + b.getSouth() + '&east=' + b.getEast() + '&west=' + b.getWest();
|
|
34583
|
+
fetch(url).then(function (r) { return r.ok ? r.json() : null; }).then(function (v) {
|
|
34584
|
+
if (mine === seq && self._google === state) { text.textContent = (v && v.copyright) || 'Google'; }
|
|
34585
|
+
}, function () {});
|
|
34586
|
+
};
|
|
34587
|
+
var watch = function (e) {
|
|
34588
|
+
if (!e || e.sourceId !== BASE_RASTER || state.failed) { return; }
|
|
34589
|
+
if (e.type === 'error') { state.errors += 1; } else if (e.tile) { state.loaded += 1; }
|
|
34590
|
+
if (state.errors >= 4 && state.loaded === 0) {
|
|
34591
|
+
state.failed = true;
|
|
34592
|
+
if (g.onFail) { g.onFail(); }
|
|
34593
|
+
}
|
|
34594
|
+
};
|
|
34595
|
+
m.on('moveend', copyright); state.off.push(['moveend', copyright]);
|
|
34596
|
+
m.on('error', watch); state.off.push(['error', watch]);
|
|
34597
|
+
m.on('sourcedata', watch); state.off.push(['sourcedata', watch]);
|
|
34598
|
+
copyright();
|
|
34599
|
+
};
|
|
34600
|
+
if (this.engine) { run(); } else { this.ready.then(run); }
|
|
34601
|
+
return this;
|
|
34602
|
+
};
|
|
34603
|
+
|
|
34604
|
+
var LAYERS_ICON = '<svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true" focusable="false" fill="none" ' +
|
|
34605
|
+
'stroke="currentColor" stroke-width="2" stroke-linejoin="round"><path d="M12 3 2 8l10 5 10-5-10-5z"/>' +
|
|
34606
|
+
'<path d="m2 13 10 5 10-5"/></svg>';
|
|
34607
|
+
|
|
34608
|
+
/**
|
|
34609
|
+
* The layer switcher: one button opening a list of base maps (one at a time) and overlays (each on or off).
|
|
34610
|
+
* base: [{ id, label, style? (a scheme, 'custom:…' or a style URL), imagery? (as setBaseImagery) }]
|
|
34611
|
+
* overlays: [{ id, label, layers: [layer ids], visible? }]
|
|
34612
|
+
* value (the base shown at first), title (the button's label), onChange({ base, overlays: [ids shown] })
|
|
34613
|
+
*/
|
|
34614
|
+
function layersControl(map, opts) {
|
|
34615
|
+
var base = opts.base || [];
|
|
34616
|
+
var overlays = opts.overlays || [];
|
|
34617
|
+
var current = opts.value || (base[0] && base[0].id) || null;
|
|
34618
|
+
var shown = {};
|
|
34619
|
+
overlays.forEach(function (ov) { shown[ov.id] = ov.visible !== false; });
|
|
34620
|
+
var el = null, button = null, panel = null;
|
|
34621
|
+
function report() {
|
|
34622
|
+
if (opts.onChange) { opts.onChange({ base: current, overlays: overlays.filter(function (ov) { return shown[ov.id]; }).map(function (ov) { return ov.id; }) }); }
|
|
34623
|
+
}
|
|
34624
|
+
function applyBase(entry) {
|
|
34625
|
+
if (!entry) { return; }
|
|
34626
|
+
// The scheme already shown is not loaded again (no flicker when only the imagery changes).
|
|
34627
|
+
if (entry.style !== undefined && entry.style !== null && !map._isStyle(entry.style)) { map.setStyle(entry.style); }
|
|
34628
|
+
map.setBaseImagery(entry.imagery || null);
|
|
34629
|
+
}
|
|
34630
|
+
function applyOverlay(ov) { (ov.layers || []).forEach(function (id) { map.setVisible(id, !!shown[ov.id]); }); }
|
|
34631
|
+
function render() {
|
|
34632
|
+
var doc = el.ownerDocument;
|
|
34633
|
+
while (panel.firstChild) { panel.removeChild(panel.firstChild); }
|
|
34634
|
+
var name = 'smap-base-' + Math.random().toString(36).slice(2);
|
|
34635
|
+
base.forEach(function (entry) {
|
|
34636
|
+
var row = doc.createElement('label');
|
|
34637
|
+
row.className = 'smap-layers-row';
|
|
34638
|
+
var input = doc.createElement('input');
|
|
34639
|
+
input.type = 'radio'; input.name = name; input.value = entry.id; input.checked = entry.id === current;
|
|
34640
|
+
input.addEventListener('change', function () { current = entry.id; applyBase(entry); report(); });
|
|
34641
|
+
row.appendChild(input);
|
|
34642
|
+
row.appendChild(doc.createTextNode(' ' + (entry.label || entry.id)));
|
|
34643
|
+
panel.appendChild(row);
|
|
34644
|
+
});
|
|
34645
|
+
if (base.length && overlays.length) { var hr = doc.createElement('div'); hr.className = 'smap-layers-sep'; panel.appendChild(hr); }
|
|
34646
|
+
overlays.forEach(function (ov) {
|
|
34647
|
+
var row = doc.createElement('label');
|
|
34648
|
+
row.className = 'smap-layers-row';
|
|
34649
|
+
var input = doc.createElement('input');
|
|
34650
|
+
input.type = 'checkbox'; input.value = ov.id; input.checked = !!shown[ov.id];
|
|
34651
|
+
input.addEventListener('change', function () { shown[ov.id] = input.checked; applyOverlay(ov); report(); });
|
|
34652
|
+
row.appendChild(input);
|
|
34653
|
+
row.appendChild(doc.createTextNode(' ' + (ov.label || ov.id)));
|
|
34654
|
+
panel.appendChild(row);
|
|
34655
|
+
});
|
|
34656
|
+
}
|
|
34657
|
+
function open(on) { panel.hidden = !on; button.setAttribute('aria-expanded', on ? 'true' : 'false'); }
|
|
34658
|
+
function close() { if (panel && !panel.hidden) { open(false); } }
|
|
34659
|
+
return {
|
|
34660
|
+
onAdd: function (m) {
|
|
34661
|
+
var doc = m.getContainer().ownerDocument;
|
|
34662
|
+
el = doc.createElement('div');
|
|
34663
|
+
el.className = 'maplibregl-ctrl maplibregl-ctrl-group smap-layers';
|
|
34664
|
+
button = doc.createElement('button');
|
|
34665
|
+
button.type = 'button';
|
|
34666
|
+
button.className = 'smap-layers-button';
|
|
34667
|
+
button.innerHTML = LAYERS_ICON;
|
|
34668
|
+
button.title = opts.title || 'Layers';
|
|
34669
|
+
button.setAttribute('aria-label', opts.title || 'Layers');
|
|
34670
|
+
button.setAttribute('aria-haspopup', 'true');
|
|
34671
|
+
button.addEventListener('click', function () { open(panel.hidden); });
|
|
34672
|
+
panel = doc.createElement('div');
|
|
34673
|
+
panel.className = 'smap-layers-panel';
|
|
34674
|
+
el.appendChild(button);
|
|
34675
|
+
el.appendChild(panel);
|
|
34676
|
+
render();
|
|
34677
|
+
open(false);
|
|
34678
|
+
m.on('click', close);
|
|
34679
|
+
// The starting state: the chosen base and every overlay as given.
|
|
34680
|
+
var entry = base.filter(function (b) { return b.id === current; })[0];
|
|
34681
|
+
if (entry && entry.imagery) { map.setBaseImagery(entry.imagery); }
|
|
34682
|
+
overlays.forEach(function (ov) { if (!shown[ov.id]) { applyOverlay(ov); } });
|
|
34683
|
+
return el;
|
|
34684
|
+
},
|
|
34685
|
+
onRemove: function (m) { m.off('click', close); if (el && el.parentNode) { el.parentNode.removeChild(el); } el = null; },
|
|
34686
|
+
};
|
|
34687
|
+
}
|
|
34688
|
+
|
|
31460
34689
|
// ------------------------------------------------------------------ plan editor (OPEN-ROUTE-EDIT E10)
|
|
31461
34690
|
|
|
31462
34691
|
/**
|
|
@@ -31976,7 +35205,9 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
31976
35205
|
var out = { type: 'smap.layers', version: 1 };
|
|
31977
35206
|
if (!this._customStyle) { out.basemap = { style: this._serverStyle || this._flavor, language: this._language, overrides: clone(this._basemapOverrides) }; }
|
|
31978
35207
|
if (this._sourceOrder.length) { out.sources = this.sources(); }
|
|
31979
|
-
|
|
35208
|
+
// The shared description has points, lines, areas and style layers; custom markers, heat maps and 3D models are
|
|
35209
|
+
// not part of it and are left out.
|
|
35210
|
+
out.layers = this._order.filter(function (id) { return ['points', 'lines', 'polygons', 'style'].indexOf(self._layers[id].kind) >= 0; }).map(function (id) {
|
|
31980
35211
|
var def = self._layers[id];
|
|
31981
35212
|
var e = { id: id, kind: def.kind };
|
|
31982
35213
|
if (def.slot) { e.slot = def.slot; }
|
|
@@ -32035,7 +35266,8 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
32035
35266
|
def.slot = o.slot || null;
|
|
32036
35267
|
def.before = o.before || null;
|
|
32037
35268
|
def.popupFn = typeof o.popup === 'string' ? popupTemplate(o.popup) : null;
|
|
32038
|
-
|
|
35269
|
+
// A layer's place among layers; a per-feature zIndex (custom markers) orders within the layer only.
|
|
35270
|
+
def.zIndex = typeof def.o.zIndex === 'number' ? def.o.zIndex : 0;
|
|
32039
35271
|
def.engineLayers = [];
|
|
32040
35272
|
if (this._layerToolHides(id)) { def.hidden = true; }
|
|
32041
35273
|
this._layers[id] = def;
|
|
@@ -32094,6 +35326,10 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
32094
35326
|
|
|
32095
35327
|
SMap.prototype._drop = function (id, def) {
|
|
32096
35328
|
var m = this.engine;
|
|
35329
|
+
if (def.kind === 'custom') { this._dropCustom(id, def); return; }
|
|
35330
|
+
if (this._ghost && this._ghost.id === id && m) { this._dropGhost(id); }
|
|
35331
|
+
if (this._tooltipFor && this._tooltipFor.id === id) { this._hideTooltip(); }
|
|
35332
|
+
if (def.clusterSet) { this._dropCustom(id + ':clusters', def.clusterSet); def.clusterSet = null; }
|
|
32097
35333
|
if (!m) { return; }
|
|
32098
35334
|
def.engineLayers.forEach(function (l) { if (m.getLayer(l)) { m.removeLayer(l); } });
|
|
32099
35335
|
if (def.kind !== 'style' && m.getSource('smap-' + id)) { m.removeSource('smap-' + id); }
|
|
@@ -32113,13 +35349,24 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
32113
35349
|
Object.keys(this._basemapEdits).forEach(function (id) { self._applyEdits(id, self._basemapEdits[id]); });
|
|
32114
35350
|
this._sourceOrder.forEach(function (id) { if (!m.getSource(id)) { self._engineSource(id); } });
|
|
32115
35351
|
this._applyBuildings();
|
|
35352
|
+
this._imageryHid = [];
|
|
35353
|
+
if (this._baseImagery) { this._applyBaseImagery(); }
|
|
35354
|
+
if (this._terrain) { this._applyTerrain(); }
|
|
32116
35355
|
this._order.forEach(function (id) { self._add(id, self._layers[id]); });
|
|
32117
35356
|
};
|
|
32118
35357
|
|
|
32119
35358
|
SMap.prototype._add = function (id, def) {
|
|
32120
35359
|
var m = this.engine;
|
|
32121
35360
|
var o = def.o;
|
|
35361
|
+
if (def.kind === 'custom') { this._addCustom(id, def); return; }
|
|
32122
35362
|
var before = this._beforeId(id);
|
|
35363
|
+
if (def.kind === 'models') {
|
|
35364
|
+
var modelLayer = this._modelLayer(id, def);
|
|
35365
|
+
if (o.minzoom !== undefined) { modelLayer.minzoom = o.minzoom; }
|
|
35366
|
+
m.addLayer(modelLayer, before);
|
|
35367
|
+
def.engineLayers.push(modelLayer.id);
|
|
35368
|
+
return;
|
|
35369
|
+
}
|
|
32123
35370
|
if (def.kind === 'style') {
|
|
32124
35371
|
// Passed to the engine as it is (a copy, so the engine never holds the object you gave).
|
|
32125
35372
|
var styleLayer = clone(def.layer);
|
|
@@ -32135,6 +35382,8 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
32135
35382
|
if (o.maxzoom !== undefined) { range.maxzoom = o.maxzoom; }
|
|
32136
35383
|
var visibility = def.hidden ? 'none' : 'visible';
|
|
32137
35384
|
var source = { type: 'geojson', data: def.data, promoteId: o.idProperty };
|
|
35385
|
+
// A dragged point is found again by its place in the data (unless idProperty names it).
|
|
35386
|
+
if (def.kind === 'points' && o.draggable && !o.idProperty) { source.generateId = true; }
|
|
32138
35387
|
if (def.kind === 'points' && o.cluster) {
|
|
32139
35388
|
source.cluster = true;
|
|
32140
35389
|
source.clusterRadius = (o.cluster && o.cluster.radius) || 50;
|
|
@@ -32157,7 +35406,8 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
32157
35406
|
var color = o.color || '#4f46e5';
|
|
32158
35407
|
var main;
|
|
32159
35408
|
if (def.kind === 'points') {
|
|
32160
|
-
|
|
35409
|
+
var htmlClusters = o.cluster && (o.cluster.html || o.cluster.ring);
|
|
35410
|
+
if (o.cluster && !htmlClusters) {
|
|
32161
35411
|
add({
|
|
32162
35412
|
id: src + '-clusters', type: 'circle', filter: ['has', 'point_count'],
|
|
32163
35413
|
paint: {
|
|
@@ -32178,6 +35428,7 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
32178
35428
|
});
|
|
32179
35429
|
});
|
|
32180
35430
|
}
|
|
35431
|
+
if (htmlClusters) { this._htmlClusters(id, def, src); }
|
|
32181
35432
|
var only = o.cluster ? ['!', ['has', 'point_count']] : undefined;
|
|
32182
35433
|
var both = function (extra) { return only ? ['all', only, extra] : extra; };
|
|
32183
35434
|
if (o.marker) {
|
|
@@ -32272,6 +35523,30 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
32272
35523
|
paint: { 'text-color': '#1f2937', 'text-halo-color': '#ffffff', 'text-halo-width': 1.4 },
|
|
32273
35524
|
});
|
|
32274
35525
|
}
|
|
35526
|
+
} else if (def.kind === 'heatmap') {
|
|
35527
|
+
main = src + '-heat';
|
|
35528
|
+
var hc = o.colors;
|
|
35529
|
+
var ramp = Array.isArray(hc) && hc.length && Array.isArray(hc[0]) ? hc : null;
|
|
35530
|
+
var colorExpr = ['interpolate', ['linear'], ['heatmap-density']];
|
|
35531
|
+
if (ramp) { ramp.forEach(function (st) { colorExpr.push(st[0], st[1]); }); }
|
|
35532
|
+
else if (Array.isArray(hc) && hc.length) {
|
|
35533
|
+
// Colours from low to high: transparent at no density, then evenly spaced.
|
|
35534
|
+
colorExpr.push(0, 'rgba(0, 0, 0, 0)');
|
|
35535
|
+
hc.forEach(function (c, i) { colorExpr.push((i + 1) / hc.length, c); });
|
|
35536
|
+
} else {
|
|
35537
|
+
colorExpr.push(0, 'rgba(33, 102, 172, 0)', 0.2, 'rgb(103, 169, 207)', 0.4, 'rgb(209, 229, 240)', 0.6, 'rgb(253, 219, 199)', 0.8, 'rgb(239, 138, 98)', 1, 'rgb(178, 24, 43)');
|
|
35538
|
+
}
|
|
35539
|
+
var weight = o.weight === undefined ? 1 : (typeof o.weight === 'number' ? o.weight : ['to-number', valueExpr(o.weight, 1), 1]);
|
|
35540
|
+
add({
|
|
35541
|
+
id: main, type: 'heatmap',
|
|
35542
|
+
paint: {
|
|
35543
|
+
'heatmap-weight': weight,
|
|
35544
|
+
'heatmap-radius': o.radius === undefined ? 20 : o.radius,
|
|
35545
|
+
'heatmap-intensity': o.intensity === undefined ? 1 : o.intensity,
|
|
35546
|
+
'heatmap-opacity': o.opacity === undefined ? 0.8 : o.opacity,
|
|
35547
|
+
'heatmap-color': colorExpr,
|
|
35548
|
+
},
|
|
35549
|
+
});
|
|
32275
35550
|
} else if (def.kind === 'lines') {
|
|
32276
35551
|
main = src + '-lines';
|
|
32277
35552
|
// simplestyle (stroke, stroke-width, stroke-opacity) when the layer gives no colour of its own
|
|
@@ -32296,6 +35571,15 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
32296
35571
|
};
|
|
32297
35572
|
if (o.dash) { paint['line-dasharray'] = o.dash; }
|
|
32298
35573
|
add({ id: main, type: 'line', layout: round, paint: paint });
|
|
35574
|
+
if (o.label) {
|
|
35575
|
+
// A permanent label along the line (a route's name), repeated along long lines.
|
|
35576
|
+
add({
|
|
35577
|
+
id: src + '-labels', type: 'symbol',
|
|
35578
|
+
layout: { 'symbol-placement': 'line', 'symbol-spacing': o.labelSpacing || 250, 'text-field': ['to-string', ['get', o.label]],
|
|
35579
|
+
'text-font': ['Noto Sans Medium'], 'text-size': 12, 'text-keep-upright': true },
|
|
35580
|
+
paint: { 'text-color': valueExpr(o.labelColor, '#1f2937'), 'text-halo-color': '#ffffff', 'text-halo-width': 1.6 },
|
|
35581
|
+
});
|
|
35582
|
+
}
|
|
32299
35583
|
if (o.arrows) {
|
|
32300
35584
|
var ar = o.arrows === true ? {} : o.arrows;
|
|
32301
35585
|
var arrowSize = ar.size || 14;
|
|
@@ -32338,7 +35622,9 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
32338
35622
|
}
|
|
32339
35623
|
// The popup is opened by the map's one click handler (_popupClick), from this layer's own hit test.
|
|
32340
35624
|
def.popupLayer = o.popup ? main : null;
|
|
32341
|
-
if (main &&
|
|
35625
|
+
if (main && o.tooltip) { this._tooltips(id, def, main); }
|
|
35626
|
+
if (main && def.kind === 'points' && o.draggable) { this._dragPoints(id, def, main); }
|
|
35627
|
+
if (main && (o.popup || o.onClick || o.draggable)) {
|
|
32342
35628
|
if (o.onClick) {
|
|
32343
35629
|
this._listen(id, 'click', main, function (e) { o.onClick(e.features[0], e); });
|
|
32344
35630
|
}
|
|
@@ -32409,6 +35695,8 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
32409
35695
|
slots: SLOTS.slice(),
|
|
32410
35696
|
formats: interop.formats,
|
|
32411
35697
|
convert: interop.convert,
|
|
35698
|
+
geo: interop.geo,
|
|
35699
|
+
useThree: interop.useThree,
|
|
32412
35700
|
rasterUrl: interop.rasterUrl,
|
|
32413
35701
|
rasterTileJsonUrl: interop.rasterTileJsonUrl,
|
|
32414
35702
|
styleUrl: interop.styleUrl,
|
|
@@ -32427,9 +35715,10 @@ function smapCreateSdk(engine, interop, plan) {
|
|
|
32427
35715
|
return module.exports;
|
|
32428
35716
|
}
|
|
32429
35717
|
var smapInterop = smapCreateInterop();
|
|
35718
|
+
var smapCanvasLib = smapCreateCanvas();
|
|
32430
35719
|
var smapPlan = smapCreatePlan();
|
|
32431
35720
|
function smapLoad() {
|
|
32432
|
-
if (!smapCore) { smapCore = smapCreateSdk(smapCreateEngine(), smapInterop, smapPlan); }
|
|
35721
|
+
if (!smapCore) { smapCore = smapCreateSdk(smapCreateEngine(), smapInterop, smapCanvasLib, smapPlan); }
|
|
32433
35722
|
return smapCore;
|
|
32434
35723
|
}
|
|
32435
35724
|
// Every top-level name starts with smap: the engine and the SDK run inside this scope and must keep seeing the
|
|
@@ -32443,19 +35732,20 @@ function SMapMap(options) {
|
|
|
32443
35732
|
SMapMap.prototype = core.Map.prototype;
|
|
32444
35733
|
return new core.Map(options);
|
|
32445
35734
|
}
|
|
32446
|
-
var smapVersion = "1.
|
|
35735
|
+
var smapVersion = "1.2.0";
|
|
32447
35736
|
var smapStyles = ['light', 'white', 'grayscale', 'dark', 'black'];
|
|
32448
35737
|
/** The bundled rendering engine, for anything the SDK does not wrap (loaded on the first call). */
|
|
32449
35738
|
function smapGetEngine() { return smapLoad().engine; }
|
|
32450
35739
|
// Templates need no engine: before the first map they come from an SDK made without one.
|
|
32451
35740
|
var smapNoEngine = null;
|
|
32452
35741
|
function smapPopupTemplate(template) {
|
|
32453
|
-
if (!smapCore && !smapNoEngine) { smapNoEngine = smapCreateSdk(null, smapInterop, smapPlan); }
|
|
35742
|
+
if (!smapCore && !smapNoEngine) { smapNoEngine = smapCreateSdk(null, smapInterop, smapCanvasLib, smapPlan); }
|
|
32454
35743
|
return (smapCore || smapNoEngine).popupTemplate(template);
|
|
32455
35744
|
}
|
|
32456
35745
|
var smapSlots = ['bottom', 'roads', 'labels', 'top'];
|
|
32457
35746
|
var smap_formats = smapInterop.formats;
|
|
32458
35747
|
var smap_convert = smapInterop.convert;
|
|
35748
|
+
var smap_geo = smapInterop.geo;
|
|
32459
35749
|
var smap_rasterUrl = smapInterop.rasterUrl;
|
|
32460
35750
|
var smap_rasterTileJsonUrl = smapInterop.rasterTileJsonUrl;
|
|
32461
35751
|
var smap_styleUrl = smapInterop.styleUrl;
|
|
@@ -32465,11 +35755,12 @@ var smap_slotAnchors = smapInterop.slotAnchors;
|
|
|
32465
35755
|
var smap_parseWmtsCapabilities = smapInterop.parseWmtsCapabilities;
|
|
32466
35756
|
var smap_PMTilesArchive = smapInterop.PMTilesArchive;
|
|
32467
35757
|
var smap_useDeck = smapInterop.useDeck;
|
|
35758
|
+
var smap_useThree = smapInterop.useThree;
|
|
32468
35759
|
var smapPlanEditor = smapPlan.PlanEditor;
|
|
32469
35760
|
var smapPlanTexts = smapPlan.texts;
|
|
32470
35761
|
var smapPlanEditorParts = smapPlan.parts;
|
|
32471
35762
|
var smapDefault = { Map: SMapMap, version: smapVersion, styles: smapStyles, getEngine: smapGetEngine, popupTemplate: smapPopupTemplate,
|
|
32472
|
-
slots: smapSlots, formats: smap_formats, convert: smap_convert, rasterUrl: smap_rasterUrl, rasterTileJsonUrl: smap_rasterTileJsonUrl, styleUrl: smap_styleUrl, applyBasemapOverrides: smap_applyBasemapOverrides, basemapClasses: smap_basemapClasses, slotAnchors: smap_slotAnchors, parseWmtsCapabilities: smap_parseWmtsCapabilities, PMTilesArchive: smap_PMTilesArchive, useDeck: smap_useDeck, PlanEditor: smapPlanEditor, planTexts: smapPlanTexts,
|
|
35763
|
+
slots: smapSlots, formats: smap_formats, convert: smap_convert, geo: smap_geo, rasterUrl: smap_rasterUrl, rasterTileJsonUrl: smap_rasterTileJsonUrl, styleUrl: smap_styleUrl, applyBasemapOverrides: smap_applyBasemapOverrides, basemapClasses: smap_basemapClasses, slotAnchors: smap_slotAnchors, parseWmtsCapabilities: smap_parseWmtsCapabilities, PMTilesArchive: smap_PMTilesArchive, useDeck: smap_useDeck, useThree: smap_useThree, PlanEditor: smapPlanEditor, planTexts: smapPlanTexts,
|
|
32473
35764
|
planEditorParts: smapPlanEditorParts };
|
|
32474
35765
|
exports.Map = SMapMap;
|
|
32475
35766
|
exports.version = smapVersion;
|
|
@@ -32479,6 +35770,7 @@ exports.popupTemplate = smapPopupTemplate;
|
|
|
32479
35770
|
exports.slots = smapSlots;
|
|
32480
35771
|
exports.formats = smap_formats;
|
|
32481
35772
|
exports.convert = smap_convert;
|
|
35773
|
+
exports.geo = smap_geo;
|
|
32482
35774
|
exports.rasterUrl = smap_rasterUrl;
|
|
32483
35775
|
exports.rasterTileJsonUrl = smap_rasterTileJsonUrl;
|
|
32484
35776
|
exports.styleUrl = smap_styleUrl;
|
|
@@ -32488,6 +35780,7 @@ exports.slotAnchors = smap_slotAnchors;
|
|
|
32488
35780
|
exports.parseWmtsCapabilities = smap_parseWmtsCapabilities;
|
|
32489
35781
|
exports.PMTilesArchive = smap_PMTilesArchive;
|
|
32490
35782
|
exports.useDeck = smap_useDeck;
|
|
35783
|
+
exports.useThree = smap_useThree;
|
|
32491
35784
|
exports.PlanEditor = smapPlanEditor;
|
|
32492
35785
|
exports.planTexts = smapPlanTexts;
|
|
32493
35786
|
exports.planEditorParts = smapPlanEditorParts;
|