maplair 4.3.0 → 4.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. package/CHANGELOG.md +151 -8
  2. package/dist/esm/control/AttributionControl.js +30 -13
  3. package/dist/esm/control/AttributionControl.js.map +1 -1
  4. package/dist/esm/control/ButtonControl.js +0 -1
  5. package/dist/esm/control/ButtonControl.js.map +1 -1
  6. package/dist/esm/control/ContextMenu.js +1 -1
  7. package/dist/esm/control/ContextMenu.js.map +1 -1
  8. package/dist/esm/control/Control.js +16 -11
  9. package/dist/esm/control/Control.js.map +1 -1
  10. package/dist/esm/control/FullscreenControl.js +0 -1
  11. package/dist/esm/control/FullscreenControl.js.map +1 -1
  12. package/dist/esm/control/LayersControl.js +46 -27
  13. package/dist/esm/control/LayersControl.js.map +1 -1
  14. package/dist/esm/control/RotateControl.js +0 -1
  15. package/dist/esm/control/RotateControl.js.map +1 -1
  16. package/dist/esm/control/ScaleControl.js +7 -8
  17. package/dist/esm/control/ScaleControl.js.map +1 -1
  18. package/dist/esm/control/SideBySideControl.js +3 -2
  19. package/dist/esm/control/SideBySideControl.js.map +1 -1
  20. package/dist/esm/control/ZoomControl.js +14 -13
  21. package/dist/esm/control/ZoomControl.js.map +1 -1
  22. package/dist/esm/core/Events.js +3 -2
  23. package/dist/esm/core/Events.js.map +1 -1
  24. package/dist/esm/core/Util.js +17 -5
  25. package/dist/esm/core/Util.js.map +1 -1
  26. package/dist/esm/dom/DomEvent.PointerEvents.js +72 -2
  27. package/dist/esm/dom/DomEvent.PointerEvents.js.map +1 -1
  28. package/dist/esm/dom/DomEvent.js +4 -7
  29. package/dist/esm/dom/DomEvent.js.map +1 -1
  30. package/dist/esm/dom/DomUtil.js +5 -5
  31. package/dist/esm/dom/DomUtil.js.map +1 -1
  32. package/dist/esm/dom/Draggable.js +10 -8
  33. package/dist/esm/dom/Draggable.js.map +1 -1
  34. package/dist/esm/geo/LatLng.js +24 -35
  35. package/dist/esm/geo/LatLng.js.map +1 -1
  36. package/dist/esm/geo/LatLngBounds.js +6 -3
  37. package/dist/esm/geo/LatLngBounds.js.map +1 -1
  38. package/dist/esm/geometry/Bounds.js +2 -2
  39. package/dist/esm/geometry/Bounds.js.map +1 -1
  40. package/dist/esm/geometry/Point.js +16 -19
  41. package/dist/esm/geometry/Point.js.map +1 -1
  42. package/dist/esm/geometry/PolyUtil.js +10 -3
  43. package/dist/esm/geometry/PolyUtil.js.map +1 -1
  44. package/dist/esm/geometry/Transformation.js +1 -7
  45. package/dist/esm/geometry/Transformation.js.map +1 -1
  46. package/dist/esm/layer/BlanketOverlay.js +3 -2
  47. package/dist/esm/layer/BlanketOverlay.js.map +1 -1
  48. package/dist/esm/layer/DivOverlay.js +25 -25
  49. package/dist/esm/layer/DivOverlay.js.map +1 -1
  50. package/dist/esm/layer/ImageOverlay.js +8 -5
  51. package/dist/esm/layer/ImageOverlay.js.map +1 -1
  52. package/dist/esm/layer/Layer.js +12 -7
  53. package/dist/esm/layer/Layer.js.map +1 -1
  54. package/dist/esm/layer/Popup.js +33 -15
  55. package/dist/esm/layer/Popup.js.map +1 -1
  56. package/dist/esm/layer/Tooltip.js +48 -56
  57. package/dist/esm/layer/Tooltip.js.map +1 -1
  58. package/dist/esm/layer/VideoOverlay.js +2 -2
  59. package/dist/esm/layer/VideoOverlay.js.map +1 -1
  60. package/dist/esm/layer/marker/DivIcon.js +4 -8
  61. package/dist/esm/layer/marker/DivIcon.js.map +1 -1
  62. package/dist/esm/layer/marker/Icon.js +37 -17
  63. package/dist/esm/layer/marker/Icon.js.map +1 -1
  64. package/dist/esm/layer/marker/Marker.Drag.js +11 -12
  65. package/dist/esm/layer/marker/Marker.Drag.js.map +1 -1
  66. package/dist/esm/layer/marker/Marker.js +54 -53
  67. package/dist/esm/layer/marker/Marker.js.map +1 -1
  68. package/dist/esm/layer/marker/SvgIcon.js +1 -1
  69. package/dist/esm/layer/marker/SvgIcon.js.map +1 -1
  70. package/dist/esm/layer/tile/GridLayer.js +29 -21
  71. package/dist/esm/layer/tile/GridLayer.js.map +1 -1
  72. package/dist/esm/layer/tile/TileLayer.js +19 -15
  73. package/dist/esm/layer/tile/TileLayer.js.map +1 -1
  74. package/dist/esm/layer/tile/WMSTileLayer.js +7 -3
  75. package/dist/esm/layer/tile/WMSTileLayer.js.map +1 -1
  76. package/dist/esm/layer/vector/Canvas.js +45 -94
  77. package/dist/esm/layer/vector/Canvas.js.map +1 -1
  78. package/dist/esm/layer/vector/Circle.js +8 -9
  79. package/dist/esm/layer/vector/Circle.js.map +1 -1
  80. package/dist/esm/layer/vector/CircleMarker.js +15 -4
  81. package/dist/esm/layer/vector/CircleMarker.js.map +1 -1
  82. package/dist/esm/layer/vector/Path.ZoomStyles.js +4 -1
  83. package/dist/esm/layer/vector/Path.ZoomStyles.js.map +1 -1
  84. package/dist/esm/layer/vector/Path.js +10 -5
  85. package/dist/esm/layer/vector/Path.js.map +1 -1
  86. package/dist/esm/layer/vector/Polygon.js +10 -11
  87. package/dist/esm/layer/vector/Polygon.js.map +1 -1
  88. package/dist/esm/layer/vector/Polyline.js +19 -26
  89. package/dist/esm/layer/vector/Polyline.js.map +1 -1
  90. package/dist/esm/layer/vector/Renderer.getRenderer.js +2 -1
  91. package/dist/esm/layer/vector/Renderer.getRenderer.js.map +1 -1
  92. package/dist/esm/layer/vector/Renderer.js +16 -0
  93. package/dist/esm/layer/vector/Renderer.js.map +1 -1
  94. package/dist/esm/layer/vector/SVG.js +3 -3
  95. package/dist/esm/layer/vector/SVG.js.map +1 -1
  96. package/dist/esm/map/Map.Rotation.js +9 -11
  97. package/dist/esm/map/Map.Rotation.js.map +1 -1
  98. package/dist/esm/map/Map.js +158 -71
  99. package/dist/esm/map/Map.js.map +1 -1
  100. package/dist/esm/map/MapSync.js +2 -5
  101. package/dist/esm/map/MapSync.js.map +1 -1
  102. package/dist/esm/map/handler/BoxZoomHandler.js +4 -0
  103. package/dist/esm/map/handler/BoxZoomHandler.js.map +1 -1
  104. package/dist/esm/map/handler/DisplaceHandler.js +68 -30
  105. package/dist/esm/map/handler/DisplaceHandler.js.map +1 -1
  106. package/dist/esm/map/handler/DragHandler.js +7 -2
  107. package/dist/esm/map/handler/DragHandler.js.map +1 -1
  108. package/dist/esm/map/handler/KeyboardHandler.js +3 -5
  109. package/dist/esm/map/handler/KeyboardHandler.js.map +1 -1
  110. package/dist/esm/map/handler/PinchZoomHandler.js +4 -7
  111. package/dist/esm/map/handler/PinchZoomHandler.js.map +1 -1
  112. package/dist/esm/map/handler/ScrollWheelZoomHandler.js +4 -7
  113. package/dist/esm/map/handler/ScrollWheelZoomHandler.js.map +1 -1
  114. package/dist/esm/map/handler/TapDragZoomHandler.js +2 -5
  115. package/dist/esm/map/handler/TapDragZoomHandler.js.map +1 -1
  116. package/dist/esm/map/handler/TapHoldHandler.js +8 -20
  117. package/dist/esm/map/handler/TapHoldHandler.js.map +1 -1
  118. package/dist/esm/package.js +1 -1
  119. package/dist/maplair-global-src.js +970 -810
  120. package/dist/maplair-global-src.js.map +1 -1
  121. package/dist/maplair-global.js +2 -2
  122. package/dist/maplair-global.js.map +1 -1
  123. package/dist/maplair-src.js +970 -810
  124. package/dist/maplair-src.js.map +1 -1
  125. package/dist/maplair.css +1 -1
  126. package/dist/maplair.js +2 -2
  127. package/dist/maplair.js.map +1 -1
  128. package/i18n/en.json +1 -0
  129. package/package.json +1 -1
  130. package/src/control/AttributionControl.js +37 -11
  131. package/src/control/ButtonControl.js +0 -1
  132. package/src/control/ContextMenu.js +1 -1
  133. package/src/control/Control.js +13 -16
  134. package/src/control/FullscreenControl.js +0 -1
  135. package/src/control/LayersControl.js +61 -37
  136. package/src/control/RotateControl.js +0 -1
  137. package/src/control/ScaleControl.js +10 -8
  138. package/src/control/SideBySideControl.js +4 -2
  139. package/src/control/ZoomControl.js +11 -13
  140. package/src/core/Events.js +6 -2
  141. package/src/core/Util.js +17 -4
  142. package/src/dom/DomEvent.PointerEvents.js +68 -2
  143. package/src/dom/DomEvent.js +5 -17
  144. package/src/dom/DomUtil.js +5 -5
  145. package/src/dom/Draggable.js +11 -8
  146. package/src/geo/LatLng.js +24 -37
  147. package/src/geo/LatLngBounds.js +10 -4
  148. package/src/geometry/Bounds.js +3 -3
  149. package/src/geometry/Point.js +22 -28
  150. package/src/geometry/PolyUtil.js +12 -3
  151. package/src/geometry/Transformation.js +1 -8
  152. package/src/layer/BlanketOverlay.js +6 -3
  153. package/src/layer/DivOverlay.js +30 -32
  154. package/src/layer/ImageOverlay.js +12 -9
  155. package/src/layer/Layer.js +19 -7
  156. package/src/layer/Popup.js +41 -21
  157. package/src/layer/Tooltip.js +50 -70
  158. package/src/layer/VideoOverlay.js +2 -2
  159. package/src/layer/marker/DivIcon.js +3 -6
  160. package/src/layer/marker/Icon.js +41 -20
  161. package/src/layer/marker/Marker.Drag.js +22 -24
  162. package/src/layer/marker/Marker.js +71 -64
  163. package/src/layer/marker/SvgIcon.js +1 -1
  164. package/src/layer/tile/GridLayer.js +41 -30
  165. package/src/layer/tile/TileLayer.js +31 -21
  166. package/src/layer/tile/WMSTileLayer.js +4 -2
  167. package/src/layer/vector/Canvas.js +53 -149
  168. package/src/layer/vector/Circle.js +13 -13
  169. package/src/layer/vector/CircleMarker.js +16 -3
  170. package/src/layer/vector/Path.ZoomStyles.js +5 -3
  171. package/src/layer/vector/Path.js +11 -5
  172. package/src/layer/vector/Polygon.js +11 -10
  173. package/src/layer/vector/Polyline.js +20 -21
  174. package/src/layer/vector/Renderer.getRenderer.js +2 -1
  175. package/src/layer/vector/Renderer.js +21 -3
  176. package/src/layer/vector/SVG.js +3 -3
  177. package/src/map/Map.Rotation.js +13 -9
  178. package/src/map/Map.js +195 -83
  179. package/src/map/MapSync.js +2 -2
  180. package/src/map/handler/BoxZoomHandler.js +5 -0
  181. package/src/map/handler/DisplaceHandler.js +53 -7
  182. package/src/map/handler/DragHandler.js +5 -5
  183. package/src/map/handler/KeyboardHandler.js +5 -10
  184. package/src/map/handler/PinchZoomHandler.js +4 -4
  185. package/src/map/handler/ScrollWheelZoomHandler.js +4 -4
  186. package/src/map/handler/TapDragZoomHandler.js +2 -2
  187. package/src/map/handler/TapHoldHandler.js +13 -25
  188. package/src/maplair.css +50 -10
  189. package/dist/esm/dom/DomEvent.DoubleTap.js +0 -90
  190. package/dist/esm/dom/DomEvent.DoubleTap.js.map +0 -1
  191. package/src/dom/DomEvent.DoubleTap.js +0 -143
