1
|
|
|
/* |
2
|
|
|
* this code based on https://openlayers.org/en/latest/examples/custom-interactions.html |
3
|
|
|
*/ |
4
|
|
|
|
5
|
|
|
/** global: ol */ |
6
|
|
|
/** global: tmpLayer */ |
7
|
|
|
|
8
|
|
|
window.app = {}; |
9
|
|
|
var app = window.app; |
10
|
|
|
app.Drag = function () { |
11
|
|
|
ol.interaction.Pointer.call(this, { |
12
|
|
|
handleDownEvent: handleDownEvent, |
13
|
|
|
handleDragEvent: handleDragEvent, |
14
|
|
|
handleMoveEvent: handleMoveEvent, |
15
|
|
|
handleUpEvent: handleUpEvent |
16
|
|
|
}); |
17
|
|
|
|
18
|
|
|
this.coordinate_ = null; |
19
|
|
|
this.cursor_ = 'pointer'; |
20
|
|
|
this.feature_ = null; |
21
|
|
|
this.previousCursor_ = undefined; |
22
|
|
|
}; |
23
|
|
|
|
24
|
|
|
ol.inherits(app.Drag, ol.interaction.Pointer); |
25
|
|
|
|
26
|
|
|
function handleDownEvent(evt) { |
27
|
|
|
var map = evt.map; |
28
|
|
|
var feature = map.forEachFeatureAtPixel(evt.pixel, |
29
|
|
|
basicFeatureHandler, |
30
|
|
|
{layerFilter: layerFilter} |
31
|
|
|
); |
32
|
|
|
if (feature) { |
33
|
|
|
this.coordinate_ = evt.coordinate; |
34
|
|
|
this.feature_ = feature; |
35
|
|
|
} |
36
|
|
|
return !!feature; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
function handleDragEvent(evt) { |
40
|
|
|
var map = evt.map; |
41
|
|
|
var feature = map.forEachFeatureAtPixel(evt.pixel, |
|
|
|
|
42
|
|
|
basicFeatureHandler, |
43
|
|
|
{layerFilter: layerFilter}); |
44
|
|
|
|
45
|
|
|
var deltaX = evt.coordinate[0] - this.coordinate_[0]; |
46
|
|
|
var deltaY = evt.coordinate[1] - this.coordinate_[1]; |
47
|
|
|
|
48
|
|
|
var geometry = /** @type {ol.geom.SimpleGeometry} */ |
49
|
|
|
(this.feature_.getGeometry()); |
50
|
|
|
geometry.translate(deltaX, deltaY); |
51
|
|
|
|
52
|
|
|
this.coordinate_[0] = evt.coordinate[0]; |
53
|
|
|
this.coordinate_[1] = evt.coordinate[1]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
function handleMoveEvent(evt) { |
57
|
|
|
if (this.cursor_) { |
58
|
|
|
var map = evt.map; |
59
|
|
|
var feature = map.forEachFeatureAtPixel(evt.pixel, |
60
|
|
|
basicFeatureHandler, |
61
|
|
|
{layerFilter: layerFilter}); |
62
|
|
|
var element = evt.map.getTargetElement(); |
63
|
|
|
if (feature) { |
64
|
|
|
if (element.style.cursor != this.cursor_) { |
65
|
|
|
this.previousCursor_ = element.style.cursor; |
66
|
|
|
element.style.cursor = this.cursor_; |
67
|
|
|
} |
68
|
|
|
} else if (this.previousCursor_ !== undefined) { |
69
|
|
|
element.style.cursor = this.previousCursor_; |
70
|
|
|
this.previousCursor_ = undefined; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
function handleUpEvent(evt) { |
76
|
|
|
setTmpPointer(evt.coordinate); |
77
|
|
|
this.coordinate_ = null; |
78
|
|
|
this.feature_ = null; |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
function layerFilter(layer) { |
83
|
|
|
if (layer === tmpLayer) { |
84
|
|
|
return(true); |
85
|
|
|
} |
86
|
|
|
return(false); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
function basicFeatureHandler(feature, layer) { |
|
|
|
|
90
|
|
|
return(feature); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
|