Openlayers2卷帘功能的实现

五十二、openlayers官网示例Layer Swipe解析——绘制卷帘效果地图、动态裁剪图层 卷帘效果绘制的过程和绘制圆形裁剪区的代码类似,卷帘是以滑块的值为界线将左边的图像进行裁剪,而且绘制的时候要先使用getRenderPixel把地理坐标转换为屏幕坐标。这时发现圆形只绘制出来了一小部分,这是因为绘制剪切圆形路径之后,将只能在圆形上绘制图像,如果绘制的图形超出了圆形范围,将不会显示出来。所谓的卷帘效果实际上是在地图上添加了两个不同的图层,通过滑块滑动实时剪裁上层图层的图像,漏出下层图层。这也是为什么我们的卷帘裁剪是在 prerender(图层渲染前)进行,而不是图层渲染后或其他时机。 阅读详情

概述:

在WebGIS开发中,经常会有用户提需求,要实现卷帘功能,卷帘功能主要是实现两张图之间的对比。在前文中,讲到了openlayers3以及Arcgis for js中卷帘的实现,在本文讲述如何在openlayers2中实现卷帘功能。

 

结果展示:

 

实现代码:

在此,扩展了一个名为Swipe的Control,Swipe的代码如下:(猛击)

  1. /* 
  2.  * 
  3.  * @requires OpenLayers/Control.js 
  4.  */  
  5.   
  6. /* The following globals are for JSLint */  
  7. /* jslint browser: true, vars: true */  
  8. /* global OpenLayers, escape */  
  9.   
  10. /** api: (define) 
  11.  *  module =  OpenLayers.Control 
  12.  *  class = Swipe 
  13.  *  base_link = `OpenLayers.Control <http://dev.openlayers.org/apidocs/files/OpenLayers/Control-js.html>`_ 
  14.  */  
  15.   
  16. /** api: example 
  17.  *  Sample code to add a swipe control(猛击) 
  18.  * 
  19.  *  .. code-block:: javascript 
  20.  * 
  21.  *     var map = new new OpenLayers.Map("mymap"); 
  22.  *     var Swipe = new OpenLayers.Control.Swipe({map: map}); 
  23.  *     map.addControls([swipe]); 
  24.  *     Swipe.activate(); 
  25.  * 
  26.  */  
  27.   
  28. /** api: constructor 
  29.  *  .. class:: Swipe(options) 
  30.  * 
  31.  *  :param options: ``Object`` options 
  32.  * 
  33.  *  :return:  ``OpenLayers.Control.Swipe`` (猛击)
  34.  * 
  35.  *  Add a swipe control in the map 
  36.  */  
  37. OpenLayers.Control.Swipe = OpenLayers.Class(OpenLayers.Control, {  
  38.   
  39.     /** api: config[map] 
  40.      *  ``OpenLayers.Map`` 
  41.      *  A `OpenLayers.Map <http://dev.openlayers.org/docs/files/OpenLayers/Map-js.html>`_ instance 
  42.      */  
  43.     map: null,  
  44.   
  45.     width: 32,  
  46.   
  47.     /** api: config[swipeRatio] 
  48.      *  ``Number`` 
  49.      *  A number between 0 and 1 defining the position of the swipe relative to the map (from right to left) 
  50.      */  
  51.     swipeRatio: null,  
  52.   
  53.     swipeLayer: null,  
  54.   
  55.     isTitleVisible: false,  
  56.   
  57.     isDragging: false,  
  58.   
  59.     mouseDragStart: null,  
  60.   
  61.   
  62.     /** 
  63.      * Property: divEvents 
  64.      * {<OpenLayers.Events>} 
  65.      */  
  66.     divEvents: null,  
  67.   
  68.     initialize: function (options) {  
  69.         "use strict";  
  70.         OpenLayers.Control.prototype.initialize.apply(  
  71.             this,  
  72.             arguments  
  73.         );  
  74.         // Manage position of swipe  (猛击)
  75.         if (this.map && this.map.swipeRatio) {  
  76.             // Swipe ratio can be set in the map (in order to manage permalink)  
  77.             this.setSwipeRatio(this.map.swipeRatio);  
  78.         } else {  
  79.             if (!this.swipeRatio) {  
  80.                 // Default swipe ratio is 0.5  
  81.                 this.setSwipeRatio(0.5);  
  82.             } else {  
  83.                 // Swipe ratio can be set to the control  
  84.                 this.setSwipeRatio(this.swipeRatio);  
  85.             }  
  86.         }  
  87.     },  
  88.   
  89.     /** 
  90.      * Method: activate 
  91.      * Activates the control. 
  92.      * 
  93.      * Returns: 
  94.      * {Boolean} The control was effectively activated. 
  95.      */  
  96.     activate: function () {  
  97.         this.map.swipeActive = true;  
  98.         this.map.events.triggerEvent("changelayer", {  
  99.             layer: this.swipeLayer,  
  100.             property: "name"  
  101.         });  
  102.         OpenLayers.Control.prototype.activate.apply(this, arguments);  
  103.         this.map.events.on({  
  104.             "addlayer": this.handleAddLayer,  
  105.             "removelayer": this.handleRemoveLayer,  
  106.             "changelayer": this.handleChangeLayer,  
  107.             "updatesize": this.handleUpdateSize,  
  108.             "move": this.handleMove,  
  109.             "scope": this  
  110.         });  
  111.   
  112.         if (this.isLayersInLayerSwitcher()) {  
  113.             this.div.style.display = 'block';  
  114.             this.viewBigArrow();  
  115.         }  
  116.         this.resize();  
  117.   
  118.         return true;  
  119.     },  
  120.   
  121.     /** 
  122.      * Method: deactivate (无效)
  123.      * Deactivates the control. 
  124.      * 
  125.      * Returns: 
  126.      * {Boolean} The control was effectively deactivated. 
  127.      */  
  128.     deactivate: function () {  
  129.         this.map.swipeActive = false;  
  130.         this.map.events.triggerEvent("changelayer", {  
  131.             layer: this.swipeLayer,  
  132.             property: "name"  
  133.         });  
  134.         this.map.events.un({  
  135.             "addlayer": this.handleAddLayer,  
  136.             "removelayer": this.handleRemoveLayer,  
  137.             "changelayer": this.handleChangeLayer,  
  138.             "updatesize": this.handleUpdateSize,  
  139.             "move": this.handleMove,  
  140.             "scope": this  
  141.         });  
  142.         this.hideBigArrow();  
  143.         this.hideLayerTitle();  
  144.         this.div.style.display = 'none';  
  145.         if (this.swipeLayer) {  
  146.             if (this.swipeLayer.layers) {  
  147.                 for (var i = this.swipeLayer.layers.length - 1; i >= 0; i--) {  
  148.                     var layer = this.swipeLayer.layers[i];  
  149.                     if (layer.div) {  
  150.                         layer.div.style.clip = 'auto';  
  151.                     }  
  152.                 }  
  153.             } else {  
  154.                 this.swipeLayer.div.style.clip = 'auto';  
  155.             }  
  156.             this.swipeLayer = null;  
  157.         }  
  158.   
  159.         return OpenLayers.Control.prototype.deactivate.(无效)apply(  
  160.             this, arguments  
  161.         );  
  162.     },  
  163.   
  164.     /** 
  165.      * Method: destroy 
  166.      * Destroy control. 
  167.      */  
  168.     destroy: function() {  
  169.         this.map.events.un({  
  170.             "addlayer": this.handleAddLayer,  
  171.             "removelayer": this.handleRemoveLayer,  
  172.             "changelayer": this.handleChangeLayer,  
  173.             "updatesize": this.handleUpdateSize,  
  174.             "scope": this  
  175.         });  
  176.         OpenLayers.Control.prototype.destroy.apply(this, arguments);  
  177.     },  
  178.   
  179.     /** 
  180.      * Method: draw 
  181.      * Initialize control. 
  182.      * 
  183.      * Returns: 
  184.      * {DOMElement} A reference to the DIV DOMElement containing the control 
  185.      */  
  186.     draw: function() {  
  187.         OpenLayers.Control.prototype.draw.apply(this, arguments);  
  188.   
  189.         this.elementLayer = document.createElement("div");  
  190.         this.div.appendChild(this.elementLayer);  
  191.         OpenLayers.Element.addClass(  
  192.             this.elementLayer,  
  193.             'olControlSwipeLayerHide'  
  194.         );  
  195.         this.elementLayerSpan = document.createElement("span");  
  196.         this.div.appendChild(this.elementLayerSpan);  
  197.         OpenLayers.Element.addClass(  
  198.             this.elementLayerSpan,  
  199.             'olControlSwipeLayerSpan'  
  200.         );  
  201.         this.elementLeft = document.createElement("div");  
  202.         this.div.appendChild(this.elementLeft);  
  203.         OpenLayers.Element.addClass(  
  204.             this.elementLeft,  
  205.             'olControlArrowLeft'  
  206.         );  
  207.   
  208.         this.elementRight = document.createElement("div");  
  209.         this.div.appendChild(this.elementRight);  
  210.         OpenLayers.Element.addClass(  
  211.             this.elementRight,  
  212.             'olControlArrowRight'  
  213.         );  
  214.   
  215.         OpenLayers.Control.prototype.draw.apply(this, arguments);  
  216.   
  217.         this.divEvents = new OpenLayers.Events(this, this.div, null, true, {includeXY: true});  
  218.   
  219.         this.divEvents.on({  
  220.             "touchstart": this.divDown,  
  221.             "touchmove": this.divDrag,  
  222.             "touchend": this.divUp,  
  223.             "mousedown": this.divDown,  
  224.             "mousemove": this.divDrag,  
  225.             "mouseup": this.divUp,  
  226.             "mouseover": this.divMouseOver,  
  227.             "mouseout": this.divMouseOut,  
  228.             scope: this  
  229.         });  
  230.   
  231.         return this.div;  
  232.     },  
  233.   
  234.     /* 
  235.      * Method: divMouseOver 
  236.      * event listener for onmouseover event 
  237.      * 
  238.      * Parameters: 
  239.      * evt - {<OpenLayers.Event>} 
  240.      */  
  241.     divMouseOver: function(ev) {  
  242.         OpenLayers.Element.addClass(  
  243.             this.div,  
  244.             'olControlSwipeHover'  
  245.         );  
  246.         //this.viewLayerTitle();  
  247.     },  
  248.   
  249.     hideBigArrow: function() {  
  250.         if (!this.isDragging) {  
  251.             this.elementLeft.style.display = "none";  
  252.             this.elementRight.style.display = "none";  
  253.         }  
  254.     },  
  255.   
  256.     viewBigArrow: function() {  
  257.         if (!this.isDragging) {  
  258.             this.elementLeft.style.display = "block";  
  259.             this.elementRight.style.display = "block";  
  260.         }  
  261.     },  
  262.   
  263.     /* 
  264.      * Method: divMouseOut 
  265.      * event listener for onmouseout event 
  266.      * 
  267.      * Parameters: 
  268.      * evt - {<OpenLayers.Event>} 
  269.      */  
  270.     divMouseOut: function(ev) {  
  271.         OpenLayers.Element.removeClass(  
  272.             this.div,  
  273.             'olControlSwipeHover'  
  274.         );  
  275.         this.hideLayerTitle();  
  276.         this.viewBigArrow();  
  277.     },  
  278.   
  279.     /** 
  280.      * Method: passEventToDiv 
  281.      * This function is used to pass events that happen on the map, 
  282.      * through to the div, which then does its moving thing. 
  283.      * 
  284.      * Parameters: 
  285.      * evt - {<OpenLayers.Event>} 
  286.      */  
  287.     passEventToDiv:function(evt) {  
  288.         this.divEvents.handleBrowserEvent(evt);  
  289.     },  
  290.   
  291.     /* 
  292.      * Method: divDown 
  293.      * event listener for clicks on the div 
  294.      * 
  295.      * Parameters: 
  296.      * evt - {<OpenLayers.Event>} 
  297.      */  
  298.     divDown:function(evt) {  
  299.         if (!OpenLayers.Event.isLeftClick(evt) && !OpenLayers.Event.isSingleTouch(evt)) {  
  300.             return;  
  301.         }  
  302.         this.map.events.on({  
  303.             "touchmove": this.passEventToDiv,  
  304.             "mousemove": this.passEventToDiv,  
  305.             "mouseup": this.passEventToDiv,  
  306.             scope: this  
  307.         });  
  308.         this.mouseDragStart = evt.xy.clone();  
  309.         OpenLayers.Event.stop(evt);  
  310.         //this.viewLayerTitle();  
  311.         this.hideBigArrow();  
  312.         this.isDragging = true;  
  313.         return false;  
  314.     },  
  315.   
  316.     /* 
  317.      * Method: divDrag 
  318.      * This is what happens when a click has occurred, and the client is 
  319.      * dragging.  Here we must ensure that the div doesn't go beyond the 
  320.      * bottom/top of the zoombar div, as well as moving the div to its new 
  321.      * visual location 
  322.      * 
  323.      * Parameters: 
  324.      * evt - {<OpenLayers.Event>} 
  325.      */  
  326.     divDrag:function(evt) {  
  327.         if (this.mouseDragStart && this.isDragging) {  
  328.             var deltaX = this.mouseDragStart.x - evt.xy.x;  
  329.             var left = parseInt(this.div.style.left, 10);  
  330.             if ((left - deltaX) >= 0 &&  
  331.                 (left - deltaX) <= (this.map.size.w - this.width)) {  
  332.                 var delta = 0;  
  333.                 if (OpenLayers.BROWSER_NAME === "msie" || OpenLayers.BROWSER_NAME === "safari") {  
  334.                     delta = -1;  
  335.                 }  
  336.                 this.setSwipeRatio((left - deltaX) / (this.map.size.w - this.width + delta));  
  337.                 this.moveTo(this.computePosition());  
  338.                 this.clipFirstLayer();  
  339.                 this.mouseDragStart = evt.xy.clone();  
  340.             }  
  341.             OpenLayers.Event.stop(evt);  
  342.         }  
  343.         return false;  
  344.     },  
  345.   
  346.     /* 
  347.      * Method: divUp 
  348.      * Perform cleanup when a mouseup event is received 
  349.      * 
  350.      * Parameters: 
  351.      * evt - {<OpenLayers.Event>} 
  352.      */  
  353.     divUp:function(evt) {  
  354.         this.map.events.un({  
  355.             "touchmove": this.passEventToDiv,  
  356.             "mousemove": this.passEventToDiv,  
  357.             "mouseup": this.passEventToDiv,  
  358.             scope: this  
  359.         });  
  360.         if (!OpenLayers.Event.isLeftClick(evt) && evt.type !== "touchend") {  
  361.             return;  
  362.         }  
  363.         if (this.mouseDragStart) {  
  364.             this.mouseDragStart = null;  
  365.         }  
  366.         this.isDragging = false;  
  367.         this.viewBigArrow();  
  368.         if (evt.type === "touchend") {  
  369.             this.hideLayerTitle();  
  370.         }  
  371.         OpenLayers.Event.stop(evt);  
  372.         return false;  
  373.     },  
  374.   
  375.     /* 
  376.      * Method: clipFirstLayer 
  377.      * Clip the first layer present in the layer switcher 
  378.      */  
  379.     clipFirstLayer: function() {  
  380.         var newFirstLayer = this.getFirstLayerInLayerSwitcher();  
  381.         if (this.swipeLayer) {  
  382.             if (newFirstLayer.id !== this.swipeLayer.id) {  
  383.                 if (this.swipeLayer.layers) {  
  384.                     for (var i = this.swipeLayer.layers.length - 1; i >= 0; i--) {  
  385.                         var layer = this.swipeLayer.layers[i];  
  386.                         if (layer.div) {  
  387.                             layer.div.style.clip = 'auto';  
  388.                         }  
  389.                     }  
  390.                 } else {  
  391.                     this.swipeLayer.div.style.clip = 'auto';  
  392.                 }  
  393.             }  
  394.         }  
  395.   
  396.         if (newFirstLayer) {  
  397.             var width = this.map.getCurrentSize().w;  
  398.             var height = this.map.getCurrentSize().h;  
  399.             // slider position in pixels  
  400.             var s = parseInt(width * this.getSwipeRatio() * ((this.map.getCurrentSize().w - this.width) / this.map.getCurrentSize().w), 10);  
  401.             // cliping rectangle  
  402.             var top = -this.map.layerContainerOriginPx.y;  
  403.             var bottom = top + height;  
  404.             var left = -this.map.layerContainerOriginPx.x;  
  405.             var right = left + s + Math.ceil((this.width - 1) / 2);  
  406.             //Syntax for clip "rect(top,right,bottom,left)"  
  407.             var clip = "rect(" + top + "px " + right + "px " + bottom + "px " + left + "px)";  
  408.             this.swipeLayer = newFirstLayer;  
  409.             if (this.swipeLayer.layers) {  
  410.                 for (var i = this.swipeLayer.layers.length - 1; i >= 0; i--) {  
  411.                     var layer = this.swipeLayer.layers[i];  
  412.                     if (layer.div) {  
  413.                         layer.div.style.clip = clip;  
  414.                     }  
  415.                 }  
  416.             } else {  
  417.                 this.swipeLayer.div.style.clip = clip;  
  418.             }  
  419.         }  
  420.   
  421.     },  
  422.   
  423.     /* 
  424.      * Method: handleAddLayer 
  425.      * Triggered when a new layer is added 
  426.      * 
  427.      * Parameters: 
  428.      * object - {<OpenLayers.Event>} 
  429.      */  
  430.     handleAddLayer: function (object) {  
  431.         if (this.isLayersInLayerSwitcher()) {  
  432.             this.div.style.display = 'block';  
  433.             this.moveTo(this.computePosition());  
  434.             this.clipFirstLayer();  
  435.         } else {  
  436.             this.div.style.display = 'none';  
  437.             this.swipeLayer = null;  
  438.         }  
  439.     },  
  440.   
  441.     viewLayerTitle: function() {  
  442.         if (!this.isTitleVisible && !this.isDragging) {  
  443.             if (this.swipeLayer) {  
  444.                 var content = "     " + this.swipeLayer.name;  
  445.                 this.elementLayer.innerHTML = content;  
  446.                 this.elementLayerSpan.innerHTML = content;  
  447.                 OpenLayers.Element.addClass(  
  448.                     this.elementLayer,  
  449.                     'olControlSwipeLayerView'  
  450.                 );  
  451.                 OpenLayers.Element.removeClass(  
  452.                     this.elementLayer,  
  453.                     'olControlSwipeLayerHide'  
  454.                 );  
  455.                 var width = parseInt(this.elementLayerSpan.offsetWidth) + 5;  
  456.                 this.elementLayer.style.width = width + "px";  
  457.                 this.elementLayer.style.marginLeft = "-" + width + "px";  
  458.   
  459.             }  
  460.   
  461.         }  
  462.         this.isTitleVisible = true;  
  463.     },  
  464.   
  465.     hideLayerTitle: function() {  
  466.         if (!this.isDragging) {  
  467.             this.elementLayer.innerHTML = '';  
  468.             this.isTitleVisible = false;  
  469.             OpenLayers.Element.addClass(  
  470.                 this.elementLayer,  
  471.                 'olControlSwipeLayerHide'  
  472.             );  
  473.             OpenLayers.Element.removeClass(  
  474.                 this.elementLayer,  
  475.                 'olControlSwipeLayerView'  
  476.             );  
  477.         }  
  478.     },  
  479.   
  480.     /* 
  481.      * Method: handleRemoveLayer 
  482.      * Triggered when a new layer is removed 
  483.      * 
  484.      * Parameters: 
  485.      * object - {<OpenLayers.Event>} 
  486.      */  
  487.     handleRemoveLayer: function (object) {  
  488.         if (this.isLayersInLayerSwitcher()) {  
  489.             this.div.style.display = 'block';  
  490.             this.moveTo(this.computePosition());  
  491.             this.clipFirstLayer();  
  492.         } else {  
  493.             this.div.style.display = 'none';  
  494.             this.swipeLayer = null;  
  495.         }  
  496.     },  
  497.   
  498.     /* 
  499.      * Method: handleChangeLayer 
  500.      * Triggered when the layer order is changed 
  501.      * 
  502.      * Parameters: 
  503.      * object - {<OpenLayers.Event>} 
  504.      */  
  505.     handleChangeLayer: function (object) {  
  506.         if (object.property === 'order') {  
  507.             if (this.isLayersInLayerSwitcher()) {  
  508.                 this.div.style.display = 'block';  
  509.                 this.moveTo(this.computePosition());  
  510.                 this.clipFirstLayer();  
  511.             } else {  
  512.                 this.div.style.display = 'none';  
  513.                 this.swipeLayer = null;  
  514.             }  
  515.         }  
  516.     },  
  517.   
  518.     /* 
  519.      * Method: handleUpdateSize 
  520.      * Triggered when the map size changed. In this case the swipe control is upda(猛击)ted accordingly. 
  521.      * 
  522.      * Parameters: 
  523.      * object - {<OpenLayers.Event>} 
  524.      */  
  525.     handleUpdateSize: function (object) {  
  526.         //we have to delay this on Android devices  
  527.         if (navigator.userAgent.toLowerCase().indexOf("android") > 0) {  
  528.             var self = this;  
  529.             setTimeout(function() {  
  530.                 self.resize();  
  531.             }, 10);  
  532.         } else {  
  533.             this.resize();  
  534.         }  
  535.     },  
  536.   
  537.     /* 
  538.      * Method: handleMove 
  539.      * Triggered when the map is moved. In this case, the clip ares has to be updated 
  540.      * 
  541.      * Parameters: 
  542.      * object - {<OpenLayers.Event>} 
  543.      */  
  544.     handleMove: function (object) {  
  545.         this.clipFirstLayer();  
  546.     },  
  547.   
  548.     /* 
  549.      * Method: resize 
  550.      * Resize the swipe and upd(猛击)ate the first layer clipping 
  551.      */  
  552.     resize: function() {  
  553.         this.div.style.height = this.map.getCurrentSize().h + 'px';  
  554.         this.div.style.width = this.width + 'px';  
  555.         this.moveTo(this.computePosition());  
  556.         this.clipFirstLayer();  
  557.         var topPosition = (this.map.getCurrentSize().h / 2) - 32;  
  558.         this.elementLeft.style.marginTop = topPosition + 'px';  
  559.         this.elementRight.style.marginTop = topPosition + 'px';  
  560.     },  
  561.   
  562.     /* 
  563.      * Method: computePosition 
  564.      * Recompute the p(验算)osition of the swipe acording  (猛击)to swipeRatio and the size of the map 
  565.      */  
  566.     computePosition: function() {  
  567.         var y = 0;  
  568.         var x = this.getSwipeRatio() * (this.map.size.w - this.width);  
  569.         return new OpenLayers.Pixel(x, y);  
  570.     },  
  571.   
  572.     /* 
  573.      * Method: getFirstLayerInLayerSwitcher 
  574.      * Get the first layer visible in the layer switcher 
  575.      */  
  576.     getFirstLayerInLayerSwitcher: function() {  
  577.         for (var i = this.map.layers.length - 1; i >= 0; i--) {  
  578.             var layer = this.map.layers[i];  
  579.             if (layer.displayInLayerSwitcher) {  
  580.                 return layer;  
  581.             }  
  582.         }  
  583.         return null;  
  584.     },  
  585.   
  586.     /* 
  587.      * Method: isLayersInLayerSwitcher 
  588.      * Check the presence of a layer in the layer switcher 
  589.      */  
  590.     isLayersInLayerSwitcher: function() {  
  591.         for (var i = 0, len = this.map.layers.length; i < len; i++) {  
  592.             var layer = this.map.layers[i];  
  593.             if (layer.displayInLayerSwitcher) {  
  594.                 return true;  
  595.             }  
  596.         }  
  597.         return false;  
  598.     },  
  599.   
  600.     setSwipeRatio: function(ratio) {  
  601.         this.map.events.triggerEvent("changelayer", {  
  602.             layer: this.swipeLayer,  
  603.             property: "name"  
  604.         });  
  605.         this.map.swipeRatio = ratio;  
  606.         this.map.swipeActive = this.active;  
  607.     },  
  608.   
  609.     getSwipeRatio: function() {  
  610.         return this.map.swipeRatio;  
  611.     },  
  612.   
  613.     /* 
  614.      * Method: updateRatio 
  615.      * Update the swipeRatio and update the swipe control acc(猛击)ordingly 
  616.      */  
  617.     updateRatio: function(ratio) {  
  618.         this.setSwipeRatio(ratio);  
  619.         if (this.isLayersInLayerSwitcher()) {  
  620.             this.div.style.display = 'block';  
  621.             this.moveTo(this.computePosition());  
  622.             this.clipFirstLayer();  
  623.         } else {  
  624.             this.div.style.display = 'none';  
  625.             this.swipeLayer = null;  
  626.         }  
  627.     },  
  628.   
  629.     CLASS_NAME: "OpenLayers.Control.Swipe(猛击)"  
  630. });  



  1. <pre code_snippet_id="1644580" snippet_file_name="blog_20160413_1_9288680" name="code" class="javascript"><span style="font-family: Arial, Helvetica, sans-serif; rgb(255, 255, 255);">这个js文件里面引用到了一些样式,样式文件的内容如下:</span></pre>  
  1. .olControlSwipe {  
  2.     background:url("../img/line.png") repeat;  
  3.     display: none;  
  4. }  
  5.   
  6. .olControlSwipeHover {  
  7.     cursor: w-resize;  
  8. }  
  9.   
  10. .olControlSwipeLayerView {  
  11.     background-color: white;  
  12.     height: 16px;  
  13.     width: 220px;  
  14.     margin-top: 23px;  
  15.     margin-left: -230px;  
  16.     display: block;  
  17.     font-size: 11px;  
  18.     font-family: Tahoma, Arial;  
  19.     font-weight: 700;  
  20.     padding-top: 2px;  
  21.     background-image: url("../img/bigarrow_left.png");  
  22.     background-repeat: no-repeat;  
  23.     position: absolute;  
  24. }  
  25.   
  26. .olControlSwipeLayerSpan {  
  27.     visibility: hidden;  
  28.     font-size: 11px;  
  29.     font-family: Tahoma, Arial;  
  30.     font-weight: 700;  
  31.     white-space: pre;  
  32.     position: absolute;  
  33. }  
  34.   
  35. .olControlSwipeLayerHide {  
  36.     display: none;  
  37. }  
  38.   
  39. .olControlArrowLeft {  
  40.     width: 16px;  
  41.     height: 32px;  
  42.     margin-left: -1px;  
  43.     background-image: url("../img/bigarrow_left.png");  
  44.     background-repeat: no-repeat;  
  45.     position: absolute;  
  46. }  
  47. .olControlArrowRight {  
  48.     width: 16px;  
  49.     height: 32px;  
  50.     margin-left: 19px;  
  51.     background-image: url("../img/bigarrow_right.png");  
  52.     background-repeat: no-repeat;  
  53.     position: absolute;  
  54. }  