@@ -3,6 +3,8 @@
3
3
 
4
4
  const activePointers = new Map();
5
5
 
6
+ let lastPointerType;
7
+
6
8
  /**
7
9
  * Enables pointer detection and capture for the document.
8
10
  * @group Pointer detection
@@ -30,8 +32,28 @@ function disablePointerDetection(el) {
30
32
  el.removeEventListener('pointercancel', _onDelete, {capture: true});
31
33
  }
32
34
 
33
- // capturing throws for synthetic pointers the browser doesn't know about, so only capture real ones
35
+ /**
36
+ * Whether `el` is inside an element that `DomEvent.disableClickPropagation` was called
37
+ * on (before reaching `container`), or detached from the DOM.
38
+ * @internal
39
+ * @param {Node|null} el
40
+ * @param {EventTarget|null} container
41
+ * @returns {boolean}
42
+ */
43
+ function isClickDisabled(el, container) {
44
+ while (el && el !== container) {
45
+ if (el['_maplair_disable_click'] || !el.parentNode) { return true; }
46
+ el = el.parentNode;
47
+ }
48
+ return false;
49
+ }
50
+
34
51
  function _onSet(e) {
52
+ lastPointerType = e.pointerType;
53
+ // pointers in controls and popups can't start map interactions, and capturing them breaks native widgets there
54
+ // (like the clear button of a search input), so leave them to the browser
55
+ if (isClickDisabled(e.target, e.currentTarget)) { return; }
56
+ // capturing throws for synthetic pointers the browser doesn't know about, so only capture real ones
35
57
  if (e.isTrusted) { e.target.setPointerCapture(e.pointerId); }
36
58
  activePointers.set(e.pointerId, e);
37
59
  }
