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.
Files changed (262) hide show
  1. package/CHANGELOG.md +2804 -0
  2. package/LICENSE +27 -0
  3. package/README.md +40 -0
  4. package/dist/esm/Maplair.js +71 -0
  5. package/dist/esm/Maplair.js.map +1 -0
  6. package/dist/esm/_virtual/_rolldown/runtime.js +13 -0
  7. package/dist/esm/control/AttributionControl.js +79 -0
  8. package/dist/esm/control/AttributionControl.js.map +1 -0
  9. package/dist/esm/control/Control.js +81 -0
  10. package/dist/esm/control/Control.js.map +1 -0
  11. package/dist/esm/control/LayersControl.js +220 -0
  12. package/dist/esm/control/LayersControl.js.map +1 -0
  13. package/dist/esm/control/ScaleControl.js +72 -0
  14. package/dist/esm/control/ScaleControl.js.map +1 -0
  15. package/dist/esm/control/ZoomControl.js +86 -0
  16. package/dist/esm/control/ZoomControl.js.map +1 -0
  17. package/dist/esm/core/Browser.js +31 -0
  18. package/dist/esm/core/Browser.js.map +1 -0
  19. package/dist/esm/core/Class.js +52 -0
  20. package/dist/esm/core/Class.js.map +1 -0
  21. package/dist/esm/core/Events.js +149 -0
  22. package/dist/esm/core/Events.js.map +1 -0
  23. package/dist/esm/core/Handler.js +32 -0
  24. package/dist/esm/core/Handler.js.map +1 -0
  25. package/dist/esm/core/I18n.js +36 -0
  26. package/dist/esm/core/I18n.js.map +1 -0
  27. package/dist/esm/core/Util.js +71 -0
  28. package/dist/esm/core/Util.js.map +1 -0
  29. package/dist/esm/dom/DomEvent.DoubleTap.js +74 -0
  30. package/dist/esm/dom/DomEvent.DoubleTap.js.map +1 -0
  31. package/dist/esm/dom/DomEvent.PointerEvents.js +46 -0
  32. package/dist/esm/dom/DomEvent.PointerEvents.js.map +1 -0
  33. package/dist/esm/dom/DomEvent.js +135 -0
  34. package/dist/esm/dom/DomEvent.js.map +1 -0
  35. package/dist/esm/dom/DomUtil.js +107 -0
  36. package/dist/esm/dom/DomUtil.js.map +1 -0
  37. package/dist/esm/dom/Draggable.js +107 -0
  38. package/dist/esm/dom/Draggable.js.map +1 -0
  39. package/dist/esm/dom/PosAnimation.js +56 -0
  40. package/dist/esm/dom/PosAnimation.js.map +1 -0
  41. package/dist/esm/geo/LatLng.js +65 -0
  42. package/dist/esm/geo/LatLng.js.map +1 -0
  43. package/dist/esm/geo/LatLngBounds.js +114 -0
  44. package/dist/esm/geo/LatLngBounds.js.map +1 -0
  45. package/dist/esm/geo/crs/CRS.js +51 -0
  46. package/dist/esm/geo/crs/CRS.js.map +1 -0
  47. package/dist/esm/geo/crs/EPSG3395.js +16 -0
  48. package/dist/esm/geo/crs/EPSG3395.js.map +1 -0
  49. package/dist/esm/geo/crs/EPSG3857.js +19 -0
  50. package/dist/esm/geo/crs/EPSG3857.js.map +1 -0
  51. package/dist/esm/geo/crs/EPSG4326.js +13 -0
  52. package/dist/esm/geo/crs/EPSG4326.js.map +1 -0
  53. package/dist/esm/geo/crs/EarthCRS.js +14 -0
  54. package/dist/esm/geo/crs/EarthCRS.js.map +1 -0
  55. package/dist/esm/geo/crs/SimpleCRS.js +23 -0
  56. package/dist/esm/geo/crs/SimpleCRS.js.map +1 -0
  57. package/dist/esm/geo/projection/Projection.LonLat.js +19 -0
  58. package/dist/esm/geo/projection/Projection.LonLat.js.map +1 -0
  59. package/dist/esm/geo/projection/Projection.Mercator.js +33 -0
  60. package/dist/esm/geo/projection/Projection.Mercator.js.map +1 -0
  61. package/dist/esm/geo/projection/Projection.SphericalMercator.js +27 -0
  62. package/dist/esm/geo/projection/Projection.SphericalMercator.js.map +1 -0
  63. package/dist/esm/geo/projection/index.js +14 -0
  64. package/dist/esm/geo/projection/index.js.map +1 -0
  65. package/dist/esm/geometry/Bounds.js +91 -0
  66. package/dist/esm/geometry/Bounds.js.map +1 -0
  67. package/dist/esm/geometry/LineUtil.js +169 -0
  68. package/dist/esm/geometry/LineUtil.js.map +1 -0
  69. package/dist/esm/geometry/Point.js +119 -0
  70. package/dist/esm/geometry/Point.js.map +1 -0
  71. package/dist/esm/geometry/PolyUtil.js +89 -0
  72. package/dist/esm/geometry/PolyUtil.js.map +1 -0
  73. package/dist/esm/geometry/Transformation.js +34 -0
  74. package/dist/esm/geometry/Transformation.js.map +1 -0
  75. package/dist/esm/layer/BlanketOverlay.js +87 -0
  76. package/dist/esm/layer/BlanketOverlay.js.map +1 -0
  77. package/dist/esm/layer/DivOverlay.js +192 -0
  78. package/dist/esm/layer/DivOverlay.js.map +1 -0
  79. package/dist/esm/layer/FeatureGroup.js +40 -0
  80. package/dist/esm/layer/FeatureGroup.js.map +1 -0
  81. package/dist/esm/layer/GeoJSON.js +192 -0
  82. package/dist/esm/layer/GeoJSON.js.map +1 -0
  83. package/dist/esm/layer/ImageOverlay.js +140 -0
  84. package/dist/esm/layer/ImageOverlay.js.map +1 -0
  85. package/dist/esm/layer/Layer.js +119 -0
  86. package/dist/esm/layer/Layer.js.map +1 -0
  87. package/dist/esm/layer/LayerGroup.js +58 -0
  88. package/dist/esm/layer/LayerGroup.js.map +1 -0
  89. package/dist/esm/layer/Popup.js +224 -0
  90. package/dist/esm/layer/Popup.js.map +1 -0
  91. package/dist/esm/layer/SVGOverlay.js +21 -0
  92. package/dist/esm/layer/SVGOverlay.js.map +1 -0
  93. package/dist/esm/layer/Tooltip.js +228 -0
  94. package/dist/esm/layer/Tooltip.js.map +1 -0
  95. package/dist/esm/layer/VideoOverlay.js +54 -0
  96. package/dist/esm/layer/VideoOverlay.js.map +1 -0
  97. package/dist/esm/layer/marker/DefaultIcon.js +50 -0
  98. package/dist/esm/layer/marker/DefaultIcon.js.map +1 -0
  99. package/dist/esm/layer/marker/DivIcon.js +37 -0
  100. package/dist/esm/layer/marker/DivIcon.js.map +1 -0
  101. package/dist/esm/layer/marker/Icon.js +65 -0
  102. package/dist/esm/layer/marker/Icon.js.map +1 -0
  103. package/dist/esm/layer/marker/Marker.Drag.js +77 -0
  104. package/dist/esm/layer/marker/Marker.Drag.js.map +1 -0
  105. package/dist/esm/layer/marker/Marker.js +235 -0
  106. package/dist/esm/layer/marker/Marker.js.map +1 -0
  107. package/dist/esm/layer/tile/GridLayer.js +447 -0
  108. package/dist/esm/layer/tile/GridLayer.js.map +1 -0
  109. package/dist/esm/layer/tile/TileLayer.js +139 -0
  110. package/dist/esm/layer/tile/TileLayer.js.map +1 -0
  111. package/dist/esm/layer/tile/WMSTileLayer.js +68 -0
  112. package/dist/esm/layer/tile/WMSTileLayer.js.map +1 -0
  113. package/dist/esm/layer/vector/Canvas.js +292 -0
  114. package/dist/esm/layer/vector/Canvas.js.map +1 -0
  115. package/dist/esm/layer/vector/Circle.js +35 -0
  116. package/dist/esm/layer/vector/Circle.js.map +1 -0
  117. package/dist/esm/layer/vector/CircleMarker.js +69 -0
  118. package/dist/esm/layer/vector/CircleMarker.js.map +1 -0
  119. package/dist/esm/layer/vector/Path.js +68 -0
  120. package/dist/esm/layer/vector/Path.js.map +1 -0
  121. package/dist/esm/layer/vector/Polygon.js +70 -0
  122. package/dist/esm/layer/vector/Polygon.js.map +1 -0
  123. package/dist/esm/layer/vector/Polyline.js +152 -0
  124. package/dist/esm/layer/vector/Polyline.js.map +1 -0
  125. package/dist/esm/layer/vector/Rectangle.js +25 -0
  126. package/dist/esm/layer/vector/Rectangle.js.map +1 -0
  127. package/dist/esm/layer/vector/Renderer.getRenderer.js +31 -0
  128. package/dist/esm/layer/vector/Renderer.getRenderer.js.map +1 -0
  129. package/dist/esm/layer/vector/Renderer.js +39 -0
  130. package/dist/esm/layer/vector/Renderer.js.map +1 -0
  131. package/dist/esm/layer/vector/SVG.js +115 -0
  132. package/dist/esm/layer/vector/SVG.js.map +1 -0
  133. package/dist/esm/map/Map.js +844 -0
  134. package/dist/esm/map/Map.js.map +1 -0
  135. package/dist/esm/map/handler/BoxZoomHandler.js +105 -0
  136. package/dist/esm/map/handler/BoxZoomHandler.js.map +1 -0
  137. package/dist/esm/map/handler/DoubleClickZoomHandler.js +27 -0
  138. package/dist/esm/map/handler/DoubleClickZoomHandler.js.map +1 -0
  139. package/dist/esm/map/handler/DragHandler.js +133 -0
  140. package/dist/esm/map/handler/DragHandler.js.map +1 -0
  141. package/dist/esm/map/handler/KeyboardHandler.js +117 -0
  142. package/dist/esm/map/handler/KeyboardHandler.js.map +1 -0
  143. package/dist/esm/map/handler/PinchZoomHandler.js +96 -0
  144. package/dist/esm/map/handler/PinchZoomHandler.js.map +1 -0
  145. package/dist/esm/map/handler/ScrollWheelZoomHandler.js +50 -0
  146. package/dist/esm/map/handler/ScrollWheelZoomHandler.js.map +1 -0
  147. package/dist/esm/map/handler/TapHoldHandler.js +77 -0
  148. package/dist/esm/map/handler/TapHoldHandler.js.map +1 -0
  149. package/dist/esm/package.js +6 -0
  150. package/dist/esm/package.js.map +1 -0
  151. package/dist/images/layers.svg +1 -0
  152. package/dist/images/logo.svg +1 -0
  153. package/dist/images/marker-icon.svg +1 -0
  154. package/dist/images/marker-shadow.svg +1 -0
  155. package/dist/maplair-global-src.js +6810 -0
  156. package/dist/maplair-global-src.js.map +1 -0
  157. package/dist/maplair-global.js +12 -0
  158. package/dist/maplair-global.js.map +1 -0
  159. package/dist/maplair-src.js +6704 -0
  160. package/dist/maplair-src.js.map +1 -0
  161. package/dist/maplair.css +730 -0
  162. package/dist/maplair.js +7 -0
  163. package/dist/maplair.js.map +1 -0
  164. package/i18n/cs.json +12 -0
  165. package/i18n/de.json +12 -0
  166. package/i18n/en.json +12 -0
  167. package/i18n/es.json +12 -0
  168. package/i18n/fr.json +12 -0
  169. package/i18n/hu.json +12 -0
  170. package/i18n/it.json +12 -0
  171. package/i18n/rm.json +12 -0
  172. package/i18n/sk.json +12 -0
  173. package/i18n/sl.json +12 -0
  174. package/package.json +87 -0
  175. package/src/Maplair.js +24 -0
  176. package/src/control/AttributionControl.js +147 -0
  177. package/src/control/Control.js +174 -0
  178. package/src/control/LayersControl.js +444 -0
  179. package/src/control/ScaleControl.js +141 -0
  180. package/src/control/ZoomControl.js +147 -0
  181. package/src/control/index.js +5 -0
  182. package/src/core/Browser.js +75 -0
  183. package/src/core/Class.js +97 -0
  184. package/src/core/Events.js +334 -0
  185. package/src/core/Handler.js +59 -0
  186. package/src/core/I18n.js +76 -0
  187. package/src/core/Util.js +122 -0
  188. package/src/core/index.js +8 -0
  189. package/src/dom/DomEvent.DoubleTap.js +120 -0
  190. package/src/dom/DomEvent.PointerEvents.js +75 -0
  191. package/src/dom/DomEvent.js +282 -0
  192. package/src/dom/DomUtil.js +175 -0
  193. package/src/dom/Draggable.js +204 -0
  194. package/src/dom/PosAnimation.js +115 -0
  195. package/src/dom/index.js +9 -0
  196. package/src/geo/LatLng.js +163 -0
  197. package/src/geo/LatLngBounds.js +260 -0
  198. package/src/geo/crs/CRS.js +143 -0
  199. package/src/geo/crs/EPSG3395.js +19 -0
  200. package/src/geo/crs/EPSG3857.js +26 -0
  201. package/src/geo/crs/EPSG4326.js +21 -0
  202. package/src/geo/crs/EarthCRS.js +32 -0
  203. package/src/geo/crs/SimpleCRS.js +35 -0
  204. package/src/geo/crs/index.js +6 -0
  205. package/src/geo/index.js +7 -0
  206. package/src/geo/projection/Projection.LonLat.js +30 -0
  207. package/src/geo/projection/Projection.Mercator.js +53 -0
  208. package/src/geo/projection/Projection.SphericalMercator.js +46 -0
  209. package/src/geo/projection/index.js +26 -0
  210. package/src/geometry/Bounds.js +222 -0
  211. package/src/geometry/LineUtil.js +299 -0
  212. package/src/geometry/Point.js +239 -0
  213. package/src/geometry/PolyUtil.js +129 -0
  214. package/src/geometry/Transformation.js +67 -0
  215. package/src/geometry/index.js +8 -0
  216. package/src/images/layers.svg +1 -0
  217. package/src/images/logo.svg +1 -0
  218. package/src/images/marker-icon.svg +1 -0
  219. package/src/images/marker-shadow.svg +1 -0
  220. package/src/layer/BlanketOverlay.js +171 -0
  221. package/src/layer/DivOverlay.js +353 -0
  222. package/src/layer/FeatureGroup.js +93 -0
  223. package/src/layer/GeoJSON.js +432 -0
  224. package/src/layer/ImageOverlay.js +279 -0
  225. package/src/layer/Layer.js +273 -0
  226. package/src/layer/LayerGroup.js +129 -0
  227. package/src/layer/Popup.js +528 -0
  228. package/src/layer/SVGOverlay.js +47 -0
  229. package/src/layer/Tooltip.js +469 -0
  230. package/src/layer/VideoOverlay.js +111 -0
  231. package/src/layer/index.js +18 -0
  232. package/src/layer/marker/DefaultIcon.js +79 -0
  233. package/src/layer/marker/DivIcon.js +76 -0
  234. package/src/layer/marker/Icon.js +164 -0
  235. package/src/layer/marker/Marker.Drag.js +161 -0
  236. package/src/layer/marker/Marker.js +465 -0
  237. package/src/layer/marker/index.js +5 -0
  238. package/src/layer/tile/GridLayer.js +901 -0
  239. package/src/layer/tile/TileLayer.js +299 -0
  240. package/src/layer/tile/WMSTileLayer.js +133 -0
  241. package/src/layer/tile/index.js +3 -0
  242. package/src/layer/vector/Canvas.js +479 -0
  243. package/src/layer/vector/Circle.js +87 -0
  244. package/src/layer/vector/CircleMarker.js +110 -0
  245. package/src/layer/vector/Path.js +145 -0
  246. package/src/layer/vector/Polygon.js +160 -0
  247. package/src/layer/vector/Polyline.js +296 -0
  248. package/src/layer/vector/Rectangle.js +52 -0
  249. package/src/layer/vector/Renderer.getRenderer.js +48 -0
  250. package/src/layer/vector/Renderer.js +75 -0
  251. package/src/layer/vector/SVG.js +216 -0
  252. package/src/layer/vector/index.js +10 -0
  253. package/src/map/Map.js +1830 -0
  254. package/src/map/handler/BoxZoomHandler.js +156 -0
  255. package/src/map/handler/DoubleClickZoomHandler.js +62 -0
  256. package/src/map/handler/DragHandler.js +240 -0
  257. package/src/map/handler/KeyboardHandler.js +188 -0
  258. package/src/map/handler/PinchZoomHandler.js +157 -0
  259. package/src/map/handler/ScrollWheelZoomHandler.js +98 -0
  260. package/src/map/handler/TapHoldHandler.js +111 -0
  261. package/src/map/index.js +8 -0
  262. package/src/maplair.css +730 -0