最后,调用展示,代用代码如下:

    1. $("#swipebutton").on("click",function(){  
    2.     if(flag){  
    3.         swipe.deactiv(猛击)ate();  (无效)
    4.         flag=false;  
    5.     }  
    6.     else{  
    7.         swipe.activate();  
    8.         flag=true;  
    9.     }  
    10. }); 
openlayers:实现图层卷帘(附完整源码) 阅读详情

相关推荐

Vue.js + OpenLayers 实现卷帘效果的 Canvas Clip 功能

在 startDrag 方法中,我们监听鼠标的 mousedown 事件,并在该事件触发时添加了鼠标移动和鼠标释放的事件监听器。在 drag 方法中,我们根据鼠标的位置计算出分隔线的位置,并通过修改其 left 样式来实现上卷帘效果。该方法使用 OpenLayers 的 Map 类创建一个地图实例,并设置了一个基本的地图图层(使用 OpenStreetMap)和视图。最后,在组件的样式中,我们设置了地图容器和分隔线的样式,使其能够正确显示和交互。,即可看到一个带有上卷帘效果的地图。

CodeWhizZ的博客 321

全网最新版openlayers视频教程

学习关于二维地理图层渲染,投影转换,各种数据类型渲染,OGC服务,各种地图交互

44.在 Vue 3 中使用 OpenLayers 利用 Canvas Clip 实现左卷帘功能

