-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaflet-rotate1.js
1661 lines (1371 loc) · 58.2 KB
/
leaflet-rotate1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
})((function () { 'use strict';
/**
* @external L.DomUtil
*
* @see https://github.com/Leaflet/Leaflet/tree/v1.9.3/src/dom/DomUtil.js
*/
const domUtilProto = L.extend({}, L.DomUtil);
L.extend(L.DomUtil, {
setTransform: function(el, offset, scale, bearing, pivot) {
var pos = offset || new L.Point(0, 0);
if (!bearing) {
offset = pos._round();
return domUtilProto.setTransform.call(this, el, offset, scale);
}
pos = pos.rotateFrom(bearing, pivot);
el.style[L.DomUtil.TRANSFORM] =
'translate3d(' + pos.x + 'px,' + pos.y + 'px' + ',0)' +
(scale ? ' scale(' + scale + ')' : '') +
' rotate(' + bearing + 'rad)';
},
setPosition: function(el, point, bearing, pivot) { // (HTMLElement, Point[, Boolean])
if (!bearing) {
return domUtilProto.setPosition.call(this, el, point);
}
/*eslint-disable */
el._leaflet_pos = point;
/*eslint-enable */
if (L.Browser.any3d) {
L.DomUtil.setTransform(el, point, undefined, bearing, pivot);
} else {
el.style.left = point.x + 'px';
el.style.top = point.y + 'px';
}
},
// Constants for rotation
DEG_TO_RAD: Math.PI / 180,
RAD_TO_DEG: 180 / Math.PI,
});
/**
* @external L.Draggable
*
* @see https://github.com/Leaflet/Leaflet/tree/v1.9.3/src/dom/Draggable.js
*/
L.Draggable.include({
// updateMapBearing: function(mapBearing) {
// this._mapBearing = mapBearing;
// },
});
/**
* @external L.Point
*
* @see https://github.com/Leaflet/Leaflet/tree/v1.9.3/src/geometry/Point.js
*/
L.extend(L.Point.prototype, {
/**
* Rotate around (0,0) by applying the 2D rotation matrix:
*
* ⎡ x' ⎤ = ⎡ cos θ -sin θ ⎤ ⎡ x ⎤
* ⎣ y' ⎦ ⎣ sin θ cos θ ⎦ ⎣ y ⎦
*
* @param theta must be given in radians.
*/
rotate: function(theta) {
return this.rotateFrom(theta, new L.Point(0,0))
},
/**
* Rotate around (pivot.x, pivot.y) by:
*
* 1. subtract (pivot.x, pivot.y)
* 2. rotate around (0, 0)
* 3. add (pivot.x, pivot.y) back
*
* same as `this.subtract(pivot).rotate(theta).add(pivot)`
*/
rotateFrom: function(theta, pivot) {
if (!theta) { return this; }
var sinTheta = Math.sin(theta);
var cosTheta = Math.cos(theta);
var cx = pivot.x,
cy = pivot.y;
var x = this.x - cx,
y = this.y - cy;
return new L.Point(
x * cosTheta - y * sinTheta + cx,
x * sinTheta + y * cosTheta + cy
);
},
});
/**
* @external L.DivOverlay
*
* @see https://github.com/Leaflet/Leaflet/tree/v1.9.3/src/layer/DivOverlay.js
*/
const divOverlayProto = L.extend({}, L.DivOverlay.prototype);
L.DivOverlay.include({
getEvents: function() {
return L.extend(divOverlayProto.getEvents.call(this), { rotate: this._updatePosition });
},
_updatePosition: function() {
// 0. update anchor (leaflet v1.9.3)
divOverlayProto._updatePosition.call(this);
// 1. subtract anchor
// 2. rotate element
// 3. restore anchor
if (this._map && this._map._rotate && this._zoomAnimated) {
var anchor = this._getAnchor();
var pos = L.DomUtil.getPosition(this._container).subtract(anchor);
L.DomUtil.setPosition(this._container, this._map.rotatedPointToMapPanePoint(pos).add(anchor));
}
},
});
/**
* @external L.Popup
*
* @see https://github.com/Leaflet/Leaflet/tree/v1.9.3/src/layer/Popup.js
*/
const popupProto = L.extend({}, L.Popup.prototype);
L.Popup.include({
_animateZoom: function(e) {
// 0. update anchor (leaflet v1.9.3)
popupProto._animateZoom.call(this, e);
// 1. subtract anchor
// 2. rotate element
// 3. restore anchor
if (this._map && this._map._rotate) {
var anchor = this._getAnchor();
var pos = L.DomUtil.getPosition(this._container).subtract(anchor);
L.DomUtil.setPosition(this._container, this._map.rotatedPointToMapPanePoint(pos).add(anchor));
}
},
/**
* Hot fix for L.Popup.mergeOptions({ keepInView: true, });
*
* @see https://github.com/fnicollet/Leaflet/pull/21
*/
_adjustPan: function() {
if (!this.options.autoPan || (this._map._panAnim && this._map._panAnim._inProgress)) { return; }
// We can endlessly recurse if keepInView is set and the view resets.
// Let's guard against that by exiting early if we're responding to our own autopan.
if (this._autopanning) {
this._autopanning = false;
return;
}
var map = this._map,
marginBottom = parseInt(L.DomUtil.getStyle(this._container, 'marginBottom'), 10) || 0,
containerHeight = this._container.offsetHeight + marginBottom,
containerWidth = this._containerWidth,
layerPos = new L.Point(this._containerLeft, -containerHeight - this._containerBottom);
layerPos._add(L.DomUtil.getPosition(this._container));
// var containerPos = map.layerPointToContainerPoint(layerPos);
/** @TODO use popupProto._adjustPan */
var containerPos = layerPos._add(this._map._getMapPanePos()),
padding = L.point(this.options.autoPanPadding),
paddingTL = L.point(this.options.autoPanPaddingTopLeft || padding),
paddingBR = L.point(this.options.autoPanPaddingBottomRight || padding),
size = map.getSize(),
dx = 0,
dy = 0;
if (containerPos.x + containerWidth + paddingBR.x > size.x) { // right
dx = containerPos.x + containerWidth - size.x + paddingBR.x;
}
if (containerPos.x - dx - paddingTL.x < 0) { // left
dx = containerPos.x - paddingTL.x;
}
if (containerPos.y + containerHeight + paddingBR.y > size.y) { // bottom
dy = containerPos.y + containerHeight - size.y + paddingBR.y;
}
if (containerPos.y - dy - paddingTL.y < 0) { // top
dy = containerPos.y - paddingTL.y;
}
// @namespace Map
// @section Popup events
// @event autopanstart: Event
// Fired when the map starts autopanning when opening a popup.
if (dx || dy) {
// Track that we're autopanning, as this function will be re-ran on moveend
if (this.options.keepInView) {
this._autopanning = true;
}
map
.fire('autopanstart')
.panBy([dx, dy]);
}
},
});
/**
* @external L.Tooltip
*
* @see https://github.com/Leaflet/Leaflet/tree/v1.9.3/src/layer/Tooltip.js
*/
const tooltipProto = L.extend({}, L.Tooltip.prototype);
L.Tooltip.include({
_animateZoom: function(e) {
if (!this._map._rotate) {
return tooltipProto._animateZoom.call(this, e);
}
var pos = this._map._latLngToNewLayerPoint(this._latlng, e.zoom, e.center);
pos = this._map.rotatedPointToMapPanePoint(pos);
this._setPosition(pos);
},
_updatePosition: function() {
if (!this._map._rotate) {
return tooltipProto._updatePosition.call(this);
}
var pos = this._map.latLngToLayerPoint(this._latlng);
pos = this._map.rotatedPointToMapPanePoint(pos);
this._setPosition(pos);
},
});
/**
* @external L.Icon
*
* @see https://github.com/Leaflet/Leaflet/tree/v1.9.3/src/layer/marker/Icon.js
*/
L.extend({}, L.Icon.prototype);
L.Icon.include({
_setIconStyles: function(img, name) {
var options = this.options;
var sizeOption = options[name + 'Size'];
if (typeof sizeOption === 'number') {
sizeOption = [sizeOption, sizeOption];
}
var size = L.point(sizeOption),
anchor = L.point(name === 'shadow' && options.shadowAnchor || options.iconAnchor ||
size && size.divideBy(2, true));
img.className = 'leaflet-marker-' + name + ' ' + (options.className || '');
if (anchor) {
img.style.marginLeft = (-anchor.x) + 'px';
img.style.marginTop = (-anchor.y) + 'px';
/** @TODO use iconProto._setIconStyles */
img.style[L.DomUtil.TRANSFORM + "Origin"] = anchor.x + "px " + anchor.y + "px 0px";
}
if (size) {
img.style.width = size.x + 'px';
img.style.height = size.y + 'px';
}
},
});
/**
* @external L.Marker
* @external L.Handler.MarkerDrag
*
* @see https://github.com/Leaflet/Leaflet/tree/v1.9.3/src/layer/marker/Marker.js
*/
const markerProto = L.extend({}, L.Marker.prototype);
L.Marker.mergeOptions({
// @option rotation: Number = 0
// Rotation of this marker in rad
rotation: 0,
// @option rotateWithView: Boolean = false
// Rotate this marker when map rotates
rotateWithView: false,
});
var markerDragProto; // retrived at runtime (see below: L.Marker::_initInteraction())
var MarkerDrag = {
// _onDragStart: function() {
// if (!this._marker._map._rotate) {
// return markerDragProto._onDragStart.call(this)
// }
// this._draggable.updateMapBearing(this._marker._map._bearing);
// },
_onDrag: function(e) {
var marker = this._marker,
/** @TODO use markerDragProto._onDrag */
rotated_marker = marker.options.rotation || marker.options.rotateWithView,
shadow = marker._shadow,
iconPos = L.DomUtil.getPosition(marker._icon);
/** @TODO use markerDragProto._onDrag */
// update shadow position
if (!rotated_marker && shadow) {
L.DomUtil.setPosition(shadow, iconPos);
}
/** @TODO use markerDragProto._onDrag */
if (marker._map._rotate) {
// Reverse calculation from mapPane coordinates to rotatePane coordinates
iconPos = marker._map.mapPanePointToRotatedPoint(iconPos);
}
var latlng = marker._map.layerPointToLatLng(iconPos);
marker._latlng = latlng;
e.latlng = latlng;
e.oldLatLng = this._oldLatLng;
/** @TODO use markerDragProto._onDrag */
if (rotated_marker) marker.setLatLng(latlng); // use `setLatLng` to presisit rotation. low efficiency
else marker.fire('move', e); // `setLatLng` will trig 'move' event. we imitate here.
// @event drag: Event
// Fired repeatedly while the user drags the marker.
marker
.fire('drag', e);
},
_onDragEnd: function(e) {
if (this._marker._map._rotate) {
this._marker.update();
}
markerDragProto._onDragEnd.call(this, e);
},
};
L.Marker.include({
getEvents: function() {
return L.extend(markerProto.getEvents.call(this), { rotate: this.update });
},
onAdd: function(map) {
markerProto.onAdd.call(this, map);
map.on('rotate', this.update, this);
},
_initInteraction: function() {
var ret = markerProto._initInteraction.call(this);
if (this.dragging && this.dragging.enabled() && this._map && this._map._rotate) {
// L.Handler.MarkerDrag is used internally by L.Marker to make the markers draggable
markerDragProto = markerDragProto || Object.getPrototypeOf(this.dragging);
Object.assign(this.dragging, {
// _onDragStart: MarkerDrag._onDragStart.bind(this.dragging),
_onDrag: MarkerDrag._onDrag.bind(this.dragging),
_onDragEnd: MarkerDrag._onDragEnd.bind(this.dragging),
});
this.dragging.disable();
this.dragging.enable();
}
return ret;
},
_setPos: function(pos) {
/** @TODO use markerProto._setPos */
if (this._map._rotate) {
pos = this._map.rotatedPointToMapPanePoint(pos);
}
/** @TODO use markerProto._setPos */
var bearing = this.options.rotation || 0;
if (this.options.rotateWithView) {
bearing += this._map._bearing;
}
/** @TODO use markerProto._setPos */
if (this._icon) {
L.DomUtil.setPosition(this._icon, pos, bearing, pos);
}
/** @TODO use markerProto._setPos */
if (this._shadow) {
L.DomUtil.setPosition(this._shadow, pos, bearing, pos);
}
this._zIndex = pos.y + this.options.zIndexOffset;
this._resetZIndex();
},
_updateZIndex: function(offset) {
if (!this._map._rotate) {
return markerProto._updateZIndex.call(this, offset)
}
this._icon.style.zIndex = Math.round(this._zIndex + offset);
},
setRotation: function(rotation) {
this.options.rotation = rotation;
this.update();
},
});
/**
* @external L.GridLayer
*
* @see https://github.com/Leaflet/Leaflet/tree/v1.9.3/src/layer/tile/GridLayer.js
*/
const gridLayerProto = L.extend({}, L.GridLayer.prototype);
L.GridLayer.include({
getEvents: function() {
var events = gridLayerProto.getEvents.call(this);
if (this._map._rotate && !this.options.updateWhenIdle) {
if (!this._onRotate) {
this._onRotate = L.Util.throttle(this._onMoveEnd, this.options.updateInterval, this);
}
events.rotate = this._onRotate;
}
return events;
},
_getTiledPixelBounds: function(center) {
if (!this._map._rotate) {
return gridLayerProto._getTiledPixelBounds.call(this, center);
}
return this._map._getNewPixelBounds(center, this._tileZoom);
},
});
/**
* @external L.Canvas
*
* @see https://github.com/Leaflet/Leaflet/tree/v1.9.3/src/layer/vector/Canvas.js
*/
L.extend({}, L.Canvas.prototype);
L.Canvas.include({
// onAdd: function() {
// canvasProto.onAdd.call(this);
// // When rotating the canvas itself, it is cleared by some weird reason, so redraw.
// this._map.on('rotate', this._redraw, this);
// },
// onRemove: function() {
// canvasProto.onRemove.call(this);
// this._map.off('rotate', this._redraw, this);
// },
// _update: function() {
// canvasProto._update.call(this);
// // Tell paths to redraw themselves
// this.fire('update')
// },
});
/**
* @external L.Renderer
*
* @see https://github.com/Leaflet/Leaflet/tree/v1.9.3/src/layer/vector/Renderer.js
*/
const rendererProto = L.extend({}, L.Renderer.prototype);
L.Renderer.include({
// onAdd: function() {
// rendererProto.onAdd.call(this);
// // this._map.on('rotate', this._update, this);
// },
// onRemove: function() {
// rendererProto.onRemove.call(this);
// // this._map.off('rotate', this._update, this);
// },
/**
* @TODO rechek this changes from leaflet@v1.9.3
*
* @see https://github.com/Leaflet/Leaflet/compare/v1.7.0...v1.9.3
*/
_updateTransform: function(center, zoom) {
if (!this._map._rotate) {
return rendererProto._updateTransform.call(this, center, zoom);
}
var scale = this._map.getZoomScale(zoom, this._zoom),
offset = this._map._latLngToNewLayerPoint(this._topLeft, zoom, center);
if (L.Browser.any3d) {
L.DomUtil.setTransform(this._container, offset, scale);
} else {
L.DomUtil.setPosition(this._container, offset);
}
},
_update: function() {
if (!this._map._rotate) {
return rendererProto._update.call(this);
}
// Update pixel bounds of renderer container (for positioning/sizing/clipping later)
// Subclasses are responsible of firing the 'update' event.
this._bounds = this._map._getPaddedPixelBounds(this.options.padding);
this._topLeft = this._map.layerPointToLatLng(this._bounds.min);
this._center = this._map.getCenter();
this._zoom = this._map.getZoom();
},
});
/**
* @external L.SVG
*
* @see https://github.com/Leaflet/Leaflet/tree/v1.9.3/src/layer/vector/SVG.js
*/
L.extend({}, L.SVG.prototype);
L.SVG.include({
// _update: function() {
// svgProto._update.call(this);
// if (this._map._rotate) {
// this.fire('update');
// }
// },
});
/**
* @external L.Map
*
* @see https://github.com/Leaflet/Leaflet/blob/v1.9.3/src/map/Map.js
*/
const mapProto = L.extend({}, L.Map.prototype);
L.Map.mergeOptions({ rotate: false, bearing: 0, });
L.Map.include({
/**
* @param {(HTMLElement|String)} id html selector
* @param {Object} [options={}] leaflet map options
*/
initialize: function(id, options) {
if (options.rotate) {
this._rotate = true;
this._bearing = 0;
}
mapProto.initialize.call(this, id, options);
if(this.options.rotate){
this.setBearing(this.options.bearing);
}
},
/**
* Given a pixel coordinate relative to the map container,
* returns the corresponding pixel coordinate relative to
* the [origin pixel](#map-getpixelorigin).
*
* @param {L.Point} point pixel screen coordinates
* @returns {L.Point} transformed pixel point
*/
containerPointToLayerPoint: function(point) {
if (!this._rotate) {
return mapProto.containerPointToLayerPoint.call(this, point);
}
return L.point(point)
.subtract(this._getMapPanePos())
.rotateFrom(-this._bearing, this._getRotatePanePos())
.subtract(this._getRotatePanePos());
},
/**
* Given a pixel coordinate relative to the [origin pixel](#map-getpixelorigin),
* returns the corresponding pixel coordinate relative to the map container.
*
* @param {L.Point} point pixel screen coordinates
* @returns {L.Point} transformed pixel point
*/
layerPointToContainerPoint: function(point) { // (Point)
if (!this._rotate) {
return mapProto.layerPointToContainerPoint.call(this, point);
}
return L.point(point)
.add(this._getRotatePanePos())
.rotateFrom(this._bearing, this._getRotatePanePos())
.add(this._getMapPanePos());
},
/**
* Converts a coordinate from the rotated pane reference system
* to the reference system of the not rotated map pane.
*
* (rotatePane) --> (mapPane)
* (rotatePane) --> (norotatePane)
*
* @param {L.Point} point pixel screen coordinates
* @returns {L.Point}
*/
rotatedPointToMapPanePoint: function(point) {
return L.point(point)
.rotate(this._bearing)
._add(this._getRotatePanePos());
},
/**
* Converts a coordinate from the not rotated map pane reference system
* to the reference system of the rotated pane.
*
* (mapPane) --> (rotatePane)
* (norotatePane) --> (rotatePane)
*
* @param {L.Point} point pixel screen coordinates
*/
mapPanePointToRotatedPoint: function(point) {
return L.point(point)
._subtract(this._getRotatePanePos())
.rotate(-this._bearing);
},
/**
* Returns geographical bounds visible in the current map view
*
* @TODO find out if map bounds calculated by `L.Map::getBounds()`
* function should match the `rotatePane` or `norotatePane` bounds
*
* @see https://github.com/fnicollet/Leaflet/issues/7
*
* @returns {L.LatLngBounds}
*/
getBounds: function() {
if (!this._rotate) {
return mapProto.getBounds.call(this);
}
// SEE: https://github.com/fnicollet/Leaflet/pull/22
//
// var bounds = this.getPixelBounds(),
// sw = this.unproject(bounds.getBottomLeft()),
// ne = this.unproject(bounds.getTopRight());
// return new LatLngBounds(sw, ne);
//
// LatLngBounds' constructor automatically
// extends the bounds to fit the passed points
var size = this.getSize();
return new L.LatLngBounds([
this.containerPointToLatLng([0, 0]), // topleft
this.containerPointToLatLng([size.x, 0]), // topright
this.containerPointToLatLng([size.x, size.y]), // bottomright
this.containerPointToLatLng([0, size.y]), // bottomleft
]);
},
/**
* Returns the bounds of the current map view in projected pixel
* coordinates (sometimes useful in layer and overlay implementations).
*
* @TODO find out if map bounds calculated by `L.Map::getPixelBounds()`
* function should match the `rotatePane` or `norotatePane` bounds
*
* @see https://github.com/fnicollet/Leaflet/issues/7
*
* @returns {L.Bounds}
*/
// getPixelBounds(center, zoom) {
// // const topLeftPoint = map.containerPointToLayerPoint(this._getTopLeftPoint());
// const topLeftPoint = this._getTopLeftPoint(center, zoom);
// return new L.Bounds(topLeftPoint, topLeftPoint.add(this.getSize()));
// },
/**
* Change map rotation
*
* @param {number} theta map degrees
*/
setBearing: function(theta) {
if (!L.Browser.any3d || !this._rotate) { return; }
var bearing = L.Util.wrapNum(theta, [0, 360]) * L.DomUtil.DEG_TO_RAD,
center = this._getPixelCenter(),
oldPos = this._getRotatePanePos().rotateFrom(-this._bearing, center),
newPos = oldPos.rotateFrom(bearing, center);
// CSS transform
L.DomUtil.setPosition(this._rotatePane, oldPos, bearing, center);
this._pivot = center;
this._bearing = bearing;
this._rotatePanePos = newPos;
this.fire('rotate');
},
/**
* Get current map rotation
*
* @returns {number} theta map degrees
*/
getBearing: function() {
return this._bearing * L.DomUtil.RAD_TO_DEG;
},
/**
* Creates a new [map pane](#map-pane) with the given name if it doesn't
* exist already, then returns it. The pane is created as a child of
* `container`, or as a child of the main map pane if not set.
*
* @param {String} name leaflet pane
* @param {HTMLElement} [container] parent element
* @returns {HTMLElement} pane container
*/
// createPane: function(name, container) {
// if (!this._rotate || name == 'mapPane') {
// return mapProto.createPane.call(this, name, container);
// }
// // init "rotatePane"
// if (!this._rotatePane) {
// // this._pivot = this.getSize().divideBy(2);
// this._rotatePane = mapProto.createPane.call(this, 'rotatePane', this._mapPane);
// L.DomUtil.setPosition(this._rotatePane, new L.Point(0, 0), this._bearing, this._pivot);
// }
// return mapProto.createPane.call(this, name, container || this._rotatePane);
// },
/**
* Panes are DOM elements used to control the ordering of layers on
* the map. You can access panes with [`map.getPane`](#map-getpane)
* or [`map.getPanes`](#map-getpanes) methods. New panes can be created
* with the [`map.createPane`](#map-createpane) method.
*
* Every map has the following default panes that differ only in zIndex:
*
* - mapPane [HTMLElement = 'auto'] - Pane that contains all other map panes
* - tilePane [HTMLElement = 2] - Pane for tile layers
* - overlayPane [HTMLElement = 4] - Pane for overlays like polylines and polygons
* - shadowPane [HTMLElement = 5] - Pane for overlay shadows (e.g. marker shadows)
* - markerPane [HTMLElement = 6] - Pane for marker icons
* - tooltipPane [HTMLElement = 650] - Pane for tooltips.
* - popupPane [HTMLElement = 700] - Pane for popups.
*/
_initPanes: function() {
var panes = this._panes = {};
this._paneRenderers = {};
this._mapPane = this.createPane('mapPane', this._container);
L.DomUtil.setPosition(this._mapPane, new L.Point(0, 0));
if (this._rotate) {
this._rotatePane = this.createPane('rotatePane', this._mapPane);
this._norotatePane = this.createPane('norotatePane', this._mapPane);
// rotatePane
this.createPane('tilePane', this._rotatePane);
this.createPane('overlayPane', this._rotatePane);
// norotatePane
this.createPane('shadowPane', this._norotatePane);
this.createPane('markerPane', this._norotatePane);
this.createPane('tooltipPane', this._norotatePane);
this.createPane('popupPane', this._norotatePane);
} else {
this.createPane('tilePane');
this.createPane('overlayPane');
this.createPane('shadowPane');
this.createPane('markerPane');
this.createPane('tooltipPane');
this.createPane('popupPane');
}
if (!this.options.markerZoomAnimation) {
L.DomUtil.addClass(panes.markerPane, 'leaflet-zoom-hide');
L.DomUtil.addClass(panes.shadowPane, 'leaflet-zoom-hide');
}
},
/**
* Pans the map the minimum amount to make the `latlng` visible. Use
* padding options to fit the display to more restricted bounds.
* If `latlng` is already within the (optionally padded) display bounds,
* the map will not be panned.
*
* @see https://github.com/Raruto/leaflet-rotate/issues/18
*
* @param {L.LatLng} latlng coordinates
* @param {Object} [options={}] padding options
*
* @returns {L.Map} current map instance
*/
panInside(latlng, options) {
options = options || {};
const paddingTL = L.point(options.paddingTopLeft || options.padding || [0, 0]),
paddingBR = L.point(options.paddingBottomRight || options.padding || [0, 0]),
/** @TODO use mapProto.panInside */
// pixelPoint = this.project(latlng),
// pixelBounds = this.getPixelBounds(),
// pixelCenter = this.project(this.getCenter()),
rect = this._container.getBoundingClientRect(),
pixelPoint = this.latLngToContainerPoint(latlng),
pixelBounds = L.bounds([ L.point(rect), L.point(rect).add(this.getSize()) ]),
pixelCenter = pixelBounds.getCenter(),
//
paddedBounds = L.bounds([pixelBounds.min.add(paddingTL), pixelBounds.max.subtract(paddingBR)]),
paddedSize = paddedBounds.getSize();
if (!paddedBounds.contains(pixelPoint)) {
this._enforcingBounds = true;
const centerOffset = pixelPoint.subtract(paddedBounds.getCenter());
const offset = paddedBounds.extend(pixelPoint).getSize().subtract(paddedSize);
pixelCenter.x += centerOffset.x < 0 ? -offset.x : offset.x;
pixelCenter.y += centerOffset.y < 0 ? -offset.y : offset.y;
/** @TODO use mapProto.panInside */
// this.panTo(this.unproject(pixelCenter), options);
this.panTo(this.containerPointToLatLng(pixelCenter), options);
//
this._enforcingBounds = false;
}
return this;
},
/**
* 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.
*
* @TODO check if map bounds calculated by `L.Map::panInsideBounds()`
* function should match the `rotatePane` or `norotatePane` bounds
*
* @see https://github.com/fnicollet/Leaflet/issues/7
*
* @param {L.LatLngBounds} bounds coordinates
* @param {Object} [options] pan options
* @returns {L.Map} current map instance
*/
// panInsideBounds: function (bounds, options) {
// this._enforcingBounds = true;
// var center = this.getCenter(),
// newCenter = this._limitCenter(center, this._zoom, L.latLngBounds(bounds));
// if (!center.equals(newCenter)) {
// this.panTo(newCenter, options);
// }
// this._enforcingBounds = false;
// return this;
// },
/**
* Layer point of the current center
*
* @returns {L.Point} layer center
*/
// _getCenterLayerPoint: function () {
// return this.containerPointToLayerPoint(this.getSize()._divideBy(2));
// },
/**
* Offset of the specified place to the current center in pixels
*
* @param {L.LatLng} latlng map coordinates
*/
_getCenterOffset: function(latlng) {
var centerOffset = mapProto._getCenterOffset.call(this, latlng);
if (this._rotate) {
centerOffset = centerOffset.rotate(this._bearing);
}
return centerOffset;
},
_getRotatePanePos: function() {
return this._rotatePanePos || new L.Point(0, 0);
// return L.DomUtil.getPosition(this._rotatePane) || new L.Point(0, 0);
},
_getNewPixelOrigin: function(center, zoom) {
var viewHalf = this.getSize()._divideBy(2);
if (!this._rotate) {
return mapProto._getNewPixelOrigin.call(this, center, zoom);
}
return this.project(center, zoom)
.rotate(this._bearing)
._subtract(viewHalf)
._add(this._getMapPanePos())
._add(this._getRotatePanePos())
.rotate(-this._bearing)
._round();
},
/**
* @since leaflet-rotate (v0.2)
*
* @see src\layer\tile\GridLayer::_getTiledPixelBounds()
*/
_getNewPixelBounds: function(center, zoom) {
center = center || this.getCenter();
zoom = zoom || this.getZoom();
if (!this._rotate && mapProto._getNewPixelBounds) {
return mapProto._getNewPixelBounds.apply(this, arguments);
}
var mapZoom = this._animatingZoom ? Math.max(this._animateToZoom, this.getZoom()) : this.getZoom(),
scale = this.getZoomScale(mapZoom, zoom),
pixelCenter = this.project(center, zoom).floor(),
size = this.getSize(),
halfSize = new L.Bounds([
this.containerPointToLayerPoint([0, 0]).floor(),
this.containerPointToLayerPoint([size.x, 0]).floor(),
this.containerPointToLayerPoint([0, size.y]).floor(),
this.containerPointToLayerPoint([size.x, size.y]).floor()
]).getSize().divideBy(scale * 2);
return new L.Bounds(pixelCenter.subtract(halfSize), pixelCenter.add(halfSize));
},
/**
* @since leaflet-rotate (v0.2)
*
* @return {L.Point} map pivot point (center)
*/
_getPixelCenter: function() {
if (!this._rotate && mapProto._getPixelCenter) {
return mapProto._getPixelCenter.apply(this, arguments);
}
return this.getSize()._divideBy(2)._subtract(this._getMapPanePos());
},
/**
* @since leaflet-rotate (v0.2)
*
* @see src\layer\vector\Renderer::_update()
*/
_getPaddedPixelBounds: function(padding) {
if (!this._rotate && mapProto._getPaddedPixelBounds) {
return mapProto._getPaddedPixelBounds.apply(this, arguments);
}
var p = padding,
size = this.getSize(),
padMin = size.multiplyBy(-p),
padMax = size.multiplyBy(1 + p);
//min = this.containerPointToLayerPoint(size.multiplyBy(-p)).round();
return new L.Bounds([
this.containerPointToLayerPoint([padMin.x, padMin.y]).floor(),
this.containerPointToLayerPoint([padMin.x, padMax.y]).floor(),
this.containerPointToLayerPoint([padMax.x, padMin.y]).floor(),
this.containerPointToLayerPoint([padMax.x, padMax.y]).floor()
]);
},
_handleGeolocationResponse: function(pos) {
if (!this._container._leaflet_id) { return; }
var lat = pos.coords.latitude,