forked from dwilhelm89/LeafletSlider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSliderControl.js
117 lines (105 loc) · 4.32 KB
/
SliderControl.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
L.Control.SliderControl = L.Control.extend({
options: {
position: 'topright',
layers: null,
maxValue: -1,
minValue: -1,
markers: null,
range: false
},
initialize: function (options) {
L.Util.setOptions(this, options);
this._layer = this.options.layer;
},
setPosition: function (position) {
var map = this._map;
if (map) {
map.removeControl(this);
}
this.options.position = position;
if (map) {
map.addControl(this);
}
this.startSlider();
return this;
},
onAdd: function (map) {
this.options.map = map;
// Create a control sliderContainer with a jquery ui slider
var sliderContainer = L.DomUtil.create('div', 'slider', this._container);
$(sliderContainer).append('<div id="leaflet-slider" style="width:200px"><div class="ui-slider-handle"></div><div id="slider-timestamp" style="width:200px; margin-top:10px;background-color:#FFFFFF"></div></div>');
//Prevent map panning/zooming while using the slider
$(sliderContainer).mousedown(function () {
map.dragging.disable();
});
$(document).mouseup(function () {
map.dragging.enable();
//Only show the slider timestamp while using the slider
$('#slider-timestamp').html('');
});
var options = this.options;
this.options.markers = [];
//If a layer has been provided: calculate the min and max values for the slider
if (this._layer) {
this._layer.eachLayer(function (layer) {
if (options.minValue === -1) {
options.minValue = layer._leaflet_id;
}
options.maxValue = layer._leaflet_id;
options.markers[layer._leaflet_id] = layer;
});
this.options = options;
}
else {
console.log("Error: You have to specify a layer via new SliderControl({layer: your_layer});");
}
return sliderContainer;
},
onRemove: function (map) {
//Delete all markers which where added via the slider and remove the slider div
for (i = this.options.minValue; i < this.options.maxValue; i++) {
map.removeLayer(this.options.markers[i]);
}
$('#leaflet-slider').remove();
},
startSlider: function () {
options = this.options;
$("#leaflet-slider").slider({
range: options.range,
value: options.minValue + 1,
min: options.minValue,
max: options.maxValue +1,
step: 1,
slide: function (e, ui) {
if(!!options.markers[ui.value]) {
//If there is no time property, this line has to be removed (or exchanged with a different property)
if(options.markers[ui.value].feature.properties.time){
if(options.markers[ui.value]) $('#slider-timestamp').html(options.markers[ui.value].feature.properties.time.substr(0, 19));
}
if(options.range){
for (var i = ui.values[0]; i< ui.values[1]; i++){
if(options.markers[i]) map.addLayer(options.markers[i]);
}
for (var i = options.maxValue; i > ui.values[1]; i--) {
if(options.markers[i]) map.removeLayer(options.markers[i]);
}
for (var i = options.minValue; i < ui.values[0]; i++) {
if(options.markers[i]) map.removeLayer(options.markers[i]);
}
}else{
for (var i = options.minValue; i < ui.value ; i++) {
if(options.markers[i]) map.addLayer(options.markers[i]);
}
for (var i = ui.value; i <= options.maxValue; i++) {
if(options.markers[i]) map.removeLayer(options.markers[i]);
}
}
}
}
});
map.addLayer(options.markers[options.minValue]);
}
});
L.control.sliderControl = function (options) {
return new L.Control.SliderControl(options);
};