实现目标基于 Vue 3 和 OpenLayers 搭建基础 Web 地图。添加滑块控制卷帘效果,动态调整地图的显示区域。利用 OpenLayers 的prerender和postrender钩子操作 canvas 绘图上下文,控制图层显示。本文通过 Vue 3 和 OpenLayers 的结合,展示了如何利用 Canvas Clip 技术实现地图卷帘效果。此实现方式不仅简单灵活,还能应用于多种地图可视化场景,如对比分析、遮罩高亮等。❤️。

小彭的博客 967

openlayers实现卷帘功能

openlayers实现卷帘功能。

weixin_44614518的博客 1273

226:vue+openlayers 利用swipe插件实现左卷帘功能

第226个本示例的目的是介绍演示如何在vue+openlayers项目中引用插件,实现左卷帘功能。

大剑师兰特的GIS世界 890

227:vue+openlayers自定义js横向卷帘,图层名称跟着分割线移动

本示例演示如何在vue+openlayers项目中自定义js实现横向卷帘。这个示例中的功能包括开启关闭卷帘,选择显示的地图作为左边,另外一个作为右边,不能的选择,不能的切换效果。左右拉动按钮,可以看上下层的内容,文字跟着分割线来移动。

大剑师兰特的GIS世界 859

Openlayers 卷帘功能