@@ -59,9 +81,53 @@ function getPointers() {
59
81
  return [...activePointers.values()];
60
82
  }
61
83
 
84
+ const dblclickProps = ['bubbles', 'cancelable', 'composed', 'view', 'screenX', 'screenY', 'clientX', 'clientY',
85
+ 'ctrlKey', 'shiftKey', 'altKey', 'metaKey', 'button', 'buttons', 'relatedTarget'];
86
+
87
+ let lastClickTime = 0,
88
+ lastClick,
89
+ lastClickMap,
90
+ clickCount = 0;
91
+
92
+ /**
93
+ * Fires a `dblclick` on double tap where the browser doesn't (like iOS Safari),
94
+ * which is where it always reports `detail: 1` on clicks (see Leaflet#7012). The
95
+ * pointer type comes from `pointerdown`, because iOS Safari reports `mouse` on
96
+ * clicks from touch (WebKit bug 282988). Called by the map for clicks in it.
97
+ * @internal
98
+ * @param {MouseEvent} e
99
+ * @param {HTMLElement} mapContainer The container of the map, two taps make a double
100
+ * tap only in the same map.
101
+ * @returns {void}
102
+ */
103
+ function simulateDblclick(e, mapContainer) {
104
+ // a click in a nested map (e.g. an overview map) counts for the innermost one only
105
+ if (e === lastClick) { return; }
106
+ lastClick = e;
107
+ const target = e.target;
108
+
109
+ // a click on a label is followed by a click on its input, so it would count as a double tap
110
+ if (e.detail !== 1 || lastPointerType === 'mouse' || target.closest('label')) {
111
+ clickCount = 0;
112
+ return;
113
+ }
114
+ const now = Date.now();
115
+ clickCount = now - lastClickTime <= 200 && mapContainer === lastClickMap ? clickCount + 1 : 1;
116
+ lastClickTime = now;
117
+ lastClickMap = mapContainer;
118
+
119
+ if (clickCount === 2) {
120
+ const init = {detail: 2};
121
+ for (const prop of dblclickProps) { init[prop] = e[prop]; }
122
+ target.dispatchEvent(new MouseEvent('dblclick', init));
123
+ }
124
+ }
125
+
62
126
  export {
63
127
  enablePointerDetection,
64
128
  disablePointerDetection,
65
- getPointers
129
+ getPointers,
130
+ isClickDisabled,
131
+ simulateDblclick
66
132
  };
