maplair 4.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2804 -0
- package/LICENSE +27 -0
- package/README.md +40 -0
- package/dist/esm/Maplair.js +71 -0
- package/dist/esm/Maplair.js.map +1 -0
- package/dist/esm/_virtual/_rolldown/runtime.js +13 -0
- package/dist/esm/control/AttributionControl.js +79 -0
- package/dist/esm/control/AttributionControl.js.map +1 -0
- package/dist/esm/control/Control.js +81 -0
- package/dist/esm/control/Control.js.map +1 -0
- package/dist/esm/control/LayersControl.js +220 -0
- package/dist/esm/control/LayersControl.js.map +1 -0
- package/dist/esm/control/ScaleControl.js +72 -0
- package/dist/esm/control/ScaleControl.js.map +1 -0
- package/dist/esm/control/ZoomControl.js +86 -0
- package/dist/esm/control/ZoomControl.js.map +1 -0
- package/dist/esm/core/Browser.js +31 -0
- package/dist/esm/core/Browser.js.map +1 -0
- package/dist/esm/core/Class.js +52 -0
- package/dist/esm/core/Class.js.map +1 -0
- package/dist/esm/core/Events.js +149 -0
- package/dist/esm/core/Events.js.map +1 -0
- package/dist/esm/core/Handler.js +32 -0
- package/dist/esm/core/Handler.js.map +1 -0
- package/dist/esm/core/I18n.js +36 -0
- package/dist/esm/core/I18n.js.map +1 -0
- package/dist/esm/core/Util.js +71 -0
- package/dist/esm/core/Util.js.map +1 -0
- package/dist/esm/dom/DomEvent.DoubleTap.js +74 -0
- package/dist/esm/dom/DomEvent.DoubleTap.js.map +1 -0
- package/dist/esm/dom/DomEvent.PointerEvents.js +46 -0
- package/dist/esm/dom/DomEvent.PointerEvents.js.map +1 -0
- package/dist/esm/dom/DomEvent.js +135 -0
- package/dist/esm/dom/DomEvent.js.map +1 -0
- package/dist/esm/dom/DomUtil.js +107 -0
- package/dist/esm/dom/DomUtil.js.map +1 -0
- package/dist/esm/dom/Draggable.js +107 -0
- package/dist/esm/dom/Draggable.js.map +1 -0
- package/dist/esm/dom/PosAnimation.js +56 -0
- package/dist/esm/dom/PosAnimation.js.map +1 -0
- package/dist/esm/geo/LatLng.js +65 -0
- package/dist/esm/geo/LatLng.js.map +1 -0
- package/dist/esm/geo/LatLngBounds.js +114 -0
- package/dist/esm/geo/LatLngBounds.js.map +1 -0
- package/dist/esm/geo/crs/CRS.js +51 -0
- package/dist/esm/geo/crs/CRS.js.map +1 -0
- package/dist/esm/geo/crs/EPSG3395.js +16 -0
- package/dist/esm/geo/crs/EPSG3395.js.map +1 -0
- package/dist/esm/geo/crs/EPSG3857.js +19 -0
- package/dist/esm/geo/crs/EPSG3857.js.map +1 -0
- package/dist/esm/geo/crs/EPSG4326.js +13 -0
- package/dist/esm/geo/crs/EPSG4326.js.map +1 -0
- package/dist/esm/geo/crs/EarthCRS.js +14 -0
- package/dist/esm/geo/crs/EarthCRS.js.map +1 -0
- package/dist/esm/geo/crs/SimpleCRS.js +23 -0
- package/dist/esm/geo/crs/SimpleCRS.js.map +1 -0
- package/dist/esm/geo/projection/Projection.LonLat.js +19 -0
- package/dist/esm/geo/projection/Projection.LonLat.js.map +1 -0
- package/dist/esm/geo/projection/Projection.Mercator.js +33 -0
- package/dist/esm/geo/projection/Projection.Mercator.js.map +1 -0
- package/dist/esm/geo/projection/Projection.SphericalMercator.js +27 -0
- package/dist/esm/geo/projection/Projection.SphericalMercator.js.map +1 -0
- package/dist/esm/geo/projection/index.js +14 -0
- package/dist/esm/geo/projection/index.js.map +1 -0
- package/dist/esm/geometry/Bounds.js +91 -0
- package/dist/esm/geometry/Bounds.js.map +1 -0
- package/dist/esm/geometry/LineUtil.js +169 -0
- package/dist/esm/geometry/LineUtil.js.map +1 -0
- package/dist/esm/geometry/Point.js +119 -0
- package/dist/esm/geometry/Point.js.map +1 -0
- package/dist/esm/geometry/PolyUtil.js +89 -0
- package/dist/esm/geometry/PolyUtil.js.map +1 -0
- package/dist/esm/geometry/Transformation.js +34 -0
- package/dist/esm/geometry/Transformation.js.map +1 -0
- package/dist/esm/layer/BlanketOverlay.js +87 -0
- package/dist/esm/layer/BlanketOverlay.js.map +1 -0
- package/dist/esm/layer/DivOverlay.js +192 -0
- package/dist/esm/layer/DivOverlay.js.map +1 -0
- package/dist/esm/layer/FeatureGroup.js +40 -0
- package/dist/esm/layer/FeatureGroup.js.map +1 -0
- package/dist/esm/layer/GeoJSON.js +192 -0
- package/dist/esm/layer/GeoJSON.js.map +1 -0
- package/dist/esm/layer/ImageOverlay.js +140 -0
- package/dist/esm/layer/ImageOverlay.js.map +1 -0
- package/dist/esm/layer/Layer.js +119 -0
- package/dist/esm/layer/Layer.js.map +1 -0
- package/dist/esm/layer/LayerGroup.js +58 -0
- package/dist/esm/layer/LayerGroup.js.map +1 -0
- package/dist/esm/layer/Popup.js +224 -0
- package/dist/esm/layer/Popup.js.map +1 -0
- package/dist/esm/layer/SVGOverlay.js +21 -0
- package/dist/esm/layer/SVGOverlay.js.map +1 -0
- package/dist/esm/layer/Tooltip.js +228 -0
- package/dist/esm/layer/Tooltip.js.map +1 -0
- package/dist/esm/layer/VideoOverlay.js +54 -0
- package/dist/esm/layer/VideoOverlay.js.map +1 -0
- package/dist/esm/layer/marker/DefaultIcon.js +50 -0
- package/dist/esm/layer/marker/DefaultIcon.js.map +1 -0
- package/dist/esm/layer/marker/DivIcon.js +37 -0
- package/dist/esm/layer/marker/DivIcon.js.map +1 -0
- package/dist/esm/layer/marker/Icon.js +65 -0
- package/dist/esm/layer/marker/Icon.js.map +1 -0
- package/dist/esm/layer/marker/Marker.Drag.js +77 -0
- package/dist/esm/layer/marker/Marker.Drag.js.map +1 -0
- package/dist/esm/layer/marker/Marker.js +235 -0
- package/dist/esm/layer/marker/Marker.js.map +1 -0
- package/dist/esm/layer/tile/GridLayer.js +447 -0
- package/dist/esm/layer/tile/GridLayer.js.map +1 -0
- package/dist/esm/layer/tile/TileLayer.js +139 -0
- package/dist/esm/layer/tile/TileLayer.js.map +1 -0
- package/dist/esm/layer/tile/WMSTileLayer.js +68 -0
- package/dist/esm/layer/tile/WMSTileLayer.js.map +1 -0
- package/dist/esm/layer/vector/Canvas.js +292 -0
- package/dist/esm/layer/vector/Canvas.js.map +1 -0
- package/dist/esm/layer/vector/Circle.js +35 -0
- package/dist/esm/layer/vector/Circle.js.map +1 -0
- package/dist/esm/layer/vector/CircleMarker.js +69 -0
- package/dist/esm/layer/vector/CircleMarker.js.map +1 -0
- package/dist/esm/layer/vector/Path.js +68 -0
- package/dist/esm/layer/vector/Path.js.map +1 -0
- package/dist/esm/layer/vector/Polygon.js +70 -0
- package/dist/esm/layer/vector/Polygon.js.map +1 -0
- package/dist/esm/layer/vector/Polyline.js +152 -0
- package/dist/esm/layer/vector/Polyline.js.map +1 -0
- package/dist/esm/layer/vector/Rectangle.js +25 -0
- package/dist/esm/layer/vector/Rectangle.js.map +1 -0
- package/dist/esm/layer/vector/Renderer.getRenderer.js +31 -0
- package/dist/esm/layer/vector/Renderer.getRenderer.js.map +1 -0
- package/dist/esm/layer/vector/Renderer.js +39 -0
- package/dist/esm/layer/vector/Renderer.js.map +1 -0
- package/dist/esm/layer/vector/SVG.js +115 -0
- package/dist/esm/layer/vector/SVG.js.map +1 -0
- package/dist/esm/map/Map.js +844 -0
- package/dist/esm/map/Map.js.map +1 -0
- package/dist/esm/map/handler/BoxZoomHandler.js +105 -0
- package/dist/esm/map/handler/BoxZoomHandler.js.map +1 -0
- package/dist/esm/map/handler/DoubleClickZoomHandler.js +27 -0
- package/dist/esm/map/handler/DoubleClickZoomHandler.js.map +1 -0
- package/dist/esm/map/handler/DragHandler.js +133 -0
- package/dist/esm/map/handler/DragHandler.js.map +1 -0
- package/dist/esm/map/handler/KeyboardHandler.js +117 -0
- package/dist/esm/map/handler/KeyboardHandler.js.map +1 -0
- package/dist/esm/map/handler/PinchZoomHandler.js +96 -0
- package/dist/esm/map/handler/PinchZoomHandler.js.map +1 -0
- package/dist/esm/map/handler/ScrollWheelZoomHandler.js +50 -0
- package/dist/esm/map/handler/ScrollWheelZoomHandler.js.map +1 -0
- package/dist/esm/map/handler/TapHoldHandler.js +77 -0
- package/dist/esm/map/handler/TapHoldHandler.js.map +1 -0
- package/dist/esm/package.js +6 -0
- package/dist/esm/package.js.map +1 -0
- package/dist/images/layers.svg +1 -0
- package/dist/images/logo.svg +1 -0
- package/dist/images/marker-icon.svg +1 -0
- package/dist/images/marker-shadow.svg +1 -0
- package/dist/maplair-global-src.js +6810 -0
- package/dist/maplair-global-src.js.map +1 -0
- package/dist/maplair-global.js +12 -0
- package/dist/maplair-global.js.map +1 -0
- package/dist/maplair-src.js +6704 -0
- package/dist/maplair-src.js.map +1 -0
- package/dist/maplair.css +730 -0
- package/dist/maplair.js +7 -0
- package/dist/maplair.js.map +1 -0
- package/i18n/cs.json +12 -0
- package/i18n/de.json +12 -0
- package/i18n/en.json +12 -0
- package/i18n/es.json +12 -0
- package/i18n/fr.json +12 -0
- package/i18n/hu.json +12 -0
- package/i18n/it.json +12 -0
- package/i18n/rm.json +12 -0
- package/i18n/sk.json +12 -0
- package/i18n/sl.json +12 -0
- package/package.json +87 -0
- package/src/Maplair.js +24 -0
- package/src/control/AttributionControl.js +147 -0
- package/src/control/Control.js +174 -0
- package/src/control/LayersControl.js +444 -0
- package/src/control/ScaleControl.js +141 -0
- package/src/control/ZoomControl.js +147 -0
- package/src/control/index.js +5 -0
- package/src/core/Browser.js +75 -0
- package/src/core/Class.js +97 -0
- package/src/core/Events.js +334 -0
- package/src/core/Handler.js +59 -0
- package/src/core/I18n.js +76 -0
- package/src/core/Util.js +122 -0
- package/src/core/index.js +8 -0
- package/src/dom/DomEvent.DoubleTap.js +120 -0
- package/src/dom/DomEvent.PointerEvents.js +75 -0
- package/src/dom/DomEvent.js +282 -0
- package/src/dom/DomUtil.js +175 -0
- package/src/dom/Draggable.js +204 -0
- package/src/dom/PosAnimation.js +115 -0
- package/src/dom/index.js +9 -0
- package/src/geo/LatLng.js +163 -0
- package/src/geo/LatLngBounds.js +260 -0
- package/src/geo/crs/CRS.js +143 -0
- package/src/geo/crs/EPSG3395.js +19 -0
- package/src/geo/crs/EPSG3857.js +26 -0
- package/src/geo/crs/EPSG4326.js +21 -0
- package/src/geo/crs/EarthCRS.js +32 -0
- package/src/geo/crs/SimpleCRS.js +35 -0
- package/src/geo/crs/index.js +6 -0
- package/src/geo/index.js +7 -0
- package/src/geo/projection/Projection.LonLat.js +30 -0
- package/src/geo/projection/Projection.Mercator.js +53 -0
- package/src/geo/projection/Projection.SphericalMercator.js +46 -0
- package/src/geo/projection/index.js +26 -0
- package/src/geometry/Bounds.js +222 -0
- package/src/geometry/LineUtil.js +299 -0
- package/src/geometry/Point.js +239 -0
- package/src/geometry/PolyUtil.js +129 -0
- package/src/geometry/Transformation.js +67 -0
- package/src/geometry/index.js +8 -0
- package/src/images/layers.svg +1 -0
- package/src/images/logo.svg +1 -0
- package/src/images/marker-icon.svg +1 -0
- package/src/images/marker-shadow.svg +1 -0
- package/src/layer/BlanketOverlay.js +171 -0
- package/src/layer/DivOverlay.js +353 -0
- package/src/layer/FeatureGroup.js +93 -0
- package/src/layer/GeoJSON.js +432 -0
- package/src/layer/ImageOverlay.js +279 -0
- package/src/layer/Layer.js +273 -0
- package/src/layer/LayerGroup.js +129 -0
- package/src/layer/Popup.js +528 -0
- package/src/layer/SVGOverlay.js +47 -0
- package/src/layer/Tooltip.js +469 -0
- package/src/layer/VideoOverlay.js +111 -0
- package/src/layer/index.js +18 -0
- package/src/layer/marker/DefaultIcon.js +79 -0
- package/src/layer/marker/DivIcon.js +76 -0
- package/src/layer/marker/Icon.js +164 -0
- package/src/layer/marker/Marker.Drag.js +161 -0
- package/src/layer/marker/Marker.js +465 -0
- package/src/layer/marker/index.js +5 -0
- package/src/layer/tile/GridLayer.js +901 -0
- package/src/layer/tile/TileLayer.js +299 -0
- package/src/layer/tile/WMSTileLayer.js +133 -0
- package/src/layer/tile/index.js +3 -0
- package/src/layer/vector/Canvas.js +479 -0
- package/src/layer/vector/Circle.js +87 -0
- package/src/layer/vector/CircleMarker.js +110 -0
- package/src/layer/vector/Path.js +145 -0
- package/src/layer/vector/Polygon.js +160 -0
- package/src/layer/vector/Polyline.js +296 -0
- package/src/layer/vector/Rectangle.js +52 -0
- package/src/layer/vector/Renderer.getRenderer.js +48 -0
- package/src/layer/vector/Renderer.js +75 -0
- package/src/layer/vector/SVG.js +216 -0
- package/src/layer/vector/index.js +10 -0
- package/src/map/Map.js +1830 -0
- package/src/map/handler/BoxZoomHandler.js +156 -0
- package/src/map/handler/DoubleClickZoomHandler.js +62 -0
- package/src/map/handler/DragHandler.js +240 -0
- package/src/map/handler/KeyboardHandler.js +188 -0
- package/src/map/handler/PinchZoomHandler.js +157 -0
- package/src/map/handler/ScrollWheelZoomHandler.js +98 -0
- package/src/map/handler/TapHoldHandler.js +111 -0
- package/src/map/index.js +8 -0
- package/src/maplair.css +730 -0
|
@@ -0,0 +1,901 @@
|
|
|
1
|
+
import {Layer} from '../Layer.js';
|
|
2
|
+
import Browser from '../../core/Browser.js';
|
|
3
|
+
import * as Util from '../../core/Util.js';
|
|
4
|
+
import * as DomUtil from '../../dom/DomUtil.js';
|
|
5
|
+
import {Point} from '../../geometry/Point.js';
|
|
6
|
+
import {Bounds} from '../../geometry/Bounds.js';
|
|
7
|
+
import {LatLngBounds} from '../../geo/LatLngBounds.js';
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
* @class GridLayer
|
|
11
|
+
* @inherits Layer
|
|
12
|
+
*
|
|
13
|
+
* Generic class for handling a tiled grid of HTML elements. This is the base class for all tile layers and replaces `TileLayer.Canvas`.
|
|
14
|
+
* GridLayer can be extended to create a tiled grid of HTML elements like `<canvas>`, `<img>` or `<div>`. GridLayer will handle creating and animating these DOM elements for you.
|
|
15
|
+
*
|
|
16
|
+
*
|
|
17
|
+
* @section Synchronous usage
|
|
18
|
+
* @example
|
|
19
|
+
*
|
|
20
|
+
* To create a custom layer, extend GridLayer and implement the `createTile()` method, which will be passed a `Point` object with the `x`, `y`, and `z` (zoom level) coordinates to draw your tile.
|
|
21
|
+
*
|
|
22
|
+
* ```js
|
|
23
|
+
* class CanvasLayer extends GridLayer {
|
|
24
|
+
* createTile(coords) {
|
|
25
|
+
* // create a <canvas> element for drawing
|
|
26
|
+
* const tile = DomUtil.create('canvas', 'maplair-tile');
|
|
27
|
+
*
|
|
28
|
+
* // setup tile width and height according to the options
|
|
29
|
+
* const size = this.getTileSize();
|
|
30
|
+
* tile.width = size.x;
|
|
31
|
+
* tile.height = size.y;
|
|
32
|
+
*
|
|
33
|
+
* // get a canvas context and draw something on it using coords.x, coords.y and coords.z
|
|
34
|
+
* const ctx = tile.getContext('2d');
|
|
35
|
+
*
|
|
36
|
+
* // return the tile so it can be rendered on screen
|
|
37
|
+
* return tile;
|
|
38
|
+
* }
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @section Asynchronous usage
|
|
43
|
+
* @example
|
|
44
|
+
*
|
|
45
|
+
* Tile creation can also be asynchronous, this is useful when using a third-party drawing library. Once the tile is finished drawing it can be passed to the `done()` callback.
|
|
46
|
+
*
|
|
47
|
+
* ```js
|
|
48
|
+
* class CanvasLayer extends GridLayer {
|
|
49
|
+
* createTile(coords, done) {
|
|
50
|
+
* const error;
|
|
51
|
+
*
|
|
52
|
+
* // create a <canvas> element for drawing
|
|
53
|
+
* const tile = DomUtil.create('canvas', 'maplair-tile');
|
|
54
|
+
*
|
|
55
|
+
* // setup tile width and height according to the options
|
|
56
|
+
* const size = this.getTileSize();
|
|
57
|
+
* tile.width = size.x;
|
|
58
|
+
* tile.height = size.y;
|
|
59
|
+
*
|
|
60
|
+
* // draw something asynchronously and pass the tile to the done() callback
|
|
61
|
+
* setTimeout(function() {
|
|
62
|
+
* done(error, tile);
|
|
63
|
+
* }, 1000);
|
|
64
|
+
*
|
|
65
|
+
* return tile;
|
|
66
|
+
* }
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @section
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
// @constructor GridLayer(options?: GridLayer options)
|
|
75
|
+
// Creates a new instance of GridLayer with the supplied options.
|
|
76
|
+
export class GridLayer extends Layer {
|
|
77
|
+
|
|
78
|
+
static {
|
|
79
|
+
// @section
|
|
80
|
+
// @aka GridLayer options
|
|
81
|
+
this.setDefaultOptions({
|
|
82
|
+
// @option tileSize: Number|Point = 256
|
|
83
|
+
// Width and height of tiles in the grid. Use a number if width and height are equal, or `Point(width, height)` otherwise.
|
|
84
|
+
tileSize: 256,
|
|
85
|
+
|
|
86
|
+
// @option opacity: Number = 1.0
|
|
87
|
+
// Opacity of the tiles. Can be used in the `createTile()` function.
|
|
88
|
+
opacity: 1,
|
|
89
|
+
|
|
90
|
+
// @option updateWhenIdle: Boolean = *
|
|
91
|
+
// Load new tiles only when panning ends.
|
|
92
|
+
// Defaults to `false` (tiles are loaded _during_ panning) unless
|
|
93
|
+
// the browser is configured for reduced motion.
|
|
94
|
+
updateWhenIdle: !Browser.reducedMotion,
|
|
95
|
+
|
|
96
|
+
// @option updateWhenZooming: Boolean = true
|
|
97
|
+
// By default, a smooth zoom animation (during a [pinch zoom](#map-pinchzoom) or a [`flyTo()`](#map-flyto)) will update grid layers every integer zoom level. Setting this option to `false` will update the grid layer only when the smooth animation ends.
|
|
98
|
+
updateWhenZooming: true,
|
|
99
|
+
|
|
100
|
+
// @option updateInterval: Number = 200
|
|
101
|
+
// Tiles will not update more than once every `updateInterval`
|
|
102
|
+
// milliseconds when panning. The default value is increased to
|
|
103
|
+
// five seconds if the browser is configured for reduced motion.
|
|
104
|
+
updateInterval: Browser.reducedMotion ? 5000 : 200,
|
|
105
|
+
|
|
106
|
+
// @option zIndex: Number = 1
|
|
107
|
+
// The explicit zIndex of the tile layer.
|
|
108
|
+
zIndex: 1,
|
|
109
|
+
|
|
110
|
+
// @option bounds: LatLngBounds = undefined
|
|
111
|
+
// If set, tiles will only be loaded inside the set `LatLngBounds`.
|
|
112
|
+
bounds: null,
|
|
113
|
+
|
|
114
|
+
// @option minZoom: Number = 0
|
|
115
|
+
// The minimum zoom level down to which this layer will be displayed (inclusive).
|
|
116
|
+
minZoom: 0,
|
|
117
|
+
|
|
118
|
+
// @option maxZoom: Number = undefined
|
|
119
|
+
// The maximum zoom level up to which this layer will be displayed (inclusive).
|
|
120
|
+
maxZoom: undefined,
|
|
121
|
+
|
|
122
|
+
// @option maxNativeZoom: Number = undefined
|
|
123
|
+
// Maximum zoom number the tile source has available. If it is specified,
|
|
124
|
+
// the tiles on all zoom levels higher than `maxNativeZoom` will be loaded
|
|
125
|
+
// from `maxNativeZoom` level and auto-scaled.
|
|
126
|
+
maxNativeZoom: undefined,
|
|
127
|
+
|
|
128
|
+
// @option minNativeZoom: Number = undefined
|
|
129
|
+
// Minimum zoom number the tile source has available. If it is specified,
|
|
130
|
+
// the tiles on all zoom levels lower than `minNativeZoom` will be loaded
|
|
131
|
+
// from `minNativeZoom` level and auto-scaled.
|
|
132
|
+
minNativeZoom: undefined,
|
|
133
|
+
|
|
134
|
+
// @option noWrap: Boolean = false
|
|
135
|
+
// Whether the layer is wrapped around the antimeridian. If `true`, the
|
|
136
|
+
// GridLayer will only be displayed once at low zoom levels. Has no
|
|
137
|
+
// effect when the [map CRS](#map-crs) doesn't wrap around. Can be used
|
|
138
|
+
// in combination with [`bounds`](#gridlayer-bounds) to prevent requesting
|
|
139
|
+
// tiles outside the CRS limits.
|
|
140
|
+
noWrap: false,
|
|
141
|
+
|
|
142
|
+
// @option pane: String = 'tilePane'
|
|
143
|
+
// `Map pane` where the grid layer will be added.
|
|
144
|
+
pane: 'tilePane',
|
|
145
|
+
|
|
146
|
+
// @option className: String = ''
|
|
147
|
+
// A custom class name to assign to the tile layer. Empty by default.
|
|
148
|
+
className: '',
|
|
149
|
+
|
|
150
|
+
// @option keepBuffer: Number = 2
|
|
151
|
+
// When panning the map, keep this many rows and columns of tiles before unloading them.
|
|
152
|
+
keepBuffer: 2
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
constructor(options) {
|
|
157
|
+
super();
|
|
158
|
+
Util.setOptions(this, options);
|
|
159
|
+
|
|
160
|
+
this.callInitHooks(GridLayer);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
onAdd() {
|
|
164
|
+
this._initContainer();
|
|
165
|
+
|
|
166
|
+
this._levels = {};
|
|
167
|
+
this._tiles = {};
|
|
168
|
+
|
|
169
|
+
this._resetView(); // implicit _update() call
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
beforeAdd(map) {
|
|
173
|
+
map._addZoomLimit(this);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
onRemove(map) {
|
|
177
|
+
this._removeAllTiles();
|
|
178
|
+
this._container.remove();
|
|
179
|
+
map._removeZoomLimit(this);
|
|
180
|
+
this._container = null;
|
|
181
|
+
this._tileZoom = undefined;
|
|
182
|
+
clearTimeout(this._pruneTimeout);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// @method bringToFront: this
|
|
186
|
+
// Brings the tile layer to the top of all tile layers.
|
|
187
|
+
bringToFront() {
|
|
188
|
+
if (this._map) {
|
|
189
|
+
DomUtil.toFront(this._container);
|
|
190
|
+
this._setAutoZIndex(Math.max);
|
|
191
|
+
}
|
|
192
|
+
return this;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// @method bringToBack: this
|
|
196
|
+
// Brings the tile layer to the bottom of all tile layers.
|
|
197
|
+
bringToBack() {
|
|
198
|
+
if (this._map) {
|
|
199
|
+
DomUtil.toBack(this._container);
|
|
200
|
+
this._setAutoZIndex(Math.min);
|
|
201
|
+
}
|
|
202
|
+
return this;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// @method getContainer: HTMLElement
|
|
206
|
+
// Returns the HTML element that contains the tiles for this layer.
|
|
207
|
+
getContainer() {
|
|
208
|
+
return this._container;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// @method setOpacity(opacity: Number): this
|
|
212
|
+
// Changes the [opacity](#gridlayer-opacity) of the grid layer.
|
|
213
|
+
setOpacity(opacity) {
|
|
214
|
+
this.options.opacity = opacity;
|
|
215
|
+
this._updateOpacity();
|
|
216
|
+
return this;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// @method setZIndex(zIndex: Number): this
|
|
220
|
+
// Changes the [zIndex](#gridlayer-zindex) of the grid layer.
|
|
221
|
+
setZIndex(zIndex) {
|
|
222
|
+
this.options.zIndex = zIndex;
|
|
223
|
+
this._updateZIndex();
|
|
224
|
+
|
|
225
|
+
return this;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// @method isLoading: Boolean
|
|
229
|
+
// Returns `true` if any tile in the grid layer has not finished loading.
|
|
230
|
+
isLoading() {
|
|
231
|
+
return this._loading;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// @method redraw: this
|
|
235
|
+
// Causes the layer to clear all the tiles and request them again.
|
|
236
|
+
redraw() {
|
|
237
|
+
if (this._map) {
|
|
238
|
+
this._removeAllTiles();
|
|
239
|
+
const tileZoom = this._clampZoom(this._map.getZoom());
|
|
240
|
+
if (tileZoom !== this._tileZoom) {
|
|
241
|
+
this._tileZoom = tileZoom;
|
|
242
|
+
this._updateLevels();
|
|
243
|
+
}
|
|
244
|
+
this._update();
|
|
245
|
+
}
|
|
246
|
+
return this;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
getEvents() {
|
|
250
|
+
const events = {
|
|
251
|
+
viewprereset: this._invalidateAll,
|
|
252
|
+
viewreset: this._resetView,
|
|
253
|
+
zoom: this._resetView,
|
|
254
|
+
moveend: this._onMoveEnd
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
if (!this.options.updateWhenIdle) {
|
|
258
|
+
// update tiles on move, but not more often than once per given interval
|
|
259
|
+
if (!this._onMove) {
|
|
260
|
+
this._onMove = Util.throttle(this._onMoveEnd, this.options.updateInterval, this);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
events.move = this._onMove;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (this._zoomAnimated) {
|
|
267
|
+
events.zoomanim = this._animateZoom;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return events;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// @section Extension methods
|
|
274
|
+
// Layers extending `GridLayer` shall reimplement the following method.
|
|
275
|
+
// @method createTile(coords: Object, done?: Function): HTMLElement
|
|
276
|
+
// Called only internally, must be overridden by classes extending `GridLayer`.
|
|
277
|
+
// Returns the `HTMLElement` corresponding to the given `coords`. If the `done` callback
|
|
278
|
+
// is specified, it must be called when the tile has finished loading and drawing.
|
|
279
|
+
createTile() {
|
|
280
|
+
return document.createElement('div');
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// @section
|
|
284
|
+
// @method getTileSize: Point
|
|
285
|
+
// Normalizes the [tileSize option](#gridlayer-tilesize) into a point. Used by the `createTile()` method.
|
|
286
|
+
getTileSize() {
|
|
287
|
+
const s = this.options.tileSize;
|
|
288
|
+
return s instanceof Point ? s : new Point(s, s);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
_updateZIndex() {
|
|
292
|
+
if (this._container && this.options.zIndex !== undefined && this.options.zIndex !== null) {
|
|
293
|
+
this._container.style.zIndex = this.options.zIndex;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
_setAutoZIndex(compare) {
|
|
298
|
+
// go through all other layers of the same pane, set zIndex to max + 1 (front) or min - 1 (back)
|
|
299
|
+
|
|
300
|
+
const layers = this.getPane().children;
|
|
301
|
+
let edgeZIndex = -compare(-Infinity, Infinity); // -Infinity for max, Infinity for min
|
|
302
|
+
|
|
303
|
+
for (const layer of layers) {
|
|
304
|
+
const zIndex = layer.style.zIndex;
|
|
305
|
+
|
|
306
|
+
if (layer !== this._container && zIndex) {
|
|
307
|
+
edgeZIndex = compare(edgeZIndex, +zIndex);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (isFinite(edgeZIndex)) {
|
|
312
|
+
this.options.zIndex = edgeZIndex + compare(-1, 1);
|
|
313
|
+
this._updateZIndex();
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
_updateOpacity() {
|
|
318
|
+
if (!this._map) { return; }
|
|
319
|
+
|
|
320
|
+
this._container.style.opacity = this.options.opacity;
|
|
321
|
+
|
|
322
|
+
const now = Date.now();
|
|
323
|
+
let nextFrame = false,
|
|
324
|
+
willPrune = false;
|
|
325
|
+
|
|
326
|
+
for (const tile of Object.values(this._tiles ?? {})) {
|
|
327
|
+
if (!tile.current || !tile.loaded) { continue; }
|
|
328
|
+
|
|
329
|
+
const fade = Math.min(1, (now - tile.loaded) / 200);
|
|
330
|
+
|
|
331
|
+
tile.el.style.opacity = fade;
|
|
332
|
+
if (fade < 1) {
|
|
333
|
+
nextFrame = true;
|
|
334
|
+
} else {
|
|
335
|
+
if (tile.active) {
|
|
336
|
+
willPrune = true;
|
|
337
|
+
} else {
|
|
338
|
+
this._onOpaqueTile(tile);
|
|
339
|
+
}
|
|
340
|
+
tile.active = true;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (willPrune && !this._noPrune) { this._pruneTiles(); }
|
|
345
|
+
|
|
346
|
+
if (nextFrame) {
|
|
347
|
+
cancelAnimationFrame(this._fadeFrame);
|
|
348
|
+
this._fadeFrame = requestAnimationFrame(this._updateOpacity.bind(this));
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
_onOpaqueTile() {}
|
|
353
|
+
|
|
354
|
+
_initContainer() {
|
|
355
|
+
if (this._container) { return; }
|
|
356
|
+
|
|
357
|
+
this._container = DomUtil.create('div', `maplair-layer ${this.options.className ?? ''}`);
|
|
358
|
+
this._updateZIndex();
|
|
359
|
+
|
|
360
|
+
if (this.options.opacity < 1) {
|
|
361
|
+
this._updateOpacity();
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
this.getPane().appendChild(this._container);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
_updateLevels() {
|
|
368
|
+
|
|
369
|
+
const zoom = this._tileZoom,
|
|
370
|
+
maxZoom = this.options.maxZoom;
|
|
371
|
+
|
|
372
|
+
if (zoom === undefined) { return undefined; }
|
|
373
|
+
|
|
374
|
+
for (let z of Object.keys(this._levels)) {
|
|
375
|
+
z = Number(z);
|
|
376
|
+
if (this._levels[z].el.children.length || z === zoom) {
|
|
377
|
+
this._levels[z].el.style.zIndex = maxZoom - Math.abs(zoom - z);
|
|
378
|
+
this._onUpdateLevel(z);
|
|
379
|
+
} else {
|
|
380
|
+
this._levels[z].el.remove();
|
|
381
|
+
this._removeTilesAtZoom(z);
|
|
382
|
+
this._onRemoveLevel(z);
|
|
383
|
+
delete this._levels[z];
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
let level = this._levels[zoom];
|
|
388
|
+
const map = this._map;
|
|
389
|
+
|
|
390
|
+
if (!level) {
|
|
391
|
+
level = this._levels[zoom] = {};
|
|
392
|
+
|
|
393
|
+
level.el = DomUtil.create('div', 'maplair-tile-container maplair-zoom-animated', this._container);
|
|
394
|
+
level.el.style.zIndex = maxZoom;
|
|
395
|
+
|
|
396
|
+
level.origin = map.project(map.unproject(map.getPixelOrigin()), zoom).round();
|
|
397
|
+
level.zoom = zoom;
|
|
398
|
+
|
|
399
|
+
this._setZoomTransform(level, map.getCenter(), map.getZoom());
|
|
400
|
+
|
|
401
|
+
// force reading offsetWidth so the browser considers the newly added element for transition
|
|
402
|
+
Util.falseFn(level.el.offsetWidth);
|
|
403
|
+
|
|
404
|
+
this._onCreateLevel(level);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
this._level = level;
|
|
408
|
+
|
|
409
|
+
return level;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
_onUpdateLevel() {}
|
|
413
|
+
|
|
414
|
+
_onRemoveLevel() {}
|
|
415
|
+
|
|
416
|
+
_onCreateLevel() {}
|
|
417
|
+
|
|
418
|
+
_pruneTiles() {
|
|
419
|
+
if (!this._map) {
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const zoom = this._map.getZoom();
|
|
424
|
+
if (zoom > this.options.maxZoom ||
|
|
425
|
+
zoom < this.options.minZoom) {
|
|
426
|
+
this._removeAllTiles();
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
for (const tile of Object.values(this._tiles)) {
|
|
431
|
+
tile.retain = tile.current;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
for (const tile of Object.values(this._tiles)) {
|
|
435
|
+
if (tile.current && !tile.active) {
|
|
436
|
+
const coords = tile.coords;
|
|
437
|
+
if (!this._retainParent(coords.x, coords.y, coords.z, coords.z - 5)) {
|
|
438
|
+
this._retainChildren(coords.x, coords.y, coords.z, coords.z + 2);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
for (const [key, tile] of Object.entries(this._tiles)) {
|
|
444
|
+
if (!tile.retain) {
|
|
445
|
+
this._removeTile(key);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
_removeTilesAtZoom(zoom) {
|
|
451
|
+
for (const [key, tile] of Object.entries(this._tiles)) {
|
|
452
|
+
if (tile.coords.z === zoom) {
|
|
453
|
+
this._removeTile(key);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
_removeAllTiles() {
|
|
459
|
+
for (const key of Object.keys(this._tiles)) {
|
|
460
|
+
this._removeTile(key);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
_invalidateAll() {
|
|
465
|
+
for (const z of Object.keys(this._levels)) {
|
|
466
|
+
this._levels[z].el.remove();
|
|
467
|
+
this._onRemoveLevel(Number(z));
|
|
468
|
+
delete this._levels[z];
|
|
469
|
+
}
|
|
470
|
+
this._removeAllTiles();
|
|
471
|
+
|
|
472
|
+
this._tileZoom = undefined;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
_retainParent(x, y, z, minZoom) {
|
|
476
|
+
const x2 = Math.floor(x / 2),
|
|
477
|
+
y2 = Math.floor(y / 2),
|
|
478
|
+
z2 = z - 1,
|
|
479
|
+
coords2 = new Point(+x2, +y2);
|
|
480
|
+
coords2.z = +z2;
|
|
481
|
+
|
|
482
|
+
const key = this._tileCoordsToKey(coords2),
|
|
483
|
+
tile = this._tiles[key];
|
|
484
|
+
|
|
485
|
+
if (tile?.active) {
|
|
486
|
+
tile.retain = true;
|
|
487
|
+
return true;
|
|
488
|
+
|
|
489
|
+
} else if (tile?.loaded) {
|
|
490
|
+
tile.retain = true;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
if (z2 > minZoom) {
|
|
494
|
+
return this._retainParent(x2, y2, z2, minZoom);
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
return false;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
_retainChildren(x, y, z, maxZoom) {
|
|
501
|
+
|
|
502
|
+
for (let i = 2 * x; i < 2 * x + 2; i++) {
|
|
503
|
+
for (let j = 2 * y; j < 2 * y + 2; j++) {
|
|
504
|
+
|
|
505
|
+
const coords = new Point(i, j);
|
|
506
|
+
coords.z = z + 1;
|
|
507
|
+
|
|
508
|
+
const key = this._tileCoordsToKey(coords),
|
|
509
|
+
tile = this._tiles[key];
|
|
510
|
+
|
|
511
|
+
if (tile?.active) {
|
|
512
|
+
tile.retain = true;
|
|
513
|
+
continue;
|
|
514
|
+
|
|
515
|
+
} else if (tile?.loaded) {
|
|
516
|
+
tile.retain = true;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
if (z + 1 < maxZoom) {
|
|
520
|
+
this._retainChildren(i, j, z + 1, maxZoom);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
_resetView(e) {
|
|
527
|
+
const animating = e && (e.pinch || e.flyTo);
|
|
528
|
+
this._setView(this._map.getCenter(), this._map.getZoom(), animating, animating);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
_animateZoom(e) {
|
|
532
|
+
this._setView(e.center, e.zoom, true, e.noUpdate);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
_clampZoom(zoom) {
|
|
536
|
+
const options = this.options;
|
|
537
|
+
|
|
538
|
+
if (undefined !== options.minNativeZoom && zoom < options.minNativeZoom) {
|
|
539
|
+
return options.minNativeZoom;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
if (undefined !== options.maxNativeZoom && options.maxNativeZoom < zoom) {
|
|
543
|
+
return options.maxNativeZoom;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
return zoom;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
_setView(center, zoom, noPrune, noUpdate) {
|
|
550
|
+
let tileZoom = Math.round(zoom);
|
|
551
|
+
if ((this.options.maxZoom !== undefined && tileZoom > this.options.maxZoom) ||
|
|
552
|
+
(this.options.minZoom !== undefined && tileZoom < this.options.minZoom)) {
|
|
553
|
+
tileZoom = undefined;
|
|
554
|
+
} else {
|
|
555
|
+
tileZoom = this._clampZoom(tileZoom);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
const tileZoomChanged = this.options.updateWhenZooming && (tileZoom !== this._tileZoom);
|
|
559
|
+
|
|
560
|
+
if (!noUpdate || tileZoomChanged) {
|
|
561
|
+
|
|
562
|
+
this._tileZoom = tileZoom;
|
|
563
|
+
|
|
564
|
+
if (this._abortLoading) {
|
|
565
|
+
this._abortLoading();
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
this._updateLevels();
|
|
569
|
+
this._resetGrid();
|
|
570
|
+
|
|
571
|
+
if (tileZoom !== undefined) {
|
|
572
|
+
this._update(center);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
if (!noPrune) {
|
|
576
|
+
this._pruneTiles();
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// Flag to prevent _updateOpacity from pruning tiles during
|
|
580
|
+
// a zoom anim or a pinch gesture
|
|
581
|
+
this._noPrune = !!noPrune;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
this._setZoomTransforms(center, zoom);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
_setZoomTransforms(center, zoom) {
|
|
588
|
+
for (const level of Object.values(this._levels)) {
|
|
589
|
+
this._setZoomTransform(level, center, zoom);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
_setZoomTransform(level, center, zoom) {
|
|
594
|
+
const scale = this._map.getZoomScale(zoom, level.zoom),
|
|
595
|
+
translate = level.origin.multiplyBy(scale)
|
|
596
|
+
.subtract(this._map._getNewPixelOrigin(center, zoom)).round();
|
|
597
|
+
|
|
598
|
+
DomUtil.setTransform(level.el, translate, scale);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
_resetGrid() {
|
|
602
|
+
const map = this._map,
|
|
603
|
+
crs = map.options.crs,
|
|
604
|
+
tileSize = this._tileSize = this.getTileSize(),
|
|
605
|
+
tileZoom = this._tileZoom;
|
|
606
|
+
|
|
607
|
+
const bounds = this._map.getPixelWorldBounds(this._tileZoom);
|
|
608
|
+
if (bounds) {
|
|
609
|
+
this._globalTileRange = this._pxBoundsToTileRange(bounds);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
this._wrapX = crs.wrapLng && !this.options.noWrap && [
|
|
613
|
+
Math.floor(map.project([0, crs.wrapLng[0]], tileZoom).x / tileSize.x),
|
|
614
|
+
Math.ceil(map.project([0, crs.wrapLng[1]], tileZoom).x / tileSize.y)
|
|
615
|
+
];
|
|
616
|
+
this._wrapY = crs.wrapLat && !this.options.noWrap && [
|
|
617
|
+
Math.floor(map.project([crs.wrapLat[0], 0], tileZoom).y / tileSize.x),
|
|
618
|
+
Math.ceil(map.project([crs.wrapLat[1], 0], tileZoom).y / tileSize.y)
|
|
619
|
+
];
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
_onMoveEnd() {
|
|
623
|
+
if (!this._map || this._map._animatingZoom) { return; }
|
|
624
|
+
|
|
625
|
+
this._update();
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
_getTiledPixelBounds(center) {
|
|
629
|
+
const map = this._map,
|
|
630
|
+
mapZoom = map._animatingZoom ? Math.max(map._animateToZoom, map.getZoom()) : map.getZoom(),
|
|
631
|
+
scale = map.getZoomScale(mapZoom, this._tileZoom),
|
|
632
|
+
pixelCenter = map.project(center, this._tileZoom).floor(),
|
|
633
|
+
halfSize = map.getSize().divideBy(scale * 2);
|
|
634
|
+
|
|
635
|
+
return new Bounds(pixelCenter.subtract(halfSize), pixelCenter.add(halfSize));
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// Private method to load tiles in the grid's active zoom level according to map bounds
|
|
639
|
+
_update(center) {
|
|
640
|
+
const map = this._map;
|
|
641
|
+
if (!map) { return; }
|
|
642
|
+
const zoom = this._clampZoom(map.getZoom());
|
|
643
|
+
|
|
644
|
+
if (center === undefined) { center = map.getCenter(); }
|
|
645
|
+
if (this._tileZoom === undefined) { return; } // if out of minzoom/maxzoom
|
|
646
|
+
|
|
647
|
+
const pixelBounds = this._getTiledPixelBounds(center),
|
|
648
|
+
tileRange = this._pxBoundsToTileRange(pixelBounds),
|
|
649
|
+
tileCenter = tileRange.getCenter(),
|
|
650
|
+
queue = [],
|
|
651
|
+
margin = this.options.keepBuffer,
|
|
652
|
+
noPruneRange = new Bounds(tileRange.getBottomLeft().subtract([margin, -margin]),
|
|
653
|
+
tileRange.getTopRight().add([margin, -margin]));
|
|
654
|
+
|
|
655
|
+
// Sanity check: panic if the tile range contains Infinity somewhere.
|
|
656
|
+
if (!(isFinite(tileRange.min.x) &&
|
|
657
|
+
isFinite(tileRange.min.y) &&
|
|
658
|
+
isFinite(tileRange.max.x) &&
|
|
659
|
+
isFinite(tileRange.max.y))) { throw new Error('Attempted to load an infinite number of tiles'); }
|
|
660
|
+
|
|
661
|
+
for (const tile of Object.values(this._tiles)) {
|
|
662
|
+
const c = tile.coords;
|
|
663
|
+
if (c.z !== this._tileZoom || !noPruneRange.contains(new Point(c.x, c.y))) {
|
|
664
|
+
tile.current = false;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// _update just loads more tiles. If the tile zoom level differs too much
|
|
669
|
+
// from the map's, let _setView reset levels and prune old tiles.
|
|
670
|
+
if (Math.abs(zoom - this._tileZoom) > 1) { this._setView(center, zoom); return; }
|
|
671
|
+
|
|
672
|
+
// create a queue of coordinates to load tiles from
|
|
673
|
+
for (let j = tileRange.min.y; j <= tileRange.max.y; j++) {
|
|
674
|
+
for (let i = tileRange.min.x; i <= tileRange.max.x; i++) {
|
|
675
|
+
const coords = new Point(i, j);
|
|
676
|
+
coords.z = this._tileZoom;
|
|
677
|
+
|
|
678
|
+
if (!this._isValidTile(coords)) { continue; }
|
|
679
|
+
|
|
680
|
+
const tile = this._tiles[this._tileCoordsToKey(coords)];
|
|
681
|
+
if (tile) {
|
|
682
|
+
tile.current = true;
|
|
683
|
+
} else {
|
|
684
|
+
queue.push(coords);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
// sort tile queue to load tiles in order of their distance to center
|
|
690
|
+
queue.sort((a, b) => a.distanceTo(tileCenter) - b.distanceTo(tileCenter));
|
|
691
|
+
|
|
692
|
+
if (queue.length !== 0) {
|
|
693
|
+
// if it's the first batch of tiles to load
|
|
694
|
+
if (!this._loading) {
|
|
695
|
+
this._loading = true;
|
|
696
|
+
// @event loading: Event
|
|
697
|
+
// Fired when the grid layer starts loading tiles.
|
|
698
|
+
this.fire('loading');
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// create DOM fragment to append tiles in one batch
|
|
702
|
+
const fragment = document.createDocumentFragment();
|
|
703
|
+
|
|
704
|
+
for (const q of queue) {
|
|
705
|
+
this._addTile(q, fragment);
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
this._level.el.appendChild(fragment);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
_isValidTile(coords) {
|
|
713
|
+
const crs = this._map.options.crs;
|
|
714
|
+
|
|
715
|
+
if (!crs.infinite) {
|
|
716
|
+
// don't load tile if it's out of bounds and not wrapped
|
|
717
|
+
const bounds = this._globalTileRange;
|
|
718
|
+
if ((!crs.wrapLng && (coords.x < bounds.min.x || coords.x > bounds.max.x)) ||
|
|
719
|
+
(!crs.wrapLat && (coords.y < bounds.min.y || coords.y > bounds.max.y))) { return false; }
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
if (!this.options.bounds) { return true; }
|
|
723
|
+
|
|
724
|
+
// don't load tile if it doesn't intersect the bounds in options
|
|
725
|
+
const tileBounds = this._tileCoordsToBounds(coords);
|
|
726
|
+
return new LatLngBounds(this.options.bounds).overlaps(tileBounds);
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
_keyToBounds(key) {
|
|
730
|
+
return this._tileCoordsToBounds(this._keyToTileCoords(key));
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
_tileCoordsToNwSe(coords) {
|
|
734
|
+
const map = this._map,
|
|
735
|
+
tileSize = this.getTileSize(),
|
|
736
|
+
nwPoint = coords.scaleBy(tileSize),
|
|
737
|
+
sePoint = nwPoint.add(tileSize),
|
|
738
|
+
nw = map.unproject(nwPoint, coords.z),
|
|
739
|
+
se = map.unproject(sePoint, coords.z);
|
|
740
|
+
return [nw, se];
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
// converts tile coordinates to its geographical bounds
|
|
744
|
+
_tileCoordsToBounds(coords) {
|
|
745
|
+
const bp = this._tileCoordsToNwSe(coords);
|
|
746
|
+
let bounds = new LatLngBounds(bp[0], bp[1]);
|
|
747
|
+
|
|
748
|
+
if (!this.options.noWrap) {
|
|
749
|
+
bounds = this._map.wrapLatLngBounds(bounds);
|
|
750
|
+
}
|
|
751
|
+
return bounds;
|
|
752
|
+
}
|
|
753
|
+
// converts tile coordinates to key for the tile cache
|
|
754
|
+
_tileCoordsToKey(coords) {
|
|
755
|
+
return `${coords.x}:${coords.y}:${coords.z}`;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
// converts tile cache key to coordinates
|
|
759
|
+
_keyToTileCoords(key) {
|
|
760
|
+
const k = key.split(':'),
|
|
761
|
+
coords = new Point(+k[0], +k[1]);
|
|
762
|
+
coords.z = +k[2];
|
|
763
|
+
return coords;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
_removeTile(key) {
|
|
767
|
+
const tile = this._tiles[key];
|
|
768
|
+
if (!tile) { return; }
|
|
769
|
+
|
|
770
|
+
tile.el.remove();
|
|
771
|
+
|
|
772
|
+
delete this._tiles[key];
|
|
773
|
+
|
|
774
|
+
// @event tileunload: TileEvent
|
|
775
|
+
// Fired when a tile is removed (e.g. when a tile goes off the screen).
|
|
776
|
+
this.fire('tileunload', {
|
|
777
|
+
tile: tile.el,
|
|
778
|
+
coords: this._keyToTileCoords(key)
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
_initTile(tile) {
|
|
783
|
+
tile.classList.add('maplair-tile');
|
|
784
|
+
|
|
785
|
+
const tileSize = this.getTileSize();
|
|
786
|
+
tile.style.width = `${tileSize.x}px`;
|
|
787
|
+
tile.style.height = `${tileSize.y}px`;
|
|
788
|
+
|
|
789
|
+
tile.onselectstart = Util.falseFn;
|
|
790
|
+
tile.onpointermove = Util.falseFn;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
_addTile(coords, container) {
|
|
794
|
+
const tilePos = this._getTilePos(coords),
|
|
795
|
+
key = this._tileCoordsToKey(coords);
|
|
796
|
+
|
|
797
|
+
const tile = this.createTile(this._wrapCoords(coords), this._tileReady.bind(this, coords));
|
|
798
|
+
|
|
799
|
+
this._initTile(tile);
|
|
800
|
+
|
|
801
|
+
// if createTile is defined with a second argument ("done" callback),
|
|
802
|
+
// we know that tile is async and will be ready later; otherwise
|
|
803
|
+
if (this.createTile.length < 2) {
|
|
804
|
+
// mark tile as ready, but delay one frame for opacity animation to happen
|
|
805
|
+
requestAnimationFrame(this._tileReady.bind(this, coords, null, tile));
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
DomUtil.setPosition(tile, tilePos);
|
|
809
|
+
|
|
810
|
+
// save tile in cache
|
|
811
|
+
this._tiles[key] = {
|
|
812
|
+
el: tile,
|
|
813
|
+
coords,
|
|
814
|
+
current: true
|
|
815
|
+
};
|
|
816
|
+
|
|
817
|
+
container.appendChild(tile);
|
|
818
|
+
// @event tileloadstart: TileEvent
|
|
819
|
+
// Fired when a tile is requested and starts loading.
|
|
820
|
+
this.fire('tileloadstart', {
|
|
821
|
+
tile,
|
|
822
|
+
coords
|
|
823
|
+
});
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
_tileReady(coords, err, tile) {
|
|
827
|
+
if (err) {
|
|
828
|
+
// @event tileerror: TileErrorEvent
|
|
829
|
+
// Fired when there is an error loading a tile.
|
|
830
|
+
this.fire('tileerror', {
|
|
831
|
+
error: err,
|
|
832
|
+
tile,
|
|
833
|
+
coords
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
const key = this._tileCoordsToKey(coords);
|
|
838
|
+
|
|
839
|
+
tile = this._tiles[key];
|
|
840
|
+
if (!tile) { return; }
|
|
841
|
+
|
|
842
|
+
tile.loaded = Date.now();
|
|
843
|
+
if (this._map._fadeAnimated) {
|
|
844
|
+
tile.el.style.opacity = 0;
|
|
845
|
+
cancelAnimationFrame(this._fadeFrame);
|
|
846
|
+
this._fadeFrame = requestAnimationFrame(this._updateOpacity.bind(this));
|
|
847
|
+
} else {
|
|
848
|
+
tile.active = true;
|
|
849
|
+
this._pruneTiles();
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
if (!err) {
|
|
853
|
+
tile.el.classList.add('maplair-tile-loaded');
|
|
854
|
+
|
|
855
|
+
// @event tileload: TileEvent
|
|
856
|
+
// Fired when a tile loads.
|
|
857
|
+
this.fire('tileload', {
|
|
858
|
+
tile: tile.el,
|
|
859
|
+
coords
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
if (this._noTilesToLoad()) {
|
|
864
|
+
this._loading = false;
|
|
865
|
+
// @event load: Event
|
|
866
|
+
// Fired when the grid layer loaded all visible tiles.
|
|
867
|
+
this.fire('load');
|
|
868
|
+
|
|
869
|
+
if (!this._map._fadeAnimated) {
|
|
870
|
+
requestAnimationFrame(this._pruneTiles.bind(this));
|
|
871
|
+
} else {
|
|
872
|
+
// Wait a bit more than 0.2 secs (the duration of the tile fade-in)
|
|
873
|
+
// to trigger a pruning.
|
|
874
|
+
this._pruneTimeout = setTimeout(this._pruneTiles.bind(this), 250);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
_getTilePos(coords) {
|
|
880
|
+
return coords.scaleBy(this.getTileSize()).subtract(this._level.origin);
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
_wrapCoords(coords) {
|
|
884
|
+
const newCoords = new Point(
|
|
885
|
+
this._wrapX ? Util.wrapNum(coords.x, this._wrapX) : coords.x,
|
|
886
|
+
this._wrapY ? Util.wrapNum(coords.y, this._wrapY) : coords.y);
|
|
887
|
+
newCoords.z = coords.z;
|
|
888
|
+
return newCoords;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
_pxBoundsToTileRange(bounds) {
|
|
892
|
+
const tileSize = this.getTileSize();
|
|
893
|
+
return new Bounds(
|
|
894
|
+
bounds.min.unscaleBy(tileSize).floor(),
|
|
895
|
+
bounds.max.unscaleBy(tileSize).ceil().subtract([1, 1]));
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
_noTilesToLoad() {
|
|
899
|
+
return Object.values(this._tiles).every(t => t.loaded);
|
|
900
|
+
}
|
|
901
|
+
}
|