卷帘 “卷帘”功能可以分为两部分: 控件元素的拖动。这里控件仿照ArcGIS for JS上的样式。 地图图层渲染流程的控制,对上层图层进行裁剪。 首先我们需要一个DOM组件作为拖动的“卷帘”,将其放在map容器内部,避免影响页面其他要素。 <div id="map" class="map"> <div id="swipeContainer"> <div id="swipeDiv"> <div class="ha

weixin_38670190的博客 2047

044:vue+openlayers利用canvas clip 实现上卷帘功能(代码示例)

本示例的目的是介绍演示如何在vue+openlayers中添加上卷帘功能。上下放置两个地图层,上面的地图利用canvas的clip功能,只显示选区内的信息。图层渲染可以在precompose和postcompose事件处理程序中被控制,这两个事件处理程序的事件对象中包含一个Canvas渲染上下文属性(canvas rendering context)。.........

大剑师兰特的GIS世界 905

Openlayers笔记之实现卷帘查看功能

在 Web GIS 的开发中,总是会遇到很多新奇的需求,比如卷帘查看功能。 卷帘查看是为了方便比较不同时间、不同类型的两张地图在同一地点的差异性,通过卷帘操作能够快速对比出两张地图的差异性。 1.实现思路 Openlayers API文档中提供了控制地图范围的接口(Extent)。通过加载两张地图,移动前端绘制的卷帘轴,实时获取卷帘轴当前的屏幕坐标(x, y),并实时控制上面层级地图的显示范围,即可实现该功能。 2.具体实现 首先创建图层,加载两张在线地图: /* * 初始化地图 */ function.

