1 <html xmlns="http://www.w3.org/1999/xhtml">
3 <title>Snapping & Splitting</title>
4 <link rel="stylesheet" href="../theme/default/style.css" type="text/css" />
5 <link rel="stylesheet" href="style.css" type="text/css" />
6 <style type="text/css">
7 .olControlEditingToolbar .olControlModifyFeatureItemInactive {
8 background-position: -1px 0px ;
10 .olControlEditingToolbar .olControlModifyFeatureItemActive {
11 background-position: -1px -23px ;
15 padding: 1em 0 0.1em 0;
16 border-bottom: 1px solid grey;
26 <script src="../lib/Firebug/firebug.js"></script>
27 <script src="../lib/OpenLayers.js"></script>
28 <script type="text/javascript">
30 OpenLayers.Feature.Vector.style['default']['strokeWidth'] = '2';
38 var map, draw, modify, snap, split, vectors;
41 map = new OpenLayers.Map('map');
42 var styles = new OpenLayers.StyleMap({
43 "default": new OpenLayers.Style(null, {
49 graphicName: "square",
54 strokeColor: "#333333"
59 strokeColor: "#666666"
65 "select": new OpenLayers.Style({
66 strokeColor: "#00ccff",
69 "temporary": new OpenLayers.Style(null, {
75 graphicName: "square",
80 strokeColor: "#333333"
85 strokeColor: "#00ccff"
93 // create three vector layers
94 vectors = new OpenLayers.Layer.Vector("Lines", {
96 strategies: [new OpenLayers.Strategy.Fixed()],
97 protocol: new OpenLayers.Protocol.HTTP({
98 url: "data/roads.json",
99 format: new OpenLayers.Format.GeoJSON()
102 maxExtent: new OpenLayers.Bounds(
103 1549471.9221, 6403610.94, 1550001.32545, 6404015.8
106 map.addLayer(vectors);
108 // configure the snapping agent
109 snap = new OpenLayers.Control.Snapping({layer: vectors});
110 map.addControl(snap);
113 // configure split agent
114 split = new OpenLayers.Control.Split({
119 aftersplit: function(event) {
120 flashFeatures(event.features);
124 map.addControl(split);
127 // add some editing tools to a panel
128 var panel = new OpenLayers.Control.Panel({
129 displayClass: "olControlEditingToolbar"
131 draw = new OpenLayers.Control.DrawFeature(
132 vectors, OpenLayers.Handler.Path,
133 {displayClass: "olControlDrawFeaturePoint", title: "Draw Features"}
135 modify = new OpenLayers.Control.ModifyFeature(
136 vectors, {displayClass: "olControlModifyFeature", title: "Modify Features"}
139 new OpenLayers.Control.Navigation({title: "Navigate"}),
142 map.addControl(panel);
144 map.addControl(new OpenLayers.Control.MousePosition());
146 map.zoomToMaxExtent();
149 function flashFeatures(features, index) {
153 var current = features[index];
154 if(current && current.layer === vectors) {
155 vectors.drawFeature(features[index], "select");
157 var prev = features[index-1];
158 if(prev && prev.layer === vectors) {
159 vectors.drawFeature(prev, "default");
162 if(index <= features.length) {
163 window.setTimeout(function() {flashFeatures(features, index)}, 75);
168 * Add behavior to page elements. This basically lets us set snapping
169 * target properties with the checkboxes and text inputs. The checkboxes
170 * toggle the target node, vertex, or edge (boolean) values. The
171 * text inputs set the nodeTolerance, vertexTolerance, or edgeTolerance
175 // add behavior to snap elements
176 var snapCheck = $("snap_toggle");
177 snapCheck.checked = true;
178 snapCheck.onclick = function() {
179 if(snapCheck.checked) {
181 $("snap_options").style.display = "block";
184 $("snap_options").style.display = "none";
187 var target, type, tog, tol;
188 var types = ["node", "vertex", "edge"];
189 var target = snap.targets[0];
190 for(var j=0; j<types.length; ++j) {
192 tog = $("target_" + type);
193 tog.checked = target[type];
194 tog.onclick = (function(tog, type, target) {
195 return function() {target[type] = tog.checked;}
196 })(tog, type, target);
197 tol = $("target_" + type + "Tolerance");
198 tol.value = target[type + "Tolerance"];
199 tol.onchange = (function(tol, type, target) {
201 target[type + "Tolerance"] = Number(tol.value) || 0;
203 })(tol, type, target);
206 // add behavior to split elements
207 var splitCheck = $("split_toggle");
208 splitCheck.checked = true;
209 splitCheck.onclick = function() {
210 if(splitCheck.checked) {
212 $("split_options").style.display = "block";
215 $("split_options").style.display = "none";
218 var edgeCheck = $("edge_toggle");
219 edgeCheck.checked = split.edge;
220 edgeCheck.onclick = function() {
221 split.edge = edgeCheck.checked;
224 $("clear").onclick = function() {
226 vectors.destroyFeatures();
233 <body onload="init()">
234 <h1 id="title">Snapping & Splitting Example</h1>
235 <div id="shortdesc">A demonstration snapping and splitting while editing vector features.</div>
236 <div id="map" class="smallmap"></div>
238 <input type="checkbox" id="snap_toggle" />
239 <label for="snap_toggle" class="head">Enable Snapping</label>
240 <table id="snap_options">
243 <td>target</td><td>node</td><td>vertex</td><td>edge</td>
247 <td><input type="checkbox" id="target_node" /><input id="target_nodeTolerance" type="text" size="3" /></td>
248 <td><input type="checkbox" id="target_vertex" /><input id="target_vertexTolerance" type="text" size="3" /></td>
249 <td><input type="checkbox" id="target_edge" /><input id="target_edgeTolerance" type="text" size="3" /></td>
254 <input type="checkbox" id="split_toggle" />
255 <label for="split_toggle" class="head">Enable Splitting</label>
256 <table id="split_options">
259 <td><label for="edge_toggle">edges split</label></td>
260 <td><input type="checkbox" id="edge_toggle" /></td>
265 <button id="clear">clear</button> Clear all features.