2015-10-16 60 views
0

我有一個傳單滑塊與範圍,我想要有兩個元素來保存每個句柄的開始和結束日期,如here,任何想法如何做到這一點。 這是我試圖修改滑塊javascript文件如何獲得傳單滑塊的開始和結束

L.Control.SliderControl = L.Control.extend({ 
 
    options: { 
 
     position: 'topright', 
 
     layers: null, 
 
     timeAttribute: 'time', 
 
     isEpoch: false,  // whether the time attribute is seconds elapsed from epoch 
 
     startTimeIdx: 0, // where to start looking for a timestring 
 
     timeStrLength: 19, // the size of yyyy-mm-dd hh:mm:ss - if millis are present this will be larger 
 
     maxValue: -1, 
 
     minValue: 0, 
 
     showAllOnStart: false, 
 
     markers: null, 
 
     range: false, 
 
     follow: false, 
 
     alwaysShowDate : false, 
 
     rezoom: null 
 
    }, 
 

 
    initialize: function (options) { 
 
     L.Util.setOptions(this, options); 
 
     this._layer = this.options.layer; 
 

 
    }, 
 

 
    extractTimestamp: function(time, options) { 
 
     if (options.isEpoch) { 
 
      time = (new Date(parseInt(time))).toString(); // this is local time 
 
     } 
 
     return time.substr(options.startTimeIdx, options.startTimeIdx + options.timeStrLength); 
 
    }, 
 

 
    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('<center><div id="leaflet-slider" style="width:280px; box-shadow: rgba(0,0,10,1) 1px 2px 50px;background-color:2E2E1F" ><div class="ui-slider-handle"></div><div id="slider-timestamp" style="width:280px; margin-top:13px; background-color:#FFFFFF; text-align:center; border-radius:5px;"></div></div></center>'); 
 
\t \t //Prevent map panning/zooming while using the slider 
 
     $(sliderContainer).mousedown(function() { 
 
      map.dragging.disable(); 
 
     }); 
 
     $(document).mouseup(function() { 
 
      map.dragging.enable(); 
 
      //Hide the slider timestamp if not range and option alwaysShowDate is set on false 
 
      if (options.range || !options.alwaysShowDate) { 
 
       $('#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) { 
 
      var index_temp = 0; 
 
      this._layer.eachLayer(function (layer) { 
 
       options.markers[index_temp] = layer; 
 
       ++index_temp; 
 
      }); 
 
      options.maxValue = index_temp - 1; 
 
      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; 
 
     _extractTimestamp = this.extractTimestamp 
 
     var index_start = _options.minValue; 
 
     if(_options.showAllOnStart){ 
 
      index_start = _options.maxValue; 
 
      if(_options.range) _options.values = [_options.minValue,_options.maxValue]; 
 
      else _options.value = _options.maxValue; 
 
     } 
 
     $("#leaflet-slider").slider({ 
 
      range: _options.range, 
 
      value: _options.value, 
 
      values: _options.values, 
 
      min: _options.minValue, 
 
      max: _options.maxValue, 
 
      step: 1, 
 
      slide: function (e, ui) { 
 
       var map = _options.map; 
 
       var fg = L.featureGroup(); 
 
       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 !== undefined) { 
 
         if(_options.markers[ui.value].feature.properties[_options.f ]){ 
 
          if(_options.markers[ui.value]) $('#slider-timestamp').html(
 
           _extractTimestamp(_options.markers[ui.value].feature.properties[_options.timeAttribute], _options)); 
 
         }else { 
 
          console.error("Time property "+ _options.timeAttribute +" not found in data"); 
 
         } 
 
        }else { 
 
         // set by leaflet Vector Layers 
 
         if(_options.markers [ui.value].options[_options.timeAttribute]){ 
 
          if(_options.markers[ui.value]){ $('#slider-timestamp').html(
 
           _extractTimestamp(_options.markers[ui.value].options[_options.timeAttribute], _options)); 
 
\t \t \t \t \t \t \t \t $('#start').html($("#slider-timestamp").html()); 
 
\t \t \t \t \t \t \t $('#end').html(
 
           _extractTimestamp(_options.markers[ui.value].options[_options.timeAttribute], _options));} 
 
\t \t \t \t \t \t \t \t 
 
         }else { 
 
          console.error("Time property "+ _options.timeAttribute +" not found in data"); 
 
         } 
 
        } 
 
        
 
        var i; 
 
        // clear markers 
 
        for (i = _options.minValue; i <= _options.maxValue; i++) { 
 
         if(_options.markers[i]) map.removeLayer(_options.markers[i]); 
 
        } 
 
        if(_options.range){ 
 
         // jquery ui using range 
 
         for (i = ui.values[0]; i <= ui.values[1]; i++){ 
 
          if(_options.markers[i]) { 
 
           map.addLayer(_options.markers[i]); 
 
           fg.addLayer(_options.markers[i]); 
 
          } 
 
         } 
 
        }else if(_options.follow){ 
 
         for (i = ui.value - _options.follow + 1; i <= ui.value ; i++) { 
 
          if(_options.markers[i]) { 
 
           map.addLayer(_options.markers[i]); 
 
           fg.addLayer(_options.markers[i]); 
 
          } 
 
         } 
 
        }else{ 
 
         for (i = _options.minValue; i <= ui.value ; i++) { 
 
          if(_options.markers[i]) { 
 
           map.addLayer(_options.markers[i]); 
 
           fg.addLayer(_options.markers[i]); 
 
          } 
 
         } 
 
        } 
 
       }; 
 
       if(_options.rezoom) { 
 
        map.fitBounds(fg.getBounds(), { 
 
         maxZoom: _options.rezoom 
 
        }); 
 
       } 
 
      } 
 
     }); 
 
     if (!_options.range && _options.alwaysShowDate) { 
 
      $('#slider-timestamp').html(_extractTimeStamp(_options.markers[index_start].feature.properties[_options.timeAttribute], _options)); 
 
     } 
 
     for (i = _options.minValue; i <= index_start; i++) { 
 
      _options.map.addLayer(_options.markers[i]); 
 
     } 
 
\t \t 
 
    } 
 
}); 
 

 
L.control.sliderControl = function (options) { 
 
\t $('span').show(); 
 
    return new L.Control.SliderControl(options); 
 
};
最初的劇本是提前

回答

1

通過該示例頁面的代碼挖Dwilhelm89's slider 感謝給出了他們是如何的一些想法達到你正在尋找的效果,所以這個答案大部分是他們在那裏完成的粗糙副本。

您需要做的主要事情是在sliderContainer中創建一些可放置標籤的div。如果您在創建滑塊DIV的同時做到這一點,就應該是這樣的:

$(sliderContainer).append('<div id="slider-min"></div><div id="leaflet-slider" style="width:280px; inline-block; margin: 0 5px 0 5px;"></div><div id="slider-max"></div><div id="slider-current"><span class="start-time"></span>-<span class="end-time"></span></div>');

其中slider-minslider-max分別爲開頭的div標籤和滑塊,結束時, slider-current是範圍標籤(start-timeend-time)的div,隨着您移動滑塊而變化。

填充slider-minslider-max可以使用onAdd功能,它看起來像這樣內完成:

$('#slider-min', sliderContainer).html(this.options.markers[this.options.minValue].feature.properties[this.options.timeAttribute]); 
$('#slider-max', sliderContainer).html(this.options.markers[this.options.maxValue].feature.properties[this.options.timeAttribute]); 

初始化start-timeend-time值也可以內使用onAdd做,而是因爲他們將需要更新,給他們先分配一些名字,並用功能填充它們是有用的:

this.$currentStartDiv = $('#slider-current .start-time', sliderContainer); 
this.$currentEndDiv = $('#slider-current .end-time', sliderContainer); 
this._updateCurrentDiv(0,0); 

其中_updateCurrentDiv以L的主要級別定義eafletSlider功能(即使用onAdd外):

_updateCurrentDiv: function (startIdx, endIdx) { 
this.$currentStartDiv.html(this.options.markers[startIdx].feature.properties[this.options.timeAttribute]); 
     this.$currentEndDiv.html(this.options.markers[endIdx].feature.properties[this.options.timeAttribute]); 
}, 

對於動態變化的標籤,這個功能可以在幻燈片功能中被稱爲,但對於範圍界定,我不完全理解的原因,this需要重新 - 分配給不同的變量(即首先是self = this)。在滑塊功能,_updateCurrentDiv會被稱爲是這樣的:

if (_options.range) { 
    // jquery ui using range 
    for (i = ui.values[0]; i <= ui.values[1]; i++) { 
     if (_options.markers[i]) { 
      map.addLayer(_options.markers[i]); 
      fg.addLayer(_options.markers[i]); 
     } 
    } 
    self._updateCurrentDiv(ui.values[0], ui.values[1]); 
} 

這幾乎覆蓋了所有的功能。美學可以用CSS來處理,兩個主要的事情是滑塊和最小和最大的div(如果你想所有這些整齊排列)應該被設置爲display: inline-block,以便它們不會碰撞到單獨的行和slider-current div(如果您想要白色控制框邊界外的動態標籤)的樣式應該爲position: absoluteleft: 420px(或任何適合您需要的位移)。

的這一切工作的例子在這個小提琴拼湊起來:

http://jsfiddle.net/nathansnider/mos9Lr5v/

+0

我有這個問題,而試圖修改爲您的代碼:遺漏的類型錯誤:無法未定義讀取屬性「屬性」 – Tenz

相關問題