Spider_wang的博客 2074

045:vue+openlayers利用canvas clip实现左卷帘功能

本示例的目的是介绍演示如何在vue+openlayers中添加左卷帘功能。图层渲染可以在precompose和postcompose事件处理程序中被控制,这两个事件处理程序的事件对象中包含一个Canvas渲染上下文属性(canvas rendering context)。......

大剑师兰特的GIS世界 789

079:vue+mapbox 实现卷帘效果

本示例演示如何在vue+mapbox 中实现卷帘效果。这里利用了mapbox-gl-compare这个插件,在使用的时候,特别要注意,将id赋值给container,将引用的compare插件赋值给mapboxgl.Compare,详细使用见源代码直接复制下面的 vue+mapbox源代码,操作2分钟即可运行实现效果。

大剑师兰特的GIS世界 1609

Openlayers+vue实现卷帘功能

地图卷帘功能主要是通过控件元素的拖动 ,对地图图层渲染流程的控制。这里附上两种实现方法供大家参考。

fungis 2282

OpenLayers实现不同地图卷帘对比

所谓的重新渲染其实还是利用了裁剪的原理,比如当滑块滑动的时候,那么上层图层的宽是在不断变化的,然后根据这个变化利用图层的prerender和postrender事件,裁切掉原来宽度减去现在宽度的那一部分,那么就剩下了现在新的图层。另外使用到的核心原理其实还是裁剪。根据中间分割线的位置,计算位于上层的图层的宽高,然后重新渲染,这里的上层的意思就是按照zindex的顺序,位于上层的那个图层。那么根据这个原理我们还可以实现任意的裁切,比如我们如果想实现上下的卷帘对比也是相当的容易,只需要实时计算滑条的高度即可。

