Marker.Drag
export var MarkerDrag = Handler.extend({
initialize: function (marker) {
this._marker = marker;
},
addHooks: function () {
var icon = this._marker._icon;
if (!this._draggable) {
this._draggable = new Draggable(icon, icon, true);
}
this._draggable
.on(
{
dragstart: this._onDragStart,
predrag: this._onPreDrag,
drag: this._onDrag,
dragend: this._onDragEnd,
},
this
)
.enable();
DomUtil.addClass(icon, "leaflet-marker-draggable");
},
removeHooks: function () {
this._draggable
.off(
{
dragstart: this._onDragStart,
predrag: this._onPreDrag,
drag: this._onDrag,
dragend: this._onDragEnd,
},
this
)
.disable();
if (this._marker._icon) {
DomUtil.removeClass(this._marker._icon, "leaflet-marker-draggable");
}
},
moved: function () {
return this._draggable && this._draggable._moved;
},
_adjustPan: function (e) {
var marker = this._marker,
map = marker._map,
speed = this._marker.options.autoPanSpeed,
padding = this._marker.options.autoPanPadding,
iconPos = DomUtil.getPosition(marker._icon),
bounds = map.getPixelBounds(),
origin = map.getPixelOrigin();
var panBounds = toBounds(
bounds.min._subtract(origin).add(padding),
bounds.max._subtract(origin).subtract(padding)
);
if (!panBounds.contains(iconPos)) {
// Compute incremental movement
var movement = toPoint(
(Math.max(panBounds.max.x, iconPos.x) - panBounds.max.x) /
(bounds.max.x - panBounds.max.x) -
(Math.min(panBounds.min.x, iconPos.x) - panBounds.min.x) /
(bounds.min.x - panBounds.min.x),
(Math.max(panBounds.max.y, iconPos.y) - panBounds.max.y) /
(bounds.max.y - panBounds.max.y) -
(Math.min(panBounds.min.y, iconPos.y) - panBounds.min.y) /
(bounds.min.y - panBounds.min.y)
).multiplyBy(speed);
map.panBy(movement, { animate: false });
this._draggable._newPos._add(movement);
this._draggable._startPos._add(movement);
DomUtil.setPosition(marker._icon, this._draggable._newPos);
this._onDrag(e);
this._panRequest = requestAnimFrame(this._adjustPan.bind(this, e));
}
},
_onDragStart: function () {
this._oldLatLng = this._marker.getLatLng();
this._marker.closePopup && this._marker.closePopup();
this._marker.fire("movestart").fire("dragstart");
},
_onPreDrag: function (e) {
if (this._marker.options.autoPan) {
cancelAnimFrame(this._panRequest);
this._panRequest = requestAnimFrame(this._adjustPan.bind(this, e));
}
},
_onDrag: function (e) {
var marker = this._marker,
shadow = marker._shadow,
iconPos = DomUtil.getPosition(marker._icon),
latlng = marker._map.layerPointToLatLng(iconPos);
if (shadow) {
DomUtil.setPosition(shadow, iconPos);
}
marker._latlng = latlng;
e.latlng - latlng;
e.oldLatLng = this._oldLatLng;
marker.fire("move", e).fire("drag", e);
},
_onDragEnd: function (e) {
cancelAnimFrame(this._panRequest);
delete this._oldLatLng;
this._marker.fire("moveend").fire("dragend", e);
},
});
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
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
编辑 (opens new window)
上次更新: 2025/04/11, 08:35:36