package/src/map/Map.js ADDED
@@ -0,0 +1,1830 @@
1
+ import * as Util from '../core/Util.js';
2
+ import {Evented} from '../core/Events.js';
3
+ import {EPSG3857} from '../geo/crs/EPSG3857.js';
4
+ import {Point} from '../geometry/Point.js';
5
+ import {Bounds} from '../geometry/Bounds.js';
6
+ import {LatLng} from '../geo/LatLng.js';
7
+ import {LatLngBounds} from '../geo/LatLngBounds.js';
8
+ import Browser from '../core/Browser.js';
9
+ import * as DomEvent from '../dom/DomEvent.js';
10
+ import * as DomUtil from '../dom/DomUtil.js';
11
+ import {PosAnimation} from '../dom/PosAnimation.js';
12
+ import * as PointerEvents from '../dom/DomEvent.PointerEvents.js';
13
+ import {Control} from '../control/Control.js';
14
+ import {AttributionControl} from '../control/AttributionControl.js';
15
+ import {ZoomControl} from '../control/ZoomControl.js';
16
+ import {Layer} from '../layer/Layer.js';
17
+ import {DivOverlay} from '../layer/DivOverlay.js';
18
+ import {Popup} from '../layer/Popup.js';
19
+ import {Tooltip} from '../layer/Tooltip.js';
20
+ import * as RendererSupport from '../layer/vector/Renderer.getRenderer.js';
21
+ import {BoxZoomHandler} from './handler/BoxZoomHandler.js';
22
+ import {DoubleClickZoomHandler} from './handler/DoubleClickZoomHandler.js';
23
+ import {DragHandler} from './handler/DragHandler.js';
24
+ import {KeyboardHandler} from './handler/KeyboardHandler.js';
25
+ import {PinchZoomHandler} from './handler/PinchZoomHandler.js';
26
+ import {ScrollWheelZoomHandler} from './handler/ScrollWheelZoomHandler.js';
27
+ import {TapHoldHandler} from './handler/TapHoldHandler.js';
28
+
29
+ /*
30
+ * @class MaplairMap
31
+ * @inherits Evented
32
+ *
33
+ * The central class of the API — it is used to create a map on a page and manipulate it.
34
+ *
35
+ * @example
36
+ *
37
+ * ```js
38
+ * // initialize the map on the "map" div with a given center and zoom
39
+ * const map = new MaplairMap('map', {
40
+ * center: [51.505, -0.09],
41
+ * zoom: 13
42
+ * });
43
+ * ```
44
+ *
45
+ */
46
+
47
+ // @section
48
+ // @constructor MaplairMap(id: String, options?: MaplairMap options)
49
+ // Instantiates a map object given the DOM ID of a `<div>` element
50
+ // and optionally an object literal with `MaplairMap options`.
51
+ //
52
+ // @alternative
53
+ // @constructor MaplairMap(el: HTMLElement, options?: MaplairMap options)
54
+ // Instantiates a map object given an instance of a `<div>` HTML element
55
+ // and optionally an object literal with `MaplairMap options`.
56
+ //
57
+ // @alternative
58
+ // @constructor Map(id: String, options?: Map options)
59
+ // Instantiates a map object given the DOM ID of a `<div>` element
60
+ // and optionally an object literal with `Map options`.
61
+ //
62
+ // @alternative
63
+ // @constructor Map(el: HTMLElement, options?: Map options)
64
+ // Instantiates a map object given an instance of a `<div>` HTML element
65
+ // and optionally an object literal with `Map options`.
66
+ export class MaplairMap extends Evented {
67
+
68
+ static {
69
+ this.setDefaultOptions({
70
+ // @section Map State Options
71
+ // @option crs: CRS = EPSG3857
72
+ // The [Coordinate Reference System](#crs) to use. Don't change this if you're not
73
+ // sure what it means.
74
+ crs: EPSG3857,
75
+
76
+ // @option center: LatLng = undefined
77
+ // Initial geographic center of the map
78
+ center: undefined,
79
+
80
+ // @option zoom: Number = undefined
81
+ // Initial map zoom level
82
+ zoom: undefined,
83
+
84
+ // @option minZoom: Number = *
85
+ // Minimum zoom level of the map.
86
+ // If not specified and at least one `GridLayer` or `TileLayer` is in the map,
87
+ // the lowest of their `minZoom` options will be used instead.
88
+ minZoom: undefined,
89
+
90
+ // @option maxZoom: Number = *
91
+ // Maximum zoom level of the map.
92
+ // If not specified and at least one `GridLayer` or `TileLayer` is in the map,
93
+ // the highest of their `maxZoom` options will be used instead.
94
+ maxZoom: undefined,
95
+
96
+ // @option layers: Layer[] = []
97
+ // Array of layers that will be added to the map initially
98
+ layers: [],
99
+
100
+ // @option maxBounds: LatLngBounds = null
101
+ // When this option is set, the map restricts the view to the given
102
+ // geographical bounds, bouncing the user back if the user tries to pan
103
+ // outside the view. To set the restriction dynamically, use
104
+ // [`setMaxBounds`](#map-setmaxbounds) method.
105
+ maxBounds: undefined,
106
+
107
+ // @option renderer: Renderer = *
108
+ // The default method for drawing vector layers on the map. `SVG`
109
+ // or `Canvas` by default depending on browser support.
110
+ renderer: undefined,
111
+
112
+
113
+ // @section Animation Options
114
+ // @option zoomAnimation: Boolean = *
115
+ // Whether the map zoom animation is enabled.
116
+ // Animations are enabled by default unless the user has configured
117
+ // their browser for reduced [motion](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@media/prefers-reduced-motion#user_preferences).
118
+ zoomAnimation: !Browser.reducedMotion,
119
+
120
+ // @option zoomAnimationThreshold: Number = 4
121
+ // Won't animate zoom if the zoom difference exceeds this value.
122
+ zoomAnimationThreshold: 4,
123
+
124
+ // @option fadeAnimation: Boolean = *
125
+ // Whether the tile fade animation is enabled.
126
+ // Animations are enabled by default unless the user has configured
127
+ // their browser for reduced [motion](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@media/prefers-reduced-motion#user_preferences).
128
+ fadeAnimation: !Browser.reducedMotion,
129
+
130
+ // @option markerZoomAnimation: Boolean = true
131
+ // Whether markers animate their zoom with the zoom animation, if disabled
132
+ // they will disappear for the length of the animation.
133
+ markerZoomAnimation: true,
134
+
135
+ // @option markerCulling: Boolean = false
136
+ // Whether markers far outside of the map view (more than half a view
137
+ // away) are taken off the page until the map is moved closer to them.
138
+ // This reduces the browser's rendering work, and the work of
139
+ // repositioning markers while zooming, when a map has many markers
140
+ // (e.g. several thousands) of which only some are visible at a time.
141
+ markerCulling: false,
142
+
143
+ // @option transform3DLimit: Number = 2^23
144
+ // Defines the maximum size of a CSS translation transform. The default
145
+ // value should not be changed unless a web browser positions layers in
146
+ // the wrong place after doing a large `panBy`.
147
+ transform3DLimit: 8388608, // Precision limit of a 32-bit float
148
+
149
+ // @section Interaction Options
150
+ // @option zoomSnap: Number = 1
151
+ // Forces the map's zoom level to always be a multiple of this, particularly
152
+ // right after a [`fitBounds()`](#map-fitbounds) or a pinch-zoom.
153
+ // By default, the zoom level snaps to the nearest integer; lower values
154
+ // (e.g. `0.5` or `0.1`) allow for greater granularity. A value of `0`
155
+ // means the zoom level will not be snapped after `fitBounds` or a pinch-zoom.
156
+ zoomSnap: 1,
157
+
158
+ // @option zoomDelta: Number = 1
159
+ // Controls how much the map's zoom level will change after a
160
+ // [`zoomIn()`](#map-zoomin), [`zoomOut()`](#map-zoomout), pressing `+`
161
+ // or `-` on the keyboard, or using the [zoom controls](#control-zoom).
162
+ // Values smaller than `1` (e.g. `0.5`) allow for greater granularity.
163
+ zoomDelta: 1,
164
+
165
+ // @option trackResize: Boolean = true
166
+ // Whether the map automatically handles browser window resize to update itself.
167
+ trackResize: true
168
+ });
169
+ }
170
+
171
+ constructor(id, options) { // (HTMLElement or String, Object)
172
+ super();
173
+ options = Util.setOptions(this, options);
174
+
175
+ // Make sure to assign internal flags at the beginning,
176
+ // to avoid inconsistent state in some edge cases.
177
+ this._handlers = [];
178
+ this._layers = {};
179
+ this._zoomBoundLayers = {};
180
+ this._sizeChanged = true;
181
+
182
+ this._initContainer(id);
183
+ this._initLayout();
184
+
185
+ this._initEvents();
186
+
187
+ if (options.maxBounds) {
188
+ this.setMaxBounds(options.maxBounds);
189
+ }
190
+
191
+ if (options.zoom !== undefined) {
192
+ this._zoom = this._limitZoom(options.zoom);
193
+ }
194
+
195
+ if (options.center && options.zoom !== undefined) {
196
+ this.setView(new LatLng(options.center), options.zoom, {reset: true});
197
+ }
198
+
199
+ this.callInitHooks(MaplairMap);
200
+
201
+ this._zoomAnimated = this.options.zoomAnimation;
202
+
203
+ // zoom transitions run with the same duration for all layers, so if one of transitionend events
204
+ // happens after starting zoom animation (propagating to the map pane), we know that it ended globally
205
+ if (this._zoomAnimated) {
206
+ this._createAnimProxy();
207
+ }
208
+
209
+ this._addLayers(this.options.layers);
210
+ }
211
+
212
+ // @section Methods for modifying map state
213
+
214
+ // @method setView(center: LatLng, zoom?: Number, options?: Zoom/pan options): this
215
+ // Sets the view of the map (geographical center and zoom) with the given
216
+ // animation options.
217
+ setView(center, zoom, options) {
218
+
219
+ zoom = zoom === undefined ? this._zoom : this._limitZoom(zoom);
220
+ center = this._limitCenter(new LatLng(center), zoom, this.options.maxBounds);
221
+ options ??= {};
222
+
223
+ this._stop();
224
+
225
+ if (this._loaded && !options.reset && options !== true) {
226
+
227
+ if (options.animate !== undefined) {
228
+ options.zoom = {animate: options.animate, ...options.zoom};
229
+ options.pan = {animate: options.animate, duration: options.duration, ...options.pan};
230
+ }
231
+
232
+ // try animating pan or zoom
233
+ const moved = (this._zoom !== zoom) ?
234
+ this._tryAnimatedZoom && this._tryAnimatedZoom(center, zoom, options.zoom) :
235
+ this._tryAnimatedPan(center, options.pan);
236
+
237
+ if (moved) {
238
+ // prevent resize handler call, the view will refresh after animation anyway
239
+ clearTimeout(this._sizeTimer);
240
+ return this;
241
+ }
242
+ }
243
+
244
+ // animation didn't start, just reset the map view
245
+ this._resetView(center, zoom, options.pan?.noMoveStart);
246
+
247
+ return this;
248
+ }
249
+
250
+ // @method setZoom(zoom: Number, options?: Zoom/pan options): this
251
+ // Sets the zoom of the map.
252
+ setZoom(zoom, options) {
253
+ if (!this._loaded) {
254
+ this._zoom = zoom;
255
+ return this;
256
+ }
257
+ return this.setView(this.getCenter(), zoom, {zoom: options});
258
+ }
259
+
260
+ // @method zoomIn(delta?: Number, options?: Zoom options): this
261
+ // Increases the zoom of the map by `delta` ([`zoomDelta`](#map-zoomdelta) by default).
262
+ zoomIn(delta, options) {
263
+ delta ??= this.options.zoomDelta;
264
+ return this.setZoom(this._zoom + delta, options);
265
+ }
266
+
267
+ // @method zoomOut(delta?: Number, options?: Zoom options): this
268
+ // Decreases the zoom of the map by `delta` ([`zoomDelta`](#map-zoomdelta) by default).
269
+ zoomOut(delta, options) {
270
+ delta ??= this.options.zoomDelta;
271
+ return this.setZoom(this._zoom - delta, options);
272
+ }
273
+
274
+ // @method setZoomAround(latlng: LatLng, zoom: Number, options: Zoom options): this
275
+ // Zooms the map while keeping a specified geographical point on the map
276
+ // stationary (e.g. used internally for scroll zoom and double-click zoom).
277
+ // @alternative
278
+ // @method setZoomAround(offset: Point, zoom: Number, options: Zoom options): this
279
+ // Zooms the map while keeping a specified pixel on the map (relative to the top-left corner) stationary.
280
+ setZoomAround(latlng, zoom, options) {
281
+ const scale = this.getZoomScale(zoom),
282
+ viewHalf = this.getSize().divideBy(2),
283
+ containerPoint = latlng instanceof Point ? latlng : this.latLngToContainerPoint(latlng),
284
+
285
+ centerOffset = containerPoint.subtract(viewHalf).multiplyBy(1 - 1 / scale),
286
+ newCenter = this.containerPointToLatLng(viewHalf.add(centerOffset));
287
+
288
+ return this.setView(newCenter, zoom, {zoom: options});
289
+ }
290
+
291
+ _getBoundsCenterZoom(bounds, options) {
292
+
293
+ options ??= {};
294
+ bounds = bounds.getBounds ? bounds.getBounds() : new LatLngBounds(bounds);
295
+
296
+ const paddingTL = new Point(options.paddingTopLeft || options.padding || [0, 0]),
297
+ paddingBR = new Point(options.paddingBottomRight || options.padding || [0, 0]);
298
+
299
+ let zoom = this.getBoundsZoom(bounds, false, paddingTL.add(paddingBR));
300
+
301
+ zoom = (typeof options.maxZoom === 'number') ? Math.min(options.maxZoom, zoom) : zoom;
302
+
303
+ if (zoom === Infinity) {
304
+ return {
305
+ center: bounds.getCenter(),
306
+ zoom
307
+ };
308
+ }
309
+
310
+ const paddingOffset = paddingBR.subtract(paddingTL).divideBy(2),
311
+
312
+ swPoint = this.project(bounds.getSouthWest(), zoom),
313
+ nePoint = this.project(bounds.getNorthEast(), zoom),
314
+ center = this.unproject(swPoint.add(nePoint).divideBy(2).add(paddingOffset), zoom);
315
+
316
+ return {
317
+ center,
318
+ zoom
319
+ };
320
+ }
321
+
322
+ // @method fitBounds(bounds: LatLngBounds, options?: fitBounds options): this
323
+ // Sets a map view that contains the given geographical bounds with the
324
+ // maximum zoom level possible.
325
+ fitBounds(bounds, options) {
326
+
327
+ bounds = new LatLngBounds(bounds);
328
+
329
+ if (!bounds.isValid()) {
330
+ throw new Error('Bounds are not valid.');
331
+ }
332
+
333
+ const target = this._getBoundsCenterZoom(bounds, options);
334
+ return this.setView(target.center, target.zoom, options);
335
+ }
336
+
337
+ // @method fitWorld(options?: fitBounds options): this
338
+ // Sets a map view that mostly contains the whole world with the maximum
339
+ // zoom level possible.
340
+ fitWorld(options) {
341
+ return this.fitBounds([[-90, -180], [90, 180]], options);
342
+ }
343
+
344
+ // @method panTo(latlng: LatLng, options?: Pan options): this
345
+ // Pans the map to a given center.
346
+ panTo(center, options) {
347
+ return this.setView(center, this._zoom, {pan: options});
348
+ }
349
+
350
+ // @method panBy(offset: Point, options?: Pan options): this
351
+ // Pans the map by a given number of pixels (animated).
352
+ panBy(offset, options) {
353
+ offset = new Point(offset).round();
354
+ options ??= {};
355
+
356
+ if (!offset.x && !offset.y) {
357
+ return this.fire('moveend');
358
+ }
359
+ // If we pan too far, Chrome gets issues with tiles
360
+ // and makes them disappear or appear in the wrong place (slightly offset) #2602
361
+ if (options.animate !== true && !this.getSize().contains(offset)) {
362
+ this._resetView(this.unproject(this.project(this.getCenter()).add(offset)), this.getZoom());
363
+ return this;
364
+ }
365
+
366
+ if (!this._panAnim) {
367
+ this._panAnim = new PosAnimation();
368
+
369
+ this._panAnim.on({
370
+ 'step': this._onPanTransitionStep,
371
+ 'end': this._onPanTransitionEnd
372
+ }, this);
373
+ }
374
+
375
+ // don't fire movestart if animating inertia
376
+ if (!options.noMoveStart) {
377
+ this.fire('movestart');
378
+ }
379
+
380
+ // animate pan unless browser prefers reduced motion or animate: false specified
381
+ if (!Browser.reducedMotion && options.animate !== false) {
382
+ this._mapPane.classList.add('maplair-pan-anim');
383
+
384
+ const newPos = this._getMapPanePos().subtract(offset).round();
385
+ this._panAnim.run(this._mapPane, newPos, options.duration || 0.25, options.easeLinearity);
386
+ } else {
387
+ this._rawPanBy(offset);
388
+ this.fire('move').fire('moveend');
389
+ }
390
+
391
+ return this;
392
+ }
393
+
394
+ // @method flyTo(latlng: LatLng, zoom?: Number, options?: Zoom/pan options): this
395
+ // Sets the view of the map (geographical center and zoom) performing a smooth
396
+ // pan-zoom animation.
397
+ flyTo(targetCenter, targetZoom, options) {
398
+
399
+ options ??= {};
400
+ if (Browser.reducedMotion || options.animate === false) {
401
+ return this.setView(targetCenter, targetZoom, options);
402
+ }
403
+
404
+ this._stop();
405
+
406
+ const from = this.project(this.getCenter()),
407
+ to = this.project(targetCenter),
408
+ size = this.getSize(),
409
+ startZoom = this._zoom;
410
+
411
+ targetCenter = new LatLng(targetCenter);
412
+ targetZoom = targetZoom === undefined ? startZoom : this._limitZoom(targetZoom);
413
+
414
+ const w0 = Math.max(size.x, size.y),
415
+ w1 = w0 * this.getZoomScale(startZoom, targetZoom),
416
+ u1 = (to.distanceTo(from)) || 1,
417
+ rho = 1.42,
418
+ rho2 = rho * rho;
419
+
420
+ function r(i) {
421
+ const s1 = i ? -1 : 1,
422
+ s2 = i ? w1 : w0,
423
+ t1 = w1 * w1 - w0 * w0 + s1 * rho2 * rho2 * u1 * u1,
424
+ b1 = 2 * s2 * rho2 * u1,
425
+ b = t1 / b1,
426
+ sq = Math.sqrt(b * b + 1) - b;
427
+
428
+ // workaround for floating point precision bug when sq = 0, log = -Infinite,
429
+ // thus triggering an infinite loop in flyTo
430
+ const log = sq < 0.000000015 ? -18 : Math.log(sq);
431
+
432
+ return log;
433
+ }
434
+
435
+ function sinh(n) { return (Math.exp(n) - Math.exp(-n)) / 2; }
436
+ function cosh(n) { return (Math.exp(n) + Math.exp(-n)) / 2; }
437
+ function tanh(n) { return sinh(n) / cosh(n); }
438
+
439
+ const r0 = r(0);
440
+
441
+ function w(s) { return w0 * (cosh(r0) / cosh(r0 + rho * s)); }
442
+ function u(s) { return w0 * (cosh(r0) * tanh(r0 + rho * s) - sinh(r0)) / rho2; }
443
+
444
+ function easeOut(t) { return 1 - (1 - t) ** 1.5; }
445
+
446
+ const start = Date.now(),
447
+ S = (r(1) - r0) / rho,
448
+ duration = options.duration ? 1000 * options.duration : 1000 * S * 0.8;
449
+
450
+ const frame = () => {
451
+ const t = (Date.now() - start) / duration,
452
+ s = easeOut(t) * S;
453
+
454
+ if (t <= 1) {
455
+ this._flyToFrame = requestAnimationFrame(frame);
456
+
457
+ this._move(
458
+ this.unproject(from.add(to.subtract(from).multiplyBy(u(s) / u1)), startZoom),
459
+ this.getScaleZoom(w0 / w(s), startZoom),
460
+ {flyTo: true});
461
+
462
+ } else {
463
+ this
464
+ ._move(targetCenter, targetZoom)
465
+ ._moveEnd(true);
466
+ }
467
+ };
468
+
469
+ this._moveStart(true, options.noMoveStart);
470
+
471
+ frame(this);
472
+ return this;
473
+ }
474
+
475
+ // @method flyToBounds(bounds: LatLngBounds, options?: fitBounds options): this
476
+ // Sets the view of the map with a smooth animation like [`flyTo`](#map-flyto),
477
+ // but takes a bounds parameter like [`fitBounds`](#map-fitbounds).
478
+ flyToBounds(bounds, options) {
479
+ const target = this._getBoundsCenterZoom(bounds, options);
480
+ return this.flyTo(target.center, target.zoom, options);
481
+ }
482
+
483
+ // @method setMaxBounds(bounds: LatLngBounds): this
484
+ // Restricts the map view to the given bounds (see the [maxBounds](#map-maxbounds) option).
485
+ setMaxBounds(bounds) {
486
+ bounds = new LatLngBounds(bounds);
487
+
488
+ if (this.listens('moveend', this._panInsideMaxBounds)) {
489
+ this.off('moveend', this._panInsideMaxBounds);
490
+ }
491
+
492
+ if (!bounds.isValid()) {
493
+ this.options.maxBounds = null;
494
+ return this;
495
+ }
496
+
497
+ this.options.maxBounds = bounds;
498
+
499
+ if (this._loaded) {
500
+ this._panInsideMaxBounds();
501
+ }
502
+
503
+ return this.on('moveend', this._panInsideMaxBounds);
504
+ }
505
+
506
+ // @method setMinZoom(zoom: Number): this
507
+ // Sets the lower limit for the available zoom levels (see the [minZoom](#map-minzoom) option).
508
+ setMinZoom(zoom) {
509
+ const oldZoom = this.options.minZoom;
510
+ this.options.minZoom = zoom;
511
+
512
+ if (this._loaded && oldZoom !== zoom) {
513
+ this.fire('zoomlevelschange');
514
+
515
+ if (this.getZoom() < this.options.minZoom) {
516
+ return this.setZoom(zoom);
517
+ }
518
+ }
519
+
520
+ return this;
521
+ }
522
+
523
+ // @method setMaxZoom(zoom: Number): this
524
+ // Sets the upper limit for the available zoom levels (see the [maxZoom](#map-maxzoom) option).
525
+ setMaxZoom(zoom) {
526
+ const oldZoom = this.options.maxZoom;
527
+ this.options.maxZoom = zoom;
528
+
529
+ if (this._loaded && oldZoom !== zoom) {
530
+ this.fire('zoomlevelschange');
531
+
532
+ if (this.getZoom() > this.options.maxZoom) {
533
+ return this.setZoom(zoom);
534
+ }
535
+ }
536
+
537
+ return this;
538
+ }
539
+
540
+ // @method panInsideBounds(bounds: LatLngBounds, options?: Pan options): this
541
+ // Pans the map to the closest view that would lie inside the given bounds (if it's not already), controlling the animation using the options specific, if any.
542
+ panInsideBounds(bounds, options) {
543
+ this._enforcingBounds = true;
544
+ const center = this.getCenter(),
545
+ newCenter = this._limitCenter(center, this._zoom, new LatLngBounds(bounds));
546
+
547
+ if (!center.equals(newCenter)) {
548
+ this.panTo(newCenter, options);
549
+ }
550
+
551
+ this._enforcingBounds = false;
552
+ return this;
553
+ }
554
+
555
+ // @method panInside(latlng: LatLng, options?: padding options): this
556
+ // Pans the map the minimum amount to make the `latlng` visible. Use
557
+ // padding options to fit the display to more restricted bounds.
558
+ // If `latlng` is already within the (optionally padded) display bounds,
559
+ // the map will not be panned.
560
+ panInside(latlng, options) {
561
+ options ??= {};
562
+
563
+ const paddingTL = new Point(options.paddingTopLeft || options.padding || [0, 0]),
564
+ paddingBR = new Point(options.paddingBottomRight || options.padding || [0, 0]),
565
+ pixelCenter = this.project(this.getCenter()),
566
+ pixelPoint = this.project(latlng),
567
+ pixelBounds = this.getPixelBounds(),
568
+ paddedBounds = new Bounds([pixelBounds.min.add(paddingTL), pixelBounds.max.subtract(paddingBR)]),
569
+ paddedSize = paddedBounds.getSize();
570
+
571
+ if (!paddedBounds.contains(pixelPoint)) {
572
+ this._enforcingBounds = true;
573
+ const centerOffset = pixelPoint.subtract(paddedBounds.getCenter());
574
+ const offset = paddedBounds.extend(pixelPoint).getSize().subtract(paddedSize);
575
+ pixelCenter.x += centerOffset.x < 0 ? -offset.x : offset.x;
576
+ pixelCenter.y += centerOffset.y < 0 ? -offset.y : offset.y;
577
+ this.panTo(this.unproject(pixelCenter), options);
578
+ this._enforcingBounds = false;
579
+ }
580
+ return this;
581
+ }
582
+
583
+ // @method invalidateSize(options: invalidateSize options): this
584
+ // Checks if the map container size changed and updates the map if so —
585
+ // call it after you've changed the map size dynamically, also animating
586
+ // pan by default. If `options.pan` is `false`, panning will not occur.
587
+ // If `options.debounceMoveend` is `true`, it will delay `moveend` event so
588
+ // that it doesn't happen often even if the method is called many
589
+ // times in a row.
590
+
591
+ // @alternative
592
+ // @method invalidateSize(animate: Boolean): this
593
+ // Checks if the map container size changed and updates the map if so —
594
+ // call it after you've changed the map size dynamically, also animating
595
+ // pan by default.
596
+ invalidateSize(options) {
597
+ if (!this._loaded) { return this; }
598
+
599
+ options = {
600
+ animate: false,
601
+ pan: true,
602
+ ...(options === true ? {animate: true} : options)
603
+ };
604
+
605
+ const oldSize = this.getSize();
606
+ this._sizeChanged = true;
607
+ this._lastCenter = null;
608
+
609
+ const newSize = this.getSize(),
610
+ oldCenter = oldSize.divideBy(2).round(),
611
+ newCenter = newSize.divideBy(2).round(),
612
+ offset = oldCenter.subtract(newCenter);
613
+
614
+ if (!offset.x && !offset.y) { return this; }
615
+
616
+ if (options.animate && options.pan) {
617
+ this.panBy(offset);
618
+
619
+ } else {
620
+ if (options.pan) {
621
+ this._rawPanBy(offset);
622
+ }
623
+
624
+ this.fire('move');
625
+
626
+ if (options.debounceMoveend) {
627
+ clearTimeout(this._sizeTimer);
628
+ this._sizeTimer = setTimeout(this.fire.bind(this, 'moveend'), 200);
629
+ } else {
630
+ this.fire('moveend');
631
+ }
632
+ }
633
+
634
+ // @section Map state change events
635
+ // @event resize: ResizeEvent
636
+ // Fired when the map is resized.
637
+ return this.fire('resize', {
638
+ oldSize,
639
+ newSize
640
+ });
641
+ }
642
+
643
+ // @section Methods for modifying map state
644
+ // @method stop(): this
645
+ // Stops the currently running `panTo` or `flyTo` animation, if any.
646
+ stop() {
647
+ this.setZoom(this._limitZoom(this._zoom));
648
+ if (!this.options.zoomSnap) {
649
+ this.fire('viewreset');
650
+ }
651
+ return this._stop();
652
+ }
653
+
654
+ // @section Geolocation methods
655
+ // @method locate(options?: Locate options): this
656
+ // Tries to locate the user using the Geolocation API, firing a [`locationfound`](#map-locationfound)
657
+ // event with location data on success or a [`locationerror`](#map-locationerror) event on failure,
658
+ // and optionally sets the map view to the user's location with respect to
659
+ // detection accuracy (or to the world view if geolocation failed).
660
+ // Note that, if your page doesn't use HTTPS, this method will fail in
661
+ // modern browsers ([Chrome 50 and newer](https://sites.google.com/a/chromium.org/dev/Home/chromium-security/deprecating-powerful-features-on-insecure-origins))
662
+ // See `Locate options` for more details.
663
+ locate(options) {
664
+
665
+ options = this._locateOptions = {
666
+ timeout: 10000,
667
+ watch: false,
668
+ // setView: false
669
+ // maxZoom: <Number>
670
+ // maximumAge: 0
671
+ // enableHighAccuracy: false
672
+ ...options
673
+ };
674
+
675
+ if (!('geolocation' in navigator)) {
676
+ this._handleGeolocationError({
677
+ code: 0,
678
+ message: 'Geolocation not supported.'
679
+ });
680
+ return this;
681
+ }
682
+
683
+ const onResponse = this._handleGeolocationResponse.bind(this),
684
+ onError = this._handleGeolocationError.bind(this);
685
+
686
+ if (options.watch) {
687
+ if (this._locationWatchId !== undefined) {
688
+ navigator.geolocation.clearWatch(this._locationWatchId);
689
+ }
690
+ this._locationWatchId = navigator.geolocation.watchPosition(onResponse, onError, options);
691
+ } else {
692
+ navigator.geolocation.getCurrentPosition(onResponse, onError, options);
693
+ }
694
+ return this;
695
+ }
696
+
697
+ // @method stopLocate(): this
698
+ // Stops watching location previously initiated by `map.locate({watch: true})`
699
+ // and aborts resetting the map view if map.locate was called with
700
+ // `{setView: true}`.
701
+ stopLocate() {
702
+ navigator.geolocation?.clearWatch?.(this._locationWatchId);
703
+ if (this._locateOptions) {
704
+ this._locateOptions.setView = false;
705
+ }
706
+ return this;
707
+ }
708
+
709
+ _handleGeolocationError(error) {
710
+ if (!this._container._maplair_id) { return; }
711
+
712
+ const c = error.code,
713
+ message = error.message ||
714
+ (c === 1 ? 'permission denied' :
715
+ (c === 2 ? 'position unavailable' : 'timeout'));
716
+
717
+ if (this._locateOptions.setView && !this._loaded) {
718
+ this.fitWorld();
719
+ }
720
+
721
+ // @section Location events
722
+ // @event locationerror: ErrorEvent
723
+ // Fired when geolocation (using the [`locate`](#map-locate) method) failed.
724
+ this.fire('locationerror', {
725
+ code: c,
726
+ message: `Geolocation error: ${message}.`
727
+ });
728
+ }
729
+
730
+ _handleGeolocationResponse(pos) {
731
+ if (!this._container._maplair_id) { return; }
732
+
733
+ const lat = pos.coords.latitude,
734
+ lng = pos.coords.longitude,
735
+ latlng = new LatLng(lat, lng),
736
+ bounds = latlng.toBounds(pos.coords.accuracy * 2),
737
+ options = this._locateOptions;
738
+
739
+ if (options.setView) {
740
+ const zoom = this.getBoundsZoom(bounds);
741
+ this.setView(latlng, options.maxZoom ? Math.min(zoom, options.maxZoom) : zoom);
742
+ }
743
+
744
+ const data = {
745
+ latlng,
746
+ bounds,
747
+ timestamp: pos.timestamp
748
+ };
749
+
750
+ for (const i in pos.coords) { // do not use Object.keys here to access getters of GeolocationCoordinates
751
+ if (typeof pos.coords[i] === 'number') {
752
+ data[i] = pos.coords[i];
753
+ }
754
+ }
755
+
756
+ // @event locationfound: LocationEvent
757
+ // Fired when geolocation (using the [`locate`](#map-locate) method)
758
+ // went successfully.
759
+ this.fire('locationfound', data);
760
+ }
761
+
762
+ // TODO Appropriate docs section?
763
+ // @section Other Methods
764
+ // @method addHandler(name: String, HandlerClass: Function): this
765
+ // Adds a new `Handler` to the map, given its name and constructor function.
766
+ addHandler(name, HandlerClass) {
767
+ if (!HandlerClass) { return this; }
768
+
769
+ const handler = this[name] = new HandlerClass(this);
770
+
771
+ this._handlers.push(handler);
772
+
773
+ if (this.options[name]) {
774
+ handler.enable();
775
+ }
776
+
777
+ return this;
778
+ }
779
+
780
+ // @method remove(): this
781
+ // Destroys the map and clears all related event listeners.
782
+ remove() {
783
+
784
+ this._initEvents(true);
785
+ if (this.options.maxBounds) { this.off('moveend', this._panInsideMaxBounds); }
786
+
787
+ if (this._containerId !== this._container._maplair_id) {
788
+ throw new Error('Map container is being reused by another instance');
789
+ }
790
+
791
+ delete this._container._maplair_id;
792
+ delete this._containerId;
793
+
794
+ if (this._locationWatchId !== undefined) {
795
+ this.stopLocate();
796
+ }
797
+
798
+ this._stop();
799
+
800
+ PointerEvents.disablePointerDetection(this._container);
801
+ this._mapPane.remove();
802
+
803
+ if (this._clearControlPos) {
804
+ this._clearControlPos();
805
+ }
806
+ if (this._resizeRequest) {
807
+ cancelAnimationFrame(this._resizeRequest);
808
+ this._resizeRequest = null;
809
+ }
810
+
811
+ this._clearHandlers();
812
+
813
+ clearTimeout(this._transitionEndTimer);
814
+ clearTimeout(this._sizeTimer);
815
+
816
+ if (this._loaded) {
817
+ // @section Map state change events
818
+ // @event unload: Event
819
+ // Fired when the map is destroyed with [remove](#map-remove) method.
820
+ this.fire('unload');
821
+ }
822
+
823
+ this._destroyAnimProxy();
824
+
825
+ for (const layer of Object.values(this._layers)) {
826
+ layer.remove();
827
+ }
828
+ for (const pane of Object.values(this._panes)) {
829
+ pane.remove();
830
+ }
831
+
832
+
833
+ this._layers = {};
834
+ this._panes = {};
835
+ delete this._mapPane;
836
+ delete this._renderer;
837
+
838
+ return this;
839
+ }
840
+
841
+ // @section Other Methods
842
+ // @method createPane(name: String, container?: HTMLElement): HTMLElement
843
+ // Creates a new [map pane](#map-pane) with the given name if it doesn't exist already,
844
+ // then returns it. The pane is created as a child of `container`, or
845
+ // as a child of the main map pane if not set.
846
+ createPane(name, container) {
847
+ const className = `maplair-pane${name ? ` maplair-${name.replace('Pane', '')}-pane` : ''}`,
848
+ pane = DomUtil.create('div', className, container || this._mapPane);
849
+
850
+ if (name) {
851
+ this._panes[name] = pane;
852
+ }
853
+ return pane;
854
+ }
855
+
856
+ // @section Methods for Getting Map State
857
+
858
+ // @method getCenter(): LatLng
859
+ // Returns the geographical center of the map view
860
+ getCenter() {
861
+ this._checkIfLoaded();
862
+
863
+ if (this._lastCenter && !this._moved()) {
864
+ return new LatLng(this._lastCenter);
865
+ }
866
+ return this.layerPointToLatLng(this._getCenterLayerPoint());
867
+ }
868
+
869
+ // @method getZoom(): Number
870
+ // Returns the current zoom level of the map view
871
+ getZoom() {
872
+ return this._zoom;
873
+ }
874
+
875
+ // @method getBounds(): LatLngBounds
876
+ // Returns the geographical bounds visible in the current map view
877
+ getBounds() {
878
+ const bounds = this.getPixelBounds(),
879
+ sw = this.unproject(bounds.getBottomLeft()),
880
+ ne = this.unproject(bounds.getTopRight());
881
+
882
+ return new LatLngBounds(sw, ne);
883
+ }
884
+
885
+ // @method getMinZoom(): Number
886
+ // Returns the minimum zoom level of the map (if set in the `minZoom` option of the map or of any layers), or `0` by default.
887
+ getMinZoom() {
888
+ return this.options.minZoom ?? this._layersMinZoom ?? 0;
889
+ }
890
+
891
+ // @method getMaxZoom(): Number
892
+ // Returns the maximum zoom level of the map (if set in the `maxZoom` option of the map or of any layers).
893
+ getMaxZoom() {
894
+ return this.options.maxZoom ?? this._layersMaxZoom ?? Infinity;
895
+ }
896
+
897
+ // @method getBoundsZoom(bounds: LatLngBounds, inside?: Boolean, padding?: Point): Number
898
+ // Returns the maximum zoom level on which the given bounds fit to the map
899
+ // view in its entirety. If `inside` (optional) is set to `true`, the method
900
+ // instead returns the minimum zoom level on which the map view fits into
901
+ // the given bounds in its entirety.
902
+ getBoundsZoom(bounds, inside, padding) { // (LatLngBounds[, Boolean, Point]) -> Number
903
+ bounds = new LatLngBounds(bounds);
904
+ padding = new Point(padding ?? [0, 0]);
905
+
906
+ let zoom = this.getZoom() ?? 0;
907
+ const min = this.getMinZoom(),
908
+ max = this.getMaxZoom(),
909
+ nw = bounds.getNorthWest(),
910
+ se = bounds.getSouthEast(),
911
+ size = this.getSize().subtract(padding),
912
+ boundsSize = new Bounds(this.project(se, zoom), this.project(nw, zoom)).getSize(),
913
+ snap = this.options.zoomSnap,
914
+ scalex = size.x / boundsSize.x,
915
+ scaley = size.y / boundsSize.y,
916
+ scale = inside ? Math.max(scalex, scaley) : Math.min(scalex, scaley);
917
+
918
+ zoom = this.getScaleZoom(scale, zoom);
919
+
920
+ if (snap) {
921
+ zoom = Math.round(zoom / (snap / 100)) * (snap / 100); // don't jump if within 1% of a snap level
922
+ zoom = inside ? Math.ceil(zoom / snap) * snap : Math.floor(zoom / snap) * snap;
923
+ }
924
+
925
+ return Math.max(min, Math.min(max, zoom));
926
+ }
927
+
928
+ // @method getSize(): Point
929
+ // Returns the current size of the map container (in pixels).
930
+ getSize() {
931
+ if (!this._size || this._sizeChanged) {
932
+ this._size = new Point(
933
+ this._container.clientWidth || 0,
934
+ this._container.clientHeight || 0);
935
+
936
+ this._sizeChanged = false;
937
+ }
938
+ return new Point(this._size);
939
+ }
940
+
941
+ // @method getPixelBounds(center?: LatLng, zoom?: Number): Bounds
942
+ // Returns the bounds of the current map view in projected pixel
943
+ // coordinates (sometimes useful in layer and overlay implementations).
944
+ // If `center` and `zoom` is omitted, the map's current zoom level and center is used.
945
+ getPixelBounds(center, zoom) {
946
+ const topLeftPoint = this._getTopLeftPoint(center, zoom);
947
+ return new Bounds(topLeftPoint, topLeftPoint.add(this.getSize()));
948
+ }
949
+
950
+ // TODO: Check semantics - isn't the pixel origin the 0,0 coord relative to
951
+ // the map pane? "left point of the map layer" can be confusing, specially
952
+ // since there can be negative offsets.
953
+ // @method getPixelOrigin(): Point
954
+ // Returns the projected pixel coordinates of the top left point of
955
+ // the map layer (useful in custom layer and overlay implementations).
956
+ getPixelOrigin() {
957
+ this._checkIfLoaded();
958
+ return this._pixelOrigin;
959
+ }
960
+
961
+ // @method getPixelWorldBounds(zoom?: Number): Bounds
962
+ // Returns the world's bounds in pixel coordinates for zoom level `zoom`.
963
+ // If `zoom` is omitted, the map's current zoom level is used.
964
+ getPixelWorldBounds(zoom) {
965
+ return this.options.crs.getProjectedBounds(zoom ?? this.getZoom());
966
+ }
967
+
968
+ // @section Other Methods
969
+
970
+ // @method getPane(pane: String|HTMLElement): HTMLElement
971
+ // Returns a [map pane](#map-pane), given its name or its HTML element (its identity).
972
+ getPane(pane) {
973
+ return typeof pane === 'string' ? this._panes[pane] : pane;
974
+ }
975
+
976
+ // @method getPanes(): Object
977
+ // Returns a plain object containing the names of all [panes](#map-pane) as keys and
978
+ // the panes as values.
979
+ getPanes() {
980
+ return this._panes;
981
+ }
982
+
983
+ // @method getContainer: HTMLElement
984
+ // Returns the HTML element that contains the map.
985
+ getContainer() {
986
+ return this._container;
987
+ }
988
+
989
+
990
+ // @section Conversion Methods
991
+
992
+ // @method getZoomScale(toZoom: Number, fromZoom?: Number): Number
993
+ // Returns the scale factor to be applied to a map transition from zoom level
994
+ // `fromZoom` to `toZoom`. Used internally to help with zoom animations.
995
+ getZoomScale(toZoom, fromZoom) {
996
+ // TODO replace with universal implementation after refactoring projections
997
+ const crs = this.options.crs;
998
+ fromZoom ??= this._zoom;
999
+ return crs.scale(toZoom) / crs.scale(fromZoom);
1000
+ }
1001
+
1002
+ // @method getScaleZoom(scale: Number, fromZoom?: Number): Number
1003
+ // Returns the zoom level that the map would end up at, if it is at `fromZoom`
1004
+ // level and everything is scaled by a factor of `scale`. Inverse of
1005
+ // [`getZoomScale`](#map-getZoomScale).
1006
+ getScaleZoom(scale, fromZoom) {
1007
+ const crs = this.options.crs;
1008
+ fromZoom ??= this._zoom;
1009
+ const zoom = crs.zoom(scale * crs.scale(fromZoom));
1010
+ return isNaN(zoom) ? Infinity : zoom;
1011
+ }
1012
+
1013
+ // @method project(latlng: LatLng, zoom?: Number): Point
1014
+ // Projects a geographical coordinate `LatLng` according to the projection
1015
+ // of the map's CRS, then scales it according to `zoom` and the CRS's
1016
+ // `Transformation`. The result is pixel coordinate relative to
1017
+ // the CRS origin.
1018
+ project(latlng, zoom) {
1019
+ zoom ??= this._zoom;
1020
+ return this.options.crs.latLngToPoint(new LatLng(latlng), zoom);
1021
+ }
1022
+
1023
+ // @method unproject(point: Point, zoom?: Number): LatLng
1024
+ // Inverse of [`project`](#map-project).
1025
+ unproject(point, zoom) {
1026
+ zoom ??= this._zoom;
1027
+ return this.options.crs.pointToLatLng(new Point(point), zoom);
1028
+ }
1029
+
1030
+ // @method layerPointToLatLng(point: Point): LatLng
1031
+ // Given a pixel coordinate relative to the [origin pixel](#map-getpixelorigin),
1032
+ // returns the corresponding geographical coordinate (for the current zoom level).
1033
+ layerPointToLatLng(point) {
1034
+ const projectedPoint = new Point(point).add(this.getPixelOrigin());
1035
+ return this.unproject(projectedPoint);
1036
+ }
1037
+
1038
+ // @method latLngToLayerPoint(latlng: LatLng): Point
1039
+ // Given a geographical coordinate, returns the corresponding pixel coordinate
1040
+ // relative to the [origin pixel](#map-getpixelorigin).
1041
+ latLngToLayerPoint(latlng) {
1042
+ const projectedPoint = this.project(new LatLng(latlng))._round();
1043
+ return projectedPoint._subtract(this.getPixelOrigin());
1044
+ }
1045
+
1046
+ // @method wrapLatLng(latlng: LatLng): LatLng
1047
+ // Returns a `LatLng` where `lat` and `lng` has been wrapped according to the
1048
+ // map's CRS's `wrapLat` and `wrapLng` properties, if they are outside the
1049
+ // CRS's bounds.
1050
+ // By default this means longitude is wrapped around the dateline so its
1051
+ // value is between -180 and +180 degrees.
1052
+ wrapLatLng(latlng) {
1053
+ return this.options.crs.wrapLatLng(new LatLng(latlng));
1054
+ }
1055
+
1056
+ // @method wrapLatLngBounds(bounds: LatLngBounds): LatLngBounds
1057
+ // Returns a `LatLngBounds` with the same size as the given one, ensuring that
1058
+ // its center is within the CRS's bounds.
1059
+ // By default this means the center longitude is wrapped around the dateline so its
1060
+ // value is between -180 and +180 degrees, and the majority of the bounds
1061
+ // overlaps the CRS's bounds.
1062
+ wrapLatLngBounds(bounds) {
1063
+ return this.options.crs.wrapLatLngBounds(new LatLngBounds(bounds));
1064
+ }
1065
+
1066
+ // @method distance(latlng1: LatLng, latlng2: LatLng): Number
1067
+ // Returns the distance between two geographical coordinates according to
1068
+ // the map's CRS. By default this measures distance in meters.
1069
+ distance(latlng1, latlng2) {
1070
+ return this.options.crs.distance(new LatLng(latlng1), new LatLng(latlng2));
1071
+ }
1072
+
1073
+ // @method containerPointToLayerPoint(point: Point): Point
1074
+ // Given a pixel coordinate relative to the map container, returns the corresponding
1075
+ // pixel coordinate relative to the [origin pixel](#map-getpixelorigin).
1076
+ containerPointToLayerPoint(point) { // (Point)
1077
+ return new Point(point).subtract(this._getMapPanePos());
1078
+ }
1079
+
1080
+ // @method layerPointToContainerPoint(point: Point): Point
1081
+ // Given a pixel coordinate relative to the [origin pixel](#map-getpixelorigin),
1082
+ // returns the corresponding pixel coordinate relative to the map container.
1083
+ layerPointToContainerPoint(point) { // (Point)
1084
+ return new Point(point).add(this._getMapPanePos());
1085
+ }
1086
+
1087
+ // @method containerPointToLatLng(point: Point): LatLng
1088
+ // Given a pixel coordinate relative to the map container, returns
1089
+ // the corresponding geographical coordinate (for the current zoom level).
1090
+ containerPointToLatLng(point) {
1091
+ const layerPoint = this.containerPointToLayerPoint(new Point(point));
1092
+ return this.layerPointToLatLng(layerPoint);
1093
+ }
1094
+
1095
+ // @method latLngToContainerPoint(latlng: LatLng): Point
1096
+ // Given a geographical coordinate, returns the corresponding pixel coordinate
1097
+ // relative to the map container.
1098
+ latLngToContainerPoint(latlng) {
1099
+ return this.layerPointToContainerPoint(this.latLngToLayerPoint(new LatLng(latlng)));
1100
+ }
1101
+
1102
+ // @method pointerEventToContainerPoint(ev: PointerEvent): Point
1103
+ // Given a PointerEvent object, returns the pixel coordinate relative to the
1104
+ // map container where the event took place.
1105
+ pointerEventToContainerPoint(e) {
1106
+ return DomEvent.getPointerPosition(e, this._container);
1107
+ }
1108
+
1109
+ // @method pointerEventToLayerPoint(ev: PointerEvent): Point
1110
+ // Given a PointerEvent object, returns the pixel coordinate relative to
1111
+ // the [origin pixel](#map-getpixelorigin) where the event took place.
1112
+ pointerEventToLayerPoint(e) {
1113
+ return this.containerPointToLayerPoint(this.pointerEventToContainerPoint(e));
1114
+ }
1115
+
1116
+ // @method pointerEventToLatLng(ev: PointerEvent): LatLng
1117
+ // Given a PointerEvent object, returns geographical coordinate where the
1118
+ // event took place.
1119
+ pointerEventToLatLng(e) { // (PointerEvent)
1120
+ return this.layerPointToLatLng(this.pointerEventToLayerPoint(e));
1121
+ }
1122
+
1123
+
1124
+ // map initialization methods
1125
+
1126
+ _initContainer(id) {
1127
+ const container = this._container = DomUtil.get(id);
1128
+
1129
+ if (!container) {
1130
+ throw new Error('Map container not found.');
1131
+ } else if (container._maplair_id) {
1132
+ throw new Error('Map container is already initialized.');
1133
+ }
1134
+
1135
+ PointerEvents.enablePointerDetection(container);
1136
+ DomEvent.on(container, 'scroll', this._onScroll, this);
1137
+ this._containerId = Util.stamp(container);
1138
+
1139
+ }
1140
+
1141
+ _initLayout() {
1142
+ const container = this._container;
1143
+
1144
+ this._fadeAnimated = this.options.fadeAnimation;
1145
+
1146
+ const classes = ['maplair-container', 'maplair-touch'];
1147
+
1148
+ if (Browser.retina) { classes.push('maplair-retina'); }
1149
+ if (Browser.safari) { classes.push('maplair-safari'); }
1150
+ if (this._fadeAnimated) { classes.push('maplair-fade-anim'); }
1151
+
1152
+ container.classList.add(...classes);
1153
+
1154
+ const {position} = getComputedStyle(container);
1155
+
1156
+ if (position !== 'absolute' && position !== 'relative' && position !== 'fixed' && position !== 'sticky') {
1157
+ container.style.position = 'relative';
1158
+ }
1159
+
1160
+ this._initPanes();
1161
+
1162
+ if (this._initControlPos) {
1163
+ this._initControlPos();
1164
+ }
1165
+ }
1166
+
1167
+ _initPanes() {
1168
+ const panes = this._panes = {};
1169
+ this._paneRenderers = {};
1170
+
1171
+ // @section
1172
+ //
1173
+ // Panes are DOM elements used to control the ordering of layers on the map. You
1174
+ // can access panes with [`map.getPane`](#map-getpane) or
1175
+ // [`map.getPanes`](#map-getpanes) methods. New panes can be created with the
1176
+ // [`map.createPane`](#map-createpane) method.
1177
+ //
1178
+ // Every map has the following default panes that differ only in zIndex.
1179
+ //
1180
+ // @pane mapPane: HTMLElement = 'auto'
1181
+ // Pane that contains all other map panes
1182
+
1183
+ this._mapPane = this.createPane('mapPane', this._container);
1184
+ DomUtil.setPosition(this._mapPane, new Point(0, 0));
1185
+
1186
+ // @pane tilePane: HTMLElement = 200
1187
+ // Pane for `GridLayer`s and `TileLayer`s
1188
+ this.createPane('tilePane');
1189
+ // @pane overlayPane: HTMLElement = 400
1190
+ // Pane for vectors (`Path`s, like `Polyline`s and `Polygon`s), `ImageOverlay`s and `VideoOverlay`s
1191
+ this.createPane('overlayPane');
1192
+ // @pane shadowPane: HTMLElement = 500
1193
+ // Pane for overlay shadows (e.g. `Marker` shadows)
1194
+ this.createPane('shadowPane');
1195
+ // @pane markerPane: HTMLElement = 600
1196
+ // Pane for `Icon`s of `Marker`s
1197
+ this.createPane('markerPane');
1198
+ // @pane tooltipPane: HTMLElement = 650
1199
+ // Pane for `Tooltip`s.
1200
+ this.createPane('tooltipPane');
1201
+ // @pane popupPane: HTMLElement = 700
1202
+ // Pane for `Popup`s.
1203
+ this.createPane('popupPane');
1204
+
1205
+ if (!this.options.markerZoomAnimation) {
1206
+ panes.markerPane.classList.add('maplair-zoom-hide');
1207
+ panes.shadowPane.classList.add('maplair-zoom-hide');
1208
+ }
1209
+ }
1210
+
1211
+
1212
+ // private methods that modify map state
1213
+
1214
+ // @section Map state change events
1215
+ _resetView(center, zoom, noMoveStart) {
1216
+ DomUtil.setPosition(this._mapPane, new Point(0, 0));
1217
+
1218
+ const loading = !this._loaded;
1219
+ this._loaded = true;
1220
+ zoom = this._limitZoom(zoom);
1221
+
1222
+ this.fire('viewprereset');
1223
+
1224
+ const zoomChanged = this._zoom !== zoom;
1225
+ this
1226
+ ._moveStart(zoomChanged, noMoveStart)
1227
+ ._move(center, zoom)
1228
+ ._moveEnd(zoomChanged);
1229
+
1230
+ // @event viewreset: Event
1231
+ // Fired when the map needs to redraw its content (this usually happens
1232
+ // on map zoom or load). Very useful for creating custom overlays.
1233
+ this.fire('viewreset');
1234
+
1235
+ // @event load: Event
1236
+ // Fired when the map is initialized (when its center and zoom are set
1237
+ // for the first time).
1238
+ if (loading) {
1239
+ this.fire('load');
1240
+ }
1241
+ }
1242
+
1243
+ _moveStart(zoomChanged, noMoveStart) {
1244
+ // @event zoomstart: Event
1245
+ // Fired when the map zoom is about to change (e.g. before zoom animation).
1246
+ // @event movestart: Event
1247
+ // Fired when the view of the map starts changing (e.g. user starts dragging the map).
1248
+ if (zoomChanged) {
1249
+ this.fire('zoomstart');
1250
+ }
1251
+ if (!noMoveStart) {
1252
+ this.fire('movestart');
1253
+ }
1254
+ return this;
1255
+ }
1256
+
1257
+ _move(center, zoom, data, suppressEvent) {
1258
+ if (zoom === undefined) {
1259
+ zoom = this._zoom;
1260
+ }
1261
+ const zoomChanged = this._zoom !== zoom;
1262
+
1263
+ this._zoom = zoom;
1264
+ this._lastCenter = center;
1265
+ this._pixelOrigin = this._getNewPixelOrigin(center);
1266
+
1267
+ if (!suppressEvent) {
1268
+ // @event zoom: Event
1269
+ // Fired repeatedly during any change in zoom level,
1270
+ // including zoom and fly animations.
1271
+ if (zoomChanged || (data?.pinch)) { // Always fire 'zoom' if pinching because #3530
1272
+ this.fire('zoom', data);
1273
+ }
1274
+
1275
+ // @event move: Event
1276
+ // Fired repeatedly during any movement of the map,
1277
+ // including pan and fly animations.
1278
+ this.fire('move', data);
1279
+ } else if (data?.pinch) { // Always fire 'zoom' if pinching because #3530
1280
+ this.fire('zoom', data);
1281
+ }
1282
+ return this;
1283
+ }
1284
+
1285
+ _moveEnd(zoomChanged) {
1286
+ // @event zoomend: Event
1287
+ // Fired when the map zoom changed, after any animations.
1288
+ if (zoomChanged) {
1289
+ this.fire('zoomend');
1290
+ }
1291
+
1292
+ // @event moveend: Event
1293
+ // Fired when the center of the map stops changing
1294
+ // (e.g. user stopped dragging the map or after non-centered zoom).
1295
+ return this.fire('moveend');
1296
+ }
1297
+
1298
+ _stop() {
1299
+ cancelAnimationFrame(this._flyToFrame);
1300
+ cancelAnimationFrame(this._zoomAnimFrame);
1301
+ this._panAnim?.stop();
1302
+ return this;
1303
+ }
1304
+
1305
+ _rawPanBy(offset) {
1306
+ DomUtil.setPosition(this._mapPane, this._getMapPanePos().subtract(offset));
1307
+ }
1308
+
1309
+ _getZoomSpan() {
1310
+ return this.getMaxZoom() - this.getMinZoom();
1311
+ }
1312
+
1313
+ _panInsideMaxBounds() {
1314
+ if (!this._enforcingBounds) {
1315
+ this.panInsideBounds(this.options.maxBounds);
1316
+ }
1317
+ }
1318
+
1319
+ _checkIfLoaded() {
1320
+ if (!this._loaded) {
1321
+ throw new Error('Set map center and zoom first.');
1322
+ }
1323
+ }
1324
+
1325
+ // DOM event handling
1326
+
1327
+ // @section Interaction events
1328
+ _initEvents(remove) {
1329
+ this._targets = {};
1330
+ this._targets[Util.stamp(this._container)] = this;
1331
+
1332
+ const onOff = remove ? DomEvent.off : DomEvent.on;
1333
+
1334
+ // @event click: PointerEvent
1335
+ // Fired when the user clicks (or taps) the map.
1336
+ // @event dblclick: PointerEvent
1337
+ // Fired when the user double-clicks (or double-taps) the map.
1338
+ // @event pointerdown: PointerEvent
1339
+ // Fired when the user pushes the pointer on the map.
1340
+ // @event pointerup: PointerEvent
1341
+ // Fired when the user releases the pointer on the map.
1342
+ // @event pointerover: PointerEvent
1343
+ // Fired when the pointer enters the map.
1344
+ // @event pointerout: PointerEvent
1345
+ // Fired when the pointer leaves the map.
1346
+ // @event pointermove: PointerEvent
1347
+ // Fired while the pointer moves over the map.
1348
+ // @event contextmenu: PointerEvent
1349
+ // Fired when the user pushes the right mouse button on the map, prevents
1350
+ // default browser context menu from showing if there are listeners on
1351
+ // this event. Also fired on mobile when the user holds a single touch
1352
+ // for a second (also called long press).
1353
+ // @event keypress: KeyboardEvent
1354
+ // Fired when the user presses a key from the keyboard that produces a character value while the map is focused.
1355
+ // @event keydown: KeyboardEvent
1356
+ // Fired when the user presses a key from the keyboard while the map is focused. Unlike the `keypress` event,
1357
+ // the `keydown` event is fired for keys that produce a character value and for keys
1358
+ // that do not produce a character value.
1359
+ // @event keyup: KeyboardEvent
1360
+ // Fired when the user releases a key from the keyboard while the map is focused.
1361
+ onOff(this._container, 'click dblclick pointerdown pointerup ' +
1362
+ 'pointerover pointerout pointermove contextmenu keypress keydown keyup', this._handleDOMEvent, this);
1363
+
1364
+ if (this.options.trackResize) {
1365
+ if (!remove) {
1366
+ if (!this._resizeObserver) {
1367
+ this._resizeObserver = new ResizeObserver(this._onResize.bind(this));
1368
+ }
1369
+ this._resizeObserver.observe(this._container);
1370
+ } else {
1371
+ this._resizeObserver.disconnect();
1372
+ }
1373
+ }
1374
+
1375
+ if (this.options.transform3DLimit) {
1376
+ (remove ? this.off : this.on).call(this, 'moveend', this._onMoveEnd);
1377
+ }
1378
+ }
1379
+
1380
+ _onResize() {
1381
+ cancelAnimationFrame(this._resizeRequest);
1382
+ this._resizeRequest = requestAnimationFrame(() => { this.invalidateSize({debounceMoveend: true}); });
1383
+ }
1384
+
1385
+ _onScroll() {
1386
+ this._container.scrollTop = 0;
1387
+ this._container.scrollLeft = 0;
1388
+ }
1389
+
1390
+ _onMoveEnd() {
1391
+ const pos = this._getMapPanePos();
1392
+ if (Math.max(Math.abs(pos.x), Math.abs(pos.y)) >= this.options.transform3DLimit) {
1393
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=1203873 but Webkit also have
1394
+ // a pixel offset on very high values, see: https://jsfiddle.net/dg6r5hhb/
1395
+ this._resetView(this.getCenter(), this.getZoom());
1396
+ }
1397
+ }
1398
+
1399
+ _findEventTargets(e, type) {
1400
+ let targets = [],
1401
+ target,
1402
+ src = e.target || e.srcElement,
1403
+ dragging = false;
1404
+ const isHover = type === 'pointerout' || type === 'pointerover';
1405
+
1406
+ while (src) {
1407
+ target = this._targets[Util.stamp(src)];
1408
+ if (target && (type === 'click' || type === 'preclick') && this._draggableMoved(target)) {
1409
+ // Prevent firing click after you just dragged an object.
1410
+ dragging = true;
1411
+ break;
1412
+ }
1413
+ if (target && target.listens(type, true)) {
1414
+ if (isHover && !DomEvent.isExternalTarget(src, e)) { break; }
1415
+ targets.push(target);
1416
+ if (isHover) { break; }
1417
+ }
1418
+ if (src === this._container) { break; }
1419
+ src = src.parentNode;
1420
+ }
1421
+ if (!targets.length && !dragging && !isHover && this.listens(type, true)) {
1422
+ targets = [this];
1423
+ }
1424
+ return targets;
1425
+ }
1426
+
1427
+ _isClickDisabled(el) {
1428
+ while (el && el !== this._container) {
1429
+ if (el['_maplair_disable_click'] || !el.parentNode) { return true; }
1430
+ el = el.parentNode;
1431
+ }
1432
+ }
1433
+
1434
+ _handleDOMEvent(e) {
1435
+ const el = e.target ?? e.srcElement;
1436
+ if (!this._loaded || el['_maplair_disable_events'] || e.type === 'click' && this._isClickDisabled(el)) {
1437
+ return;
1438
+ }
1439
+
1440
+ const type = e.type;
1441
+
1442
+ if (type === 'pointerdown') {
1443
+ // prevents outline when clicking on keyboard-focusable element
1444
+ DomUtil.preventOutline(el);
1445
+ }
1446
+
1447
+ this._fireDOMEvent(e, type);
1448
+ };
1449
+
1450
+ static _pointerEvents = ['click', 'dblclick', 'pointerover', 'pointerout', 'contextmenu'];
1451
+
1452
+ _fireDOMEvent(e, type, canvasTargets) {
1453
+
1454
+ if (type === 'click') {
1455
+ // Fire a synthetic 'preclick' event which propagates up (mainly for closing popups).
1456
+ // @event preclick: PointerEvent
1457
+ // Fired before pointer click on the map (sometimes useful when you
1458
+ // want something to happen on click before any existing click
1459
+ // handlers start running).
1460
+ this._fireDOMEvent(e, 'preclick', canvasTargets);
1461
+ }
1462
+
1463
+ // Find the layer the event is propagating from and its parents.
1464
+ let targets = this._findEventTargets(e, type);
1465
+
1466
+ if (canvasTargets) {
1467
+ // pick only targets with listeners
1468
+ const filtered = canvasTargets.filter(t => t.listens(type, true));
1469
+ targets = [...filtered, ...targets];
1470
+ }
1471
+
1472
+ if (!targets.length) { return; }
1473
+
1474
+ if (type === 'contextmenu') {
1475
+ e.preventDefault();
1476
+ }
1477
+
1478
+ const target = targets[0];
1479
+ const data = {
1480
+ originalEvent: e
1481
+ };
1482
+
1483
+ if (e.type !== 'keypress' && e.type !== 'keydown' && e.type !== 'keyup') {
1484
+ const isMarker = target.getLatLng && (!target._pxRadius || target._pxRadius <= 10);
1485
+ data.containerPoint = isMarker ?
1486
+ this.latLngToContainerPoint(target.getLatLng()) : this.pointerEventToContainerPoint(e);
1487
+ data.layerPoint = this.containerPointToLayerPoint(data.containerPoint);
1488
+ data.latlng = isMarker ? target.getLatLng() : this.layerPointToLatLng(data.layerPoint);
1489
+ }
1490
+
1491
+ for (const t of targets) {
1492
+ t.fire(type, data, true);
1493
+ if (data.originalEvent._stopped ||
1494
+ (t.options.bubblingPointerEvents === false && MaplairMap._pointerEvents.includes(type))) { return; }
1495
+ }
1496
+ }
1497
+
1498
+ _draggableMoved(obj) {
1499
+ obj = obj.dragging?.enabled() ? obj : this;
1500
+ return obj.dragging?.moved() || this.boxZoom?.moved();
1501
+ }
1502
+
1503
+ _clearHandlers() {
1504
+ for (const handler of this._handlers) {
1505
+ handler.disable();
1506
+ }
1507
+ }
1508
+
1509
+ // @section Other Methods
1510
+
1511
+ // @method whenReady(fn: Function, context?: Object): this
1512
+ // Runs the given function `fn` when the map gets initialized with
1513
+ // a view (center and zoom) and at least one layer, or immediately
1514
+ // if it's already initialized, optionally passing a function context.
1515
+ whenReady(callback, context) {
1516
+ if (this._loaded) {
1517
+ callback.call(context || this, {target: this});
1518
+ } else {
1519
+ this.on('load', callback, context);
1520
+ }
1521
+ return this;
1522
+ }
1523
+
1524
+
1525
+ // private methods for getting map state
1526
+
1527
+ _getMapPanePos() {
1528
+ return DomUtil.getPosition(this._mapPane);
1529
+ }
1530
+
1531
+ _moved() {
1532
+ const pos = this._getMapPanePos();
1533
+ return pos && !pos.equals([0, 0]);
1534
+ }
1535
+
1536
+ _getTopLeftPoint(center, zoom) {
1537
+ const pixelOrigin = center && zoom !== undefined ?
1538
+ this._getNewPixelOrigin(center, zoom) :
1539
+ this.getPixelOrigin();
1540
+ return pixelOrigin.subtract(this._getMapPanePos());
1541
+ }
1542
+
1543
+ _getNewPixelOrigin(center, zoom) {
1544
+ const viewHalf = this.getSize()._divideBy(2);
1545
+ return this.project(center, zoom)._subtract(viewHalf)._add(this._getMapPanePos())._round();
1546
+ }
1547
+
1548
+ _latLngToNewLayerPoint(latlng, zoom, center) {
1549
+ const topLeft = this._getNewPixelOrigin(center, zoom);
1550
+ return this.project(latlng, zoom)._subtract(topLeft);
1551
+ }
1552
+
1553
+ _latLngBoundsToNewLayerBounds(latLngBounds, zoom, center) {
1554
+ const topLeft = this._getNewPixelOrigin(center, zoom);
1555
+ return new Bounds([
1556
+ this.project(latLngBounds.getSouthWest(), zoom)._subtract(topLeft),
1557
+ this.project(latLngBounds.getNorthWest(), zoom)._subtract(topLeft),
1558
+ this.project(latLngBounds.getSouthEast(), zoom)._subtract(topLeft),
1559
+ this.project(latLngBounds.getNorthEast(), zoom)._subtract(topLeft)
1560
+ ]);
1561
+ }
1562
+
1563
+ // layer point of the current center
1564
+ _getCenterLayerPoint() {
1565
+ return this.containerPointToLayerPoint(this.getSize()._divideBy(2));
1566
+ }
1567
+
1568
+ // offset of the specified place to the current center in pixels
1569
+ _getCenterOffset(latlng) {
1570
+ return this.latLngToLayerPoint(latlng).subtract(this._getCenterLayerPoint());
1571
+ }
1572
+
1573
+ // adjust center for view to get inside bounds
1574
+ _limitCenter(center, zoom, bounds) {
1575
+
1576
+ if (!bounds) { return center; }
1577
+
1578
+ const centerPoint = this.project(center, zoom),
1579
+ viewHalf = this.getSize().divideBy(2),
1580
+ viewBounds = new Bounds(centerPoint.subtract(viewHalf), centerPoint.add(viewHalf)),
1581
+ offset = this._getBoundsOffset(viewBounds, bounds, zoom);
1582
+
1583
+ // If offset is less than a pixel, ignore.
1584
+ // This prevents unstable projections from getting into
1585
+ // an infinite loop of tiny offsets.
1586
+ if (Math.abs(offset.x) <= 1 && Math.abs(offset.y) <= 1) {
1587
+ return center;
1588
+ }
1589
+
1590
+ return this.unproject(centerPoint.add(offset), zoom);
1591
+ }
1592
+
1593
+ // adjust offset for view to get inside bounds
1594
+ _limitOffset(offset, bounds) {
1595
+ if (!bounds) { return offset; }
1596
+
1597
+ const viewBounds = this.getPixelBounds(),
1598
+ newBounds = new Bounds(viewBounds.min.add(offset), viewBounds.max.add(offset));
1599
+
1600
+ return offset.add(this._getBoundsOffset(newBounds, bounds));
1601
+ }
1602
+
1603
+ // returns offset needed for pxBounds to get inside maxBounds at a specified zoom
1604
+ _getBoundsOffset(pxBounds, maxBounds, zoom) {
1605
+ const projectedMaxBounds = new Bounds(
1606
+ this.project(maxBounds.getNorthEast(), zoom),
1607
+ this.project(maxBounds.getSouthWest(), zoom)
1608
+ ),
1609
+ minOffset = projectedMaxBounds.min.subtract(pxBounds.min),
1610
+ maxOffset = projectedMaxBounds.max.subtract(pxBounds.max),
1611
+
1612
+ dx = this._rebound(minOffset.x, -maxOffset.x),
1613
+ dy = this._rebound(minOffset.y, -maxOffset.y);
1614
+
1615
+ return new Point(dx, dy);
1616
+ }
1617
+
1618
+ _rebound(left, right) {
1619
+ return left + right > 0 ?
1620
+ Math.round(left - right) / 2 :
1621
+ Math.max(0, Math.ceil(left)) - Math.max(0, Math.floor(right));
1622
+ }
1623
+
1624
+ _limitZoom(zoom) {
1625
+ const min = this.getMinZoom(),
1626
+ max = this.getMaxZoom(),
1627
+ snap = this.options.zoomSnap;
1628
+ if (snap) {
1629
+ zoom = Math.round(zoom / snap) * snap;
1630
+ }
1631
+ return Math.max(min, Math.min(max, zoom));
1632
+ }
1633
+
1634
+ _onPanTransitionStep() {
1635
+ this.fire('move');
1636
+ }
1637
+
1638
+ _onPanTransitionEnd() {
1639
+ this._mapPane.classList.remove('maplair-pan-anim');
1640
+ this.fire('moveend');
1641
+ }
1642
+
1643
+ _tryAnimatedPan(center, options) {
1644
+ // difference between the new and current centers in pixels
1645
+ const offset = this._getCenterOffset(center)._trunc();
1646
+
1647
+ // don't animate too far unless animate: true specified in options
1648
+ if (options?.animate !== true && !this.getSize().contains(offset)) { return false; }
1649
+
1650
+ this.panBy(offset, options);
1651
+
1652
+ return true;
1653
+ }
1654
+
1655
+ _createAnimProxy() {
1656
+ this._proxy = DomUtil.create('div', 'maplair-proxy maplair-zoom-animated');
1657
+ this._panes.mapPane.appendChild(this._proxy);
1658
+
1659
+ this.on('zoomanim', this._animateProxyZoom, this);
1660
+ this.on('load moveend', this._animMoveEnd, this);
1661
+
1662
+ DomEvent.on(this._proxy, 'transitionend', this._catchTransitionEnd, this);
1663
+ }
1664
+
1665
+ _animateProxyZoom(e) {
1666
+ const transform = this._proxy.style.transform;
1667
+
1668
+ DomUtil.setTransform(
1669
+ this._proxy,
1670
+ this.project(e.center, e.zoom),
1671
+ this.getZoomScale(e.zoom, 1),
1672
+ );
1673
+
1674
+ // workaround for case when transform is the same and so transitionend event is not fired
1675
+ if (transform === this._proxy.style.transform && this._animatingZoom) {
1676
+ this._onZoomTransitionEnd();
1677
+ }
1678
+ }
1679
+
1680
+ _animMoveEnd() {
1681
+ const c = this.getCenter();
1682
+ const z = this.getZoom();
1683
+
1684
+
1685
+
1686
+ DomUtil.setTransform(
1687
+ this._proxy,
1688
+ this.project(c, z),
1689
+ this.getZoomScale(z, 1),
1690
+ );
1691
+ }
1692
+
1693
+ _destroyAnimProxy() {
1694
+ // Just make sure this method is safe to call from anywhere, without knowledge
1695
+ // of whether the animation proxy was created in the first place.
1696
+ if (this._proxy) {
1697
+ DomEvent.off(this._proxy, 'transitionend', this._catchTransitionEnd, this);
1698
+
1699
+ this._proxy.remove();
1700
+ this.off('zoomanim', this._animateProxyZoom, this);
1701
+ this.off('load moveend', this._animMoveEnd, this);
1702
+
1703
+ delete this._proxy;
1704
+ }
1705
+ }
1706
+
1707
+ _catchTransitionEnd(e) {
1708
+ if (this._animatingZoom && e.propertyName.includes('transform')) {
1709
+ this._onZoomTransitionEnd();
1710
+ }
1711
+ }
1712
+
1713
+ _nothingToAnimate() {
1714
+ return !this._container.getElementsByClassName('maplair-zoom-animated').length;
1715
+ }
1716
+
1717
+ _tryAnimatedZoom(center, zoom, options) {
1718
+
1719
+ if (this._animatingZoom) { return true; }
1720
+
1721
+ options ??= {};
1722
+
1723
+ // don't animate if disabled, not supported or zoom difference is too large
1724
+ if (!this._zoomAnimated || options.animate === false || this._nothingToAnimate() ||
1725
+ Math.abs(zoom - this._zoom) > this.options.zoomAnimationThreshold) { return false; }
1726
+
1727
+ // offset is the pixel coords of the zoom origin relative to the current center
1728
+ const scale = this.getZoomScale(zoom),
1729
+ offset = this._getCenterOffset(center)._divideBy(1 - 1 / scale);
1730
+
1731
+ // don't animate if the zoom origin isn't within one screen from the current center, unless forced
1732
+ if (options.animate !== true && !this.getSize().contains(offset)) { return false; }
1733
+
1734
+ this._zoomAnimFrame = requestAnimationFrame(() => {
1735
+ this._zoomAnimFrame = null;
1736
+ this
1737
+ ._moveStart(true, options.noMoveStart ?? false)
1738
+ ._animateZoom(center, zoom, true);
1739
+ });
1740
+
1741
+ return true;
1742
+ }
1743
+
1744
+ _animateZoom(center, zoom, startAnim, noUpdate) {
1745
+ if (!this._mapPane) { return; }
1746
+
1747
+ if (startAnim) {
1748
+ this._animatingZoom = true;
1749
+
1750
+ // remember what center/zoom to set after animation
1751
+ this._animateToCenter = center;
1752
+ this._animateToZoom = zoom;
1753
+
1754
+ this._mapPane.classList.add('maplair-zoom-anim');
1755
+ }
1756
+
1757
+ // @section Other Events
1758
+ // @event zoomanim: ZoomAnimEvent
1759
+ // Fired at least once per zoom animation. For continuous zoom, like pinch zooming, fired once per frame during zoom.
1760
+ this.fire('zoomanim', {
1761
+ center,
1762
+ zoom,
1763
+ noUpdate
1764
+ });
1765
+
1766
+ if (!this._tempFireZoomEvent) {
1767
+ this._tempFireZoomEvent = this._zoom !== this._animateToZoom;
1768
+ }
1769
+
1770
+ this._move(this._animateToCenter, this._animateToZoom, undefined, true);
1771
+
1772
+ // Work around webkit not firing 'transitionend', see https://github.com/Leaflet/Leaflet/issues/3689, 2693
1773
+ this._transitionEndTimer = setTimeout(this._onZoomTransitionEnd.bind(this), 250);
1774
+ }
1775
+
1776
+ _onZoomTransitionEnd() {
1777
+ if (!this._animatingZoom) { return; }
1778
+
1779
+ this._mapPane?.classList.remove('maplair-zoom-anim');
1780
+
1781
+ this._animatingZoom = false;
1782
+
1783
+ this._move(this._animateToCenter, this._animateToZoom, undefined, true);
1784
+
1785
+ if (this._tempFireZoomEvent) {
1786
+ this.fire('zoom');
1787
+ }
1788
+ delete this._tempFireZoomEvent;
1789
+
1790
+ this.fire('move');
1791
+
1792
+ this._moveEnd(true);
1793
+ }
1794
+ }
1795
+
1796
+ // The default features (layers, popups, tooltips, vector renderers, controls
1797
+ // and interaction handlers) come with every map: they are registered here
1798
+ // rather than by the entry module, so that they are included whenever
1799
+ // `MaplairMap` is, while bundlers can still drop other features (e.g. GeoJSON,
1800
+ // the layers and scale controls) when an application doesn't use them.
1801
+ //
1802
+ // Possible tree-shaking optimization: popups, tooltips and the vector renderers
1803
+ // (DivOverlay, Popup, Tooltip, RendererSupport; about 25 kB of a minimal map's
1804
+ // 100 kB minified) could become optional by registering themselves when their
1805
+ // module is imported (without importing this module, to avoid circular imports)
1806
+ // instead of here. But then `layer.bindPopup()`/`bindTooltip()` and the map's
1807
+ // popup/tooltip methods would only exist in production bundles of apps that
1808
+ // actually use the `Popup`/`Tooltip` classes (bundlers drop an unused
1809
+ // `import {Tooltip}`), while they would always work in development builds.
1810
+ for (const feature of [
1811
+ AttributionControl,
1812
+ Control,
1813
+ DivOverlay,
1814
+ Layer,
1815
+ BoxZoomHandler,
1816
+ DoubleClickZoomHandler,
1817
+ DragHandler,
1818
+ KeyboardHandler,
1819
+ PinchZoomHandler,
1820
+ ScrollWheelZoomHandler,
1821
+ TapHoldHandler,
1822
+ Popup,
1823
+ Tooltip,
1824
+ RendererSupport,
1825
+ ZoomControl
1826
+ ]) {
1827
+ feature.register(MaplairMap);
1828
+ }
1829
+
1830
+ export const Map = MaplairMap;