1. 程式人生 > >select-feature openlayers2.13.1向量圖層的要素選擇 框選 點選

select-feature openlayers2.13.1向量圖層的要素選擇 框選 點選

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <title>SelectFeature Control on Layer.Vector</title> 
    <link rel="stylesheet" href="../theme/default/style.css" type="text/css">
    <link rel="stylesheet" href="style.css" type="text/css">
    <style type="text/css">
        #controlToggle li {
            list-style: none;
        }
    </style>
    <script src="../lib/OpenLayers.js"></script>
    <script type="text/javascript">
	    
        var map, drawControls;
        //定義要素的向量圖的樣式的線寬
        OpenLayers.Feature.Vector.style['default']['strokeWidth'] = '2';
        function init(){
			//初始化map 
			map = new OpenLayers.Map('map');
			//宣告並初始化一個WMS圖層
            var wmsLayer = new OpenLayers.Layer.WMS(
                "OpenLayers WMS", 
                "http://vmap0.tiles.osgeo.org/wms/vmap0",
                {layers: 'basic'}
            ); 

            // allow testing of specific renderers via "?renderer=Canvas", etc
            var renderer = OpenLayers.Util.getParameters(window.location.href).renderer;
			//?: 運算子是 if...else 語句的快捷方式
            renderer = (renderer) ? [renderer] : OpenLayers.Layer.Vector.prototype.renderers;
			
			
			//宣告並初始化一個向量圖層
            var vectors = new OpenLayers.Layer.Vector("Vector Layer", {
                renderers: renderer
            });
			
            vectors.events.on({
                'featureselected': function(feature) {
                    document.getElementById('counter').innerHTML = this.selectedFeatures.length;
                },
                'featureunselected': function(feature) {
                    document.getElementById('counter').innerHTML = this.selectedFeatures.length;
                }
            });
			//map新增2個圖層
            map.addLayers([wmsLayer, vectors]);
			
            map.addControl(new OpenLayers.Control.LayerSwitcher());
			
            //5個控制器
            drawControls = {
				//點
                point: new OpenLayers.Control.DrawFeature(
                    vectors, OpenLayers.Handler.Point
                ),
				//線
                line: new OpenLayers.Control.DrawFeature(
                    vectors, OpenLayers.Handler.Path
                ),
				//多邊形
                polygon: new OpenLayers.Control.DrawFeature(
                    vectors, OpenLayers.Handler.Polygon
                ),
				//選擇
                select: new OpenLayers.Control.SelectFeature(
                    vectors,
                    {
                        clickout: false, toggle: false,
                        multiple: false, hover: false,
                        toggleKey: "ctrlKey", // ctrl key removes from selection
                        multipleKey: "shiftKey", // shift key adds to selection
                        box: true
                    }
                ),
				//懸浮選擇
                selecthover: new OpenLayers.Control.SelectFeature(
                    vectors,
                    {
                        multiple: false, hover: true,
                        toggleKey: "ctrlKey", // ctrl key removes from selection
                        multipleKey: "shiftKey" // shift key adds to selection
                    }
                )
            };
            //map新增5個控制器
            for(var key in drawControls) {
                map.addControl(drawControls[key]);
            }
			//設定map的中心點
            map.setCenter(new OpenLayers.LonLat(0, 0), 3);

        }
		//5大功能切換
        function toggleControl(element) {
            for(key in drawControls) {
                var control = drawControls[key];
                if(element.value == key && element.checked) {
					//啟用控制器
                    control.activate();
                } else {
					//關閉控制器
                    control.deactivate();
                }
            }
        }
		//處理兩個多選框處理
        function update() {
			//選中點選空白處的配置
            var clickout = document.getElementById("clickout").checked;
            if(clickout != drawControls.select.clickout) {
                drawControls.select.clickout = clickout;
            }
			//選中框選後的配置
            var box = document.getElementById("box").checked;
            if(box != drawControls.select.box) {
                drawControls.select.box = box;
                if(drawControls.select.active) {
					//重新啟用選擇控制器
                    drawControls.select.deactivate();
                    drawControls.select.activate();
                }
            }
        }
    </script>
  </head>
  <body onload="init()">
    <h1 id="title">OpenLayers Select Feature Example</h1>
    <div id="tags">
        vector, feature, selecting, selection, advanced
    </div>        
    <p id="shortdesc">
      Select a feature on hover or click with the Control.SelectFeature on a vector layer.
	  使用懸浮或者點選操作選擇一個要素。在向量圖層上選擇要素。(一般每個向量圖層上都可以放置N多個要素。)
    </p>  
    <div id="map" class="smallmap"></div>
    <ul id="controlToggle">
        <li>
            <input type="radio" name="type" value="none" id="noneToggle"
                   onclick="toggleControl(this);" checked="checked" />
            <label for="noneToggle">navigate導航選單</label>
        </li>
        <li>
            <input type="radio" name="type" value="point" id="pointToggle"
                   onclick="toggleControl(this);" />
            <label for="pointToggle">draw point畫點</label>
        </li>
        <li>
            <input type="radio" name="type" value="line" id="lineToggle"
                   onclick="toggleControl(this);" />
            <label for="lineToggle">draw line畫線</label>
        </li>
        <li>
            <input type="radio" name="type" value="polygon" id="polygonToggle"
                   onclick="toggleControl(this);" />
            <label for="polygonToggle">draw polygon畫多邊形</label>
        </li>
        <li>
            <input type="radio" name="type" value="selecthover" id="selecthoverToggle"
                   onclick="toggleControl(this);" />
            <label for="selecthoverToggle">Select features on hover懸浮時選擇要素</label>
        </li>
        <li>
            <input type="radio" name="type" value="select" id="selectToggle"
                   onclick="toggleControl(this);" />
            <label for="selectToggle">select feature 選擇要素(<span id="counter">0</span> features selected)</label>
            <ul>
                <li>
                    <input id="box" type="checkbox" checked="checked"
                           name="box" onchange="update()" />
                    <label for="box">select features in a box框選要素</label>
                </li>
                <li>
                    <input id="clickout" type="checkbox"
                           name="clickout" onchange="update()" />
                    <label for="clickout">click out to unselect features點選空白取消選擇要素</label>
                </li>
            </ul>
        </li>
    </ul>
    <p>Use the shift key to select multiple features.  Use the ctrl key to
    toggle selection on features one at a time.  Note: the "clickout" option has no
    effect when "hover" is selected.用shift鍵新增要素。用ctrl鍵減去要素。</p>
  </body>
</html>