67
133
 
@@ -1,6 +1,5 @@
1
1
  import {Point} from '../geometry/Point.js';
2
2
  import * as Util from '../core/Util.js';
3
- import {addDoubleTapListener, removeDoubleTapListener} from './DomEvent.DoubleTap.js';
4
3
  import {getScale} from './DomUtil.js';
5
4
 
6
5
  import * as PointerEvents from './DomEvent.PointerEvents.js';
@@ -93,15 +92,11 @@ function addOne(obj, type, fn, context) {
93
92
 
94
93
  if (obj[eventsKey] && obj[eventsKey][id]) { return; }
95
94
 
96
- let handler = function (e) {
95
+ const handler = function (e) {
97
96
  return fn.call(context || obj, e);
98
97
  };
99
98
 
100
- if (type === 'dblclick') {
101
- handler = addDoubleTapListener(obj, handler);
102
- } else {
103
- obj.addEventListener(type, handler, type === 'wheel' ? {passive: false} : false);
104
- }
99
+ obj.addEventListener(type, handler, type === 'wheel' ? {passive: false} : false);
105
100
 
106
101
  obj[eventsKey] ??= {};
107
102
  obj[eventsKey][id] = handler;
@@ -113,11 +108,7 @@ function removeOne(obj, type, fn, context, id) {
113
108
 
114
109
  if (!handler) { return; }
115
110
 
116
- if (type === 'dblclick') {
117
- removeDoubleTapListener(obj, handler);
118
- } else {
119
- obj.removeEventListener(type, handler, false);
120
- }
111
+ obj.removeEventListener(type, handler, false);
121
112
 
122
113
  obj[eventsKey][id] = null;
123
114
  }
@@ -230,11 +221,8 @@ export function getWheelPxFactor() {
230
221
  * @returns {number}
231
222
  */
232
223
  export function getWheelDelta(e) {
233
- return (e.deltaY && e.deltaMode === 0) ? -e.deltaY / getWheelPxFactor() : // Pixels
234
- (e.deltaY && e.deltaMode === 1) ? -e.deltaY * 20 : // Lines
235
- (e.deltaY && e.deltaMode === 2) ? -e.deltaY * 60 : // Pages
236
- (e.deltaX || e.deltaZ) ? 0 : // Skip horizontal/depth wheel events
237
- 0;
224
+ // scaled per delta mode: pixels, lines, pages; horizontal and depth scrolling give 0
225
+ return -e.deltaY * (e.deltaMode ? e.deltaMode === 1 ? 20 : 60 : 1 / getWheelPxFactor()) || 0;
238
226
  }
239
227
 
240
228
  /**
@@ -31,7 +31,7 @@ export function create(tagName, className, container) {
31
31
  const el = document.createElement(tagName);
32
32
  el.className = className ?? '';
33
33
 
34
- container?.appendChild(el);
34
+ container?.append(el);
35
35
  return el;
36
36
  }
37
37
 
@@ -48,9 +48,9 @@ export function toFront(el) {
48
48
  // re-inserting a hovered node (e.g. bringToFront in a pointerover handler) makes Chromium 144+ fire a repeated
49
49
  // pointerover and lose the next pointerout (#10129), so move the nodes after it instead
50
50
  if (el.matches(':hover')) {
51
- while (el.nextSibling) { parent.insertBefore(el.nextSibling, el); }
51
+ while (el.nextSibling) { el.before(el.nextSibling); }
52
52
  } else {
53
- parent.appendChild(el);
53
+ parent.append(el);
54
54
  }
55
55
  }
56
56
 
@@ -64,9 +64,9 @@ export function toBack(el) {
64
64
  if (!parent || parent.firstChild === el) { return; }
65
65
 
66
66
  if (el.matches(':hover')) { // see toFront
67
- while (el.previousSibling) { parent.insertBefore(el.previousSibling, el.nextSibling); }
67
+ while (el.previousSibling) { el.after(el.previousSibling); }
68
68
  } else {
69
- parent.insertBefore(el, parent.firstChild);
69
+ parent.prepend(el);
70
70
  }
71
71
  }
72
72
 
@@ -15,9 +15,13 @@ import * as PointerEvents from './DomEvent.PointerEvents.js';
15
15
  */
16
16
 
17
17
  /**
18
- * The event object of `dragstart`, `predrag` and `drag` of `Draggable`.
18
+ * The event object of `dragstart`, `predrag` and `drag` of `Draggable`, and of
19
+ * `movestart` and `dragstart` of draggable markers.
19
20
  * @typedef {object} DraggableEvent
20
- * @property {PointerEvent} originalEvent The DOM event of the pointer which drags.
21
+ * @property {PointerEvent|KeyboardEvent} originalEvent The DOM event of the pointer which
22
+ * drags: for `dragstart`, the `pointermove` that moved the pointer past `clickTolerance`
23
+ * (not the `pointerdown`), or the `keydown` of an arrow key that moves a focused
24
+ * draggable marker.
21
25
  * @extends MaplairEvent
22
26
  */
23
27
 
@@ -99,7 +103,6 @@ export class Draggable extends Evented {
99
103
  _onDown(e) {
100
104
  this._moved = false;
101
105
 
102
- if (this._element.classList.contains('maplair-zoom-anim')) { return; }
103
106
 
104
107
  const pointers = PointerEvents.getPointers().length;
105
108
  if (pointers > 1) {
@@ -110,7 +113,9 @@ export class Draggable extends Evented {
110
113
  return;
111
114
  }
112
115
 
113
- if (Draggable._dragging || e.shiftKey || (e.button !== 0 && e.pointerType !== 'touch')) { return; }
116
+ // Shift-drag draws a box to zoom to, unless box zoom is disabled
117
+ const boxZoom = e.shiftKey && this._element.closest('.maplair-container')?.classList.contains('maplair-box-zoom');
118
+ if (Draggable._dragging || boxZoom || (e.button !== 0 && e.pointerType !== 'touch')) { return; }
114
119
  Draggable._dragging = this; // Prevent dragging multiple objects at once.
115
120
 
116
121
  DomUtil.disableImageDrag();
@@ -207,10 +212,8 @@ export class Draggable extends Evented {
207
212
  this._draggingContainer?.classList.remove('maplair-dragging');
208
213
  this._draggingContainer = null;
209
214
 
210
- if (this._lastTarget) {
211
- this._lastTarget.classList.remove('maplair-drag-target');
212
- this._lastTarget = null;
213
- }
215
+ this._lastTarget?.classList.remove('maplair-drag-target');
216
+ this._lastTarget = null;
214
217
 
215
218
  DomEvent.off(this._dragStartTarget, 'pointermove', this._onMove, this);
216
219
  DomEvent.off(this._dragStartTarget, 'pointerup pointercancel lostpointercapture', this._onUp, this);
package/src/geo/LatLng.js CHANGED
@@ -56,53 +56,45 @@ export class LatLng {
56
56
  * @param {number} [alt] Altitude in meters.
57
57
  */
58
58
  constructor(lat, lng, alt) {
59
- const valid = LatLng.validate(lat, lng, alt);
60
- if (!valid) {
61
- throw new Error(`Invalid LatLng object: (${lat}, ${lng})`);
62
- }
63
-
64
- let _lat, _lng, _alt;
59
+ let _lat = lat, _lng = lng, _alt = alt;
65
60
  if (lat instanceof LatLng) {
66
- _lat = lat.lat;
67
- _lng = lat.lng;
68
- _alt = lat.alt;
69
- } else if (Array.isArray(lat) && typeof lat[0] !== 'object') {
70
- if (lat.length === 3) {
71
- _lat = lat[0];
72
- _lng = lat[1];
73
- _alt = lat[2];
74
- } else if (lat.length === 2) {
75
- _lat = lat[0];
76
- _lng = lat[1];
77
- }
78
- } else if (typeof lat === 'object' && 'lat' in lat) {
61
+ ({lat: _lat, lng: _lng, alt: _alt} = lat);
62
+ } else if (Array.isArray(lat)) {
63
+ _lat = lat.length <= 3 ? lat[0] : NaN;
64
+ _lng = lat[1];
65
+ _alt = lat[2];
66
+ } else if (typeof lat === 'object' && lat !== null) {
79
67
  _lat = lat.lat;
80
68
  _lng = 'lng' in lat ? lat.lng : lat.lon;
81
69
  _alt = lat.alt;
82
- } else {
83
- _lat = lat;
84
- _lng = lng;
85
- _alt = alt;
86
70
  }
87
71
 
88
72
  /**
89
73
  * Latitude in degrees.
90
74
  * @type {number}
91
75
  */
92
- this.lat = +_lat;
76
+ this.lat = Util.toNumber(_lat);
93
77
 
94
78
  /**
95
79
  * Longitude in degrees.
96
80
  * @type {number}
97
81
  */
98
- this.lng = +_lng;
82
+ this.lng = Util.toNumber(_lng);
99
83
 
100
- if (_alt !== undefined) {
84
+ if (isNaN(this.lat) || isNaN(this.lng)) {
85
+ throw new Error(`Invalid LatLng object: (${_lat}, ${_lng})`);
86
+ }
87
+
88
+ if (_alt != null) {
101
89
  /**
102
90
  * Altitude in meters (optional).
103
91
  * @type {number|undefined}
104
92
  */
105
- this.alt = +_alt;
93
+ this.alt = Util.toNumber(_alt);
94
+
95
+ if (isNaN(this.alt)) {
96
+ throw new Error(`Invalid LatLng altitude: ${_alt}`);
97
+ }
106
98
  }
107
99
  }
108
100
 
@@ -135,17 +127,11 @@ export class LatLng {
135
127
  */
136
128
  // eslint-disable-next-line no-unused-vars
137
129
  static validate(lat, lng, alt) {
138
- if (lat instanceof LatLng || (typeof lat === 'object' && lat !== null && 'lat' in lat)) {
139
- return true;
140
- } else if (lat && Array.isArray(lat) && typeof lat[0] !== 'object') {
141
- if (lat.length === 3 || lat.length === 2) {
142
- return true;
143
- }
130
+ try {
131
+ return !!new LatLng(lat, lng, alt);
132
+ } catch {
144
133
  return false;
145
- } else if ((lat || lat === 0) && (lng || lng === 0)) {
146
- return true;
147
134
  }
148
- return false;
149
135
  }
150
136
 
151
137
  /**
@@ -270,7 +256,8 @@ export function haversineDistance(latlng1, latlng2, R) {
270
256
  lat2 = latlng2.lat * rad,
271
257
  sinDLat = Math.sin((latlng2.lat - latlng1.lat) * rad / 2),
272
258
  sinDLon = Math.sin((latlng2.lng - latlng1.lng) * rad / 2),
273
- a = sinDLat * sinDLat + Math.cos(lat1) * Math.cos(lat2) * sinDLon * sinDLon,
259
+ // clamp, since rounding can push this slightly above 1 for antipodal points and make `sqrt(1 - a)` NaN
260
+ a = Math.min(sinDLat * sinDLat + Math.cos(lat1) * Math.cos(lat2) * sinDLon * sinDLon, 1),
274
261
  c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
275
262
  return R * c;
276
263
  }
@@ -98,7 +98,7 @@ export class LatLngBounds {
98
98
  if (!sw2) { return this; }
99
99
  } else if (!obj) {
100
100
  return this;
101
- } else if (LatLng.validate(obj)) {
101
+ } else if (isLatLngExpression(obj)) {
102
102
  sw2 = ne2 = LatLng.from(obj);
103
103
  } else {
104
104
  return this.extend(LatLngBounds.from(obj));
@@ -215,7 +215,7 @@ export class LatLngBounds {
215
215
  * @returns {boolean}
216
216
  */
217
217
  contains(obj) { // (LatLngBounds) or (LatLng) -> Boolean
218
- if (LatLng.validate(obj)) {
218
+ if (isLatLngExpression(obj)) {
219
219
  obj = LatLng.from(obj);
220
220
  } else {
221
221
  obj = LatLngBounds.from(obj);
@@ -270,8 +270,8 @@ export class LatLngBounds {
270
270
  sw2 = bounds.getSouthWest(),
271
271
  ne2 = bounds.getNorthEast(),
272
272
 
273
- latOverlaps = (ne2.lat > sw.lat) && (sw2.lat < ne.lat),
274
- lngOverlaps = (ne2.lng > sw.lng) && (sw2.lng < ne.lng);
273
+ latOverlaps = Math.max(sw.lat, sw2.lat) < Math.min(ne.lat, ne2.lat),
274
+ lngOverlaps = Math.max(sw.lng, sw2.lng) < Math.min(ne.lng, ne2.lng);
275
275
 
276
276
  return latOverlaps && lngOverlaps;
277
277
  }
@@ -311,3 +311,9 @@ export class LatLngBounds {
311
311
  return !!(this._southWest && this._northEast);
312
312
  }
313
313
  }
314
+
315
+ // Whether `obj` has the shape of a `LatLngExpression` rather than of bounds.
316
+ function isLatLngExpression(obj) {
317
+ return obj instanceof LatLng || (Array.isArray(obj) ? obj.length > 0 && typeof obj[0] !== 'object' :
318
+ typeof obj === 'object' && obj !== null && 'lat' in obj);
319
+ }
@@ -206,8 +206,8 @@ export class Bounds {
206
206
  max = this.max,
207
207
  min2 = bounds.min,
208
208
  max2 = bounds.max,
209
- xOverlaps = (max2.x > min.x) && (min2.x < max.x),
210
- yOverlaps = (max2.y > min.y) && (min2.y < max.y);
209
+ xOverlaps = Math.max(min.x, min2.x) < Math.min(max.x, max2.x),
210
+ yOverlaps = Math.max(min.y, min2.y) < Math.min(max.y, max2.y);
211
211
 
212
212
  return xOverlaps && yOverlaps;
213
213
  }
@@ -258,5 +258,5 @@ export class Bounds {
258
258
 
259
259
  // Whether `obj` is a point (or an array or object literal of one) rather than bounds
260
260
  function isPointExpression(obj) {
261
- return obj instanceof Point || (Array.isArray(obj) ? typeof obj[0] === 'number' : 'x' in obj);
261
+ return obj instanceof Point || (Array.isArray(obj) ? obj.length > 0 && typeof obj[0] !== 'object' : 'x' in obj);
262
262
  }
@@ -1,4 +1,5 @@
1
- import {formatNum} from '../core/Util.js';
1
+ // a named import of toNumber makes Point#add ~10% slower in V8
2
+ import * as Util from '../core/Util.js';
2
3
 
3
4
  /**
4
5
  * A point in pixels: a `Point`, an array of the form `[x, y]`, or a plain object of
@@ -38,37 +39,31 @@ export class Point {
38
39
  * @param {boolean} [round]
39
40
  */
40
41
  constructor(x, y, round) {
41
-
42
- const valid = Point.validate(x, y);
43
- if (!valid) {
44
- throw new Error(`Invalid Point object: (${x}, ${y})`);
45
- }
46
-
47
- let _x, _y;
48
- if (x instanceof Point) {
49
- _x = x.x;
50
- _y = x.y;
51
- } else if (Array.isArray(x)) {
42
+ let _x = x, _y = y;
43
+ if (Array.isArray(x)) {
52
44
  _x = x[0];
53
45
  _y = x[1];
54
- } else if (typeof x === 'object' && 'x' in x && 'y' in x) {
46
+ } else if (typeof x === 'object' && x !== null) {
55
47
  _x = x.x;
56
48
  _y = x.y;
57
- } else {
58
- _x = x;
59
- _y = y;
60
49
  }
61
50
 
62
51
  /**
63
52
  * The `x` coordinate of the point.
64
53
  * @type {number}
65
54
  */
66
- this.x = (round ? Math.round(_x) : _x);
55
+ this.x = round ? Math.round(Util.toNumber(_x)) : Util.toNumber(_x);
67
56
  /**
68
57
  * The `y` coordinate of the point.
69
58
  * @type {number}
70
59
  */
71
- this.y = (round ? Math.round(_y) : _y);
60
+ this.y = round ? Math.round(Util.toNumber(_y)) : Util.toNumber(_y);
61
+
62
+ // NaN check; isNaN() here makes Point#add ~10% slower in V8
63
+ // eslint-disable-next-line no-self-compare
64
+ if (this.x !== this.x || this.y !== this.y) {
65
+ throw new Error(`Invalid Point object: (${_x}, ${_y})`);
66
+ }
72
67
  }
73
68
 
74
69
  /**
@@ -98,14 +93,11 @@ export class Point {
98
93
  * @returns {boolean}
99
94
  */
100
95
  static validate(x, y) {
101
- if (x instanceof Point || Array.isArray(x)) {
102
- return true;
103
- } else if (x && typeof x === 'object' && 'x' in x && 'y' in x) {
104
- return true;
105
- } else if ((x || x === 0) && (y || y === 0)) {
106
- return true;
96
+ try {
97
+ return !!new Point(x, y);
98
+ } catch {
99
+ return false;
107
100
  }
108
- return false;
109
101
  }
110
102
 
111
103
  /**
@@ -175,20 +167,22 @@ export class Point {
175
167
  * `scale`. In linear algebra terms, multiply the point by the
176
168
  * [scaling matrix](https://en.wikipedia.org/wiki/Scaling_%28geometry%29#Matrix_representation)
177
169
  * defined by `scale`.
178
- * @param {Point} point The scale.
170
+ * @param {PointExpression} point The scale.
179
171
  * @returns {Point}
180
172
  */
181
173
  scaleBy(point) {
174
+ point = Point.from(point);
182
175
  return new Point(this.x * point.x, this.y * point.y);
183
176
  }
184
177
 
185
178
  /**
186
179
  * Inverse of `scaleBy`. Divide each coordinate of the current point by
187
180
  * each coordinate of `scale`.
188
- * @param {Point} point The scale.
181
+ * @param {PointExpression} point The scale.
189
182
  * @returns {Point}
190
183
  */
191
184
  unscaleBy(point) {
185
+ point = Point.from(point);
192
186
  return new Point(this.x / point.x, this.y / point.y);
193
187
  }
194
188
 
@@ -293,6 +287,6 @@ export class Point {
293
287
  * @returns {string}
294
288
  */
295
289
  toString() {
296
- return `Point(${formatNum(this.x)}, ${formatNum(this.y)})`;
290
+ return `Point(${Util.formatNum(this.x)}, ${Util.formatNum(this.y)})`;
297
291
  }
298
292
  }
@@ -1,8 +1,8 @@
1
1
  import * as LineUtil from './LineUtil.js';
2
2
  import {LatLng} from '../geo/LatLng.js';
3
3
  import {Point} from './Point.js';
4
+ import {Bounds} from './Bounds.js';
4
5
  /** @import {LatLngExpression} from '../geo/LatLng.js' */
5
- /** @import {Bounds} from './Bounds.js' */
6
6
  /** @import {CRS} from '../geo/crs/CRS.js' */
7
7
 
8
8
  // PolyUtil: various utility functions for polygon geometries.
@@ -81,8 +81,17 @@ export function polygonCenter(latlngs, crs) {
81
81
  area += f;
82
82
  }
83
83
 
84
- // area is 0 when the polygon is so small that all points are on the same pixel
85
- return crs.unproject(area === 0 ? origin : new Point(x / (3 * area), y / (3 * area))._add(origin));
84
+ if (area !== 0) { return crs.unproject(new Point(x / (3 * area), y / (3 * area))._add(origin)); }
85
+
86
+ // area is 0 when all points lie on one line (or on the same spot), or when the ring goes back over its own edges;
87
+ // take the middle of the points' extent and snap it to the closest point of the ring so it stays on the shape
88
+ const mid = new Bounds(points).getCenter();
89
+ let center = points[0];
90
+ for (let i = 0, j = len - 1; i < len; j = i++) {
91
+ const p = LineUtil._sqClosestPointOnSegment(mid, points[j], points[i]);
92
+ if (p.distanceTo(mid) < center.distanceTo(mid)) { center = p; }
93
+ }
94
+ return crs.unproject(center._add(origin));
86
95
  }
87
96
 
88
97
  /**
@@ -23,14 +23,7 @@ export class Transformation {
23
23
  * @param {number} [d]
24
24
  */
25
25
  constructor(a, b, c, d) {
26
- if (Array.isArray(a)) {
27
- // use array properties
28
- this._a = a[0];
29
- this._b = a[1];
30
- this._c = a[2];
31
- this._d = a[3];
32
- return;
33
- }
26
+ if (Array.isArray(a)) { [a, b, c, d] = a; }
34
27
  this._a = a;
35
28
  this._b = b;
36
29
  this._c = c;
@@ -72,7 +72,7 @@ export class BlanketOverlay extends Layer {
72
72
  this._container.classList.add('maplair-zoom-animated');
73
73
  }
74
74
 
75
- this.getPane().appendChild(this._container);
75
+ this.getPane().append(this._container);
76
76
  this._resizeContainer();
77
77
  this._onMoveEnd();
78
78
  }
@@ -117,8 +117,11 @@ export class BlanketOverlay extends Layer {
117
117
  viewHalf = this._map.getSize().multiplyBy(0.5 + this.options.padding),
118
118
  currentCenterPoint = this._map.project(this._center, zoom),
119
119
  topLeftOffset = viewHalf.multiplyBy(-scale)._add(currentCenterPoint)
120
- ._subtract(this._map._getNewPixelOrigin(center, zoom))
121
- ._round();
120
+ ._subtract(this._map._getNewPixelOrigin(center, zoom));
121
+
122
+ // a scaled container is resampled anyway, and rounding where its corner lands would make its content away
123
+ // from the corner jitter against the zoom motion
124
+ if (scale === 1) { this._map._snapLayerPoint(topLeftOffset); }
122
125
 
123
126
  DomUtil.setTransform(this._container, topLeftOffset, scale);
124
127
  }