lz5211314121的博客 433

vue+openlayers 开发一个卷帘图层控件

vue+openlayers实现一个卷帘图层控件。

qq_34447899的博客 1755

Win10如何使用BC3.1精简版

Win10如何使用BC3.1精简版 使用Borland C++3.1 学习μC/OS—Ⅱ几乎是现在的普遍做法,但是由于Win10系统不能直接使用BC3.1,所以需要用到DOSBox。 1、解压bc3.1精简版压缩包,本文将BC3.1文件夹复制在D盘下,DOSBOX按照默认路径安装。下图为两者安装目录图示: 2、修改DOSBOX的配置文件,找到DOSBox文件安装路径,记事本方式打开DOSBox 0.74-3 Options.bat文件,在此文件末尾添加: MOUNT C D:\BC//意为把D:\BC目录

qq_45547236的博客 2377

Dify+firecrawl:一个超好用的网页转Markdown API,打造你的第一个能检索爬虫RAG知识库的聊天机器人

Firecrawl是一个超简单的API服务——你扔给它一个URL,它就能把整个网站(包括子页面!)的内容自动抓下来,并且转换成干净、易读的Markdown格式。不用配置网站的站点地图,不用写爬虫规则,API调一下,数据直接到手。接下来我们一起本地部署吧!

2501_91673550的博客 2992

python实现卷帘功能_openlayers之卷帘地图SWIPE MAP

openlayers之卷帘地图SWIPE MAP功能说明卷帘指,同一屏幕,左右或者上下同时看到两个图层,可以直观的进行对比。通过滑块滑动控制显示比例原理地图渲染是绘制的canvas,卷帘实现原理是根据滑块值,设置地图显示比例,重新绘制canvas渲染地图范围代码详解var map, view;$(function() {var osm = new ol.layer.Tile({//定义图层1sou...

weixin_33275503的博客 600

Halcon实战:5分钟搞定线序颜色检测(附完整代码解析)

本文详细介绍了如何使用Halcon在5分钟内实现工业级线序颜色检测的高效方案。通过机器视觉技术,结合实战项目经验,提供完整的代码解析和优化技巧,帮助开发者快速构建鲁棒性强、准确率高的检测系统,适用于电子制造和汽车线束等行业。

weixin_29098367的博客 441
上一篇: Openlayers2中聚类的动态实现
下一篇: Openlayers3中统计图的实现:结合highcharts实现统计图
韩慧兵
博客等级 码龄15年 170粉丝 171原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值