Adding a snapping agent for snapping geometry components to existing components while editing. r=ahocevar (closes #954)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@8951 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
226
examples/snapping.html
Normal file
226
examples/snapping.html
Normal file
@@ -0,0 +1,226 @@
|
||||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<title>Snapping</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">
|
||||
.olControlEditingToolbar .olControlModifyFeatureItemInactive {
|
||||
background-position: -1px 0px ;
|
||||
}
|
||||
.olControlEditingToolbar .olControlModifyFeatureItemActive {
|
||||
background-position: -1px -23px ;
|
||||
}
|
||||
table {
|
||||
padding: 1em 0 1em;
|
||||
}
|
||||
td {
|
||||
padding: 0.5em 1em;
|
||||
border: 1px solid #ddd;
|
||||
}
|
||||
tr.head td {
|
||||
text-align: center;
|
||||
font-weight: bold;
|
||||
}
|
||||
</style>
|
||||
<script src="../lib/Firebug/firebug.js"></script>
|
||||
<script src="../lib/OpenLayers.js"></script>
|
||||
<script type="text/javascript">
|
||||
OpenLayers.Feature.Vector.style['default']['strokeWidth'] = '2';
|
||||
|
||||
function init() {
|
||||
initMap();
|
||||
initUI();
|
||||
}
|
||||
|
||||
var map, draw, modify, snap, point, line, poly;
|
||||
function initMap() {
|
||||
|
||||
map = new OpenLayers.Map('map');
|
||||
|
||||
// create three vector layers
|
||||
poly = new OpenLayers.Layer.Vector("polygons", {
|
||||
strategies: [new OpenLayers.Strategy.Fixed()],
|
||||
protocol: new OpenLayers.Protocol.HTTP({
|
||||
url: "data/poly.json",
|
||||
format: new OpenLayers.Format.GeoJSON()
|
||||
}),
|
||||
isBaseLayer: true
|
||||
});
|
||||
line = new OpenLayers.Layer.Vector("lines", {
|
||||
strategies: [new OpenLayers.Strategy.Fixed()],
|
||||
protocol: new OpenLayers.Protocol.HTTP({
|
||||
url: "data/line.json",
|
||||
format: new OpenLayers.Format.GeoJSON()
|
||||
})
|
||||
});
|
||||
point = new OpenLayers.Layer.Vector("points", {
|
||||
strategies: [new OpenLayers.Strategy.Fixed()],
|
||||
protocol: new OpenLayers.Protocol.HTTP({
|
||||
url: "data/point.json",
|
||||
format: new OpenLayers.Format.GeoJSON()
|
||||
})
|
||||
});
|
||||
map.addLayers([poly, line, point]);
|
||||
|
||||
// configure the snapping agent
|
||||
snap = new OpenLayers.Control.Snapping({
|
||||
layer: poly,
|
||||
targets: [point, line, poly],
|
||||
greedy: false
|
||||
});
|
||||
snap.activate();
|
||||
|
||||
// add some editing tools to a panel
|
||||
var panel = new OpenLayers.Control.Panel({
|
||||
displayClass: "olControlEditingToolbar"
|
||||
});
|
||||
draw = new OpenLayers.Control.DrawFeature(
|
||||
poly, OpenLayers.Handler.Polygon,
|
||||
{displayClass: "olControlDrawFeaturePoint"}
|
||||
);
|
||||
modify = new OpenLayers.Control.ModifyFeature(
|
||||
poly, {displayClass: "olControlModifyFeature"}
|
||||
);
|
||||
panel.addControls([
|
||||
new OpenLayers.Control.Navigation(),
|
||||
draw, modify
|
||||
]);
|
||||
map.addControl(panel);
|
||||
|
||||
// give the map a location
|
||||
map.setCenter(new OpenLayers.LonLat(0, 0), 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add behavior to page elements. This basically lets us set snapping
|
||||
* target properties with the checkboxes and text inputs. The checkboxes
|
||||
* toggle the target node, vertex, or edge (boolean) values. The
|
||||
* text inputs set the nodeTolerance, vertexTolerance, or edgeTolerance
|
||||
* property values.
|
||||
*/
|
||||
function initUI() {
|
||||
var check = $("snapping");
|
||||
check.checked = true;
|
||||
check.onclick = function() {
|
||||
if(check.checked) {
|
||||
snap.activate();
|
||||
} else {
|
||||
snap.deactivate();
|
||||
}
|
||||
};
|
||||
|
||||
var sel = $("editable");
|
||||
sel.value = "poly";
|
||||
sel.onchange = function() {
|
||||
updateEditable(sel.value);
|
||||
}
|
||||
|
||||
var target, type, tog, tol;
|
||||
var types = ["node", "vertex", "edge"];
|
||||
for(var i=0; i<snap.targets.length; ++i) {
|
||||
target = snap.targets[i];
|
||||
for(var j=0; j<types.length; ++j) {
|
||||
type = types[j];
|
||||
tog = $(i + "_" + type);
|
||||
tog.checked = target[type];
|
||||
tog.onclick = (function(tog, type, target) {
|
||||
return function() {target[type] = tog.checked;}
|
||||
})(tog, type, target);
|
||||
tol = $(i + "_" + type + "Tolerance");
|
||||
tol.value = target[type + "Tolerance"];
|
||||
tol.onchange = (function(tol, type, target) {
|
||||
return function() {
|
||||
target[type + "Tolerance"] = Number(tol.value) || 0;
|
||||
}
|
||||
})(tol, type, target);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// this function allows the editable layer to be changed
|
||||
// for the snapping control, this amounts to calling setLayer
|
||||
function updateEditable(name) {
|
||||
|
||||
layer = window[name];
|
||||
|
||||
// update the editable layer for the snapping control (nice)
|
||||
snap.setLayer(layer);
|
||||
|
||||
// update the editable layer for the modify control (ugly)
|
||||
var modActive = modify.active;
|
||||
if(modActive) {
|
||||
modify.deactivate();
|
||||
}
|
||||
modify.layer = layer;
|
||||
modify.selectControl.layer = layer;
|
||||
modify.selectControl.handlers.feature.layer = layer;
|
||||
modify.dragControl.layer = layer;
|
||||
modify.dragControl.handlers.drag.layer = layer;
|
||||
modify.dragControl.handlers.feature.layer = layer;
|
||||
if(modActive) {
|
||||
modify.activate();
|
||||
}
|
||||
|
||||
// update the editable layer for the draw control (very ugly)
|
||||
var drawActive = draw.active;
|
||||
if(drawActive) {
|
||||
draw.deactivate();
|
||||
}
|
||||
draw.layer = layer;
|
||||
var handler = ({
|
||||
point: OpenLayers.Handler.Point,
|
||||
line: OpenLayers.Handler.Path,
|
||||
poly: OpenLayers.Handler.Polygon
|
||||
})[name];
|
||||
draw.handler = new handler(draw, draw.callbacks, draw.handlerOptions);
|
||||
if(drawActive) {
|
||||
draw.activate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body onload="init()">
|
||||
<h1 id="title">Snapping Example</h1>
|
||||
<div id="shortdesc">A demonstration snapping while editing vector features.</div>
|
||||
<div id="map" class="smallmap"></div>
|
||||
<br/>
|
||||
<label for="editable">Editable Layer:</label>
|
||||
<select id="editable" name="editable">
|
||||
<option value="poly">polygons</option>
|
||||
<option value="line">lines</option>
|
||||
<option value="point">points</option>
|
||||
</select>
|
||||
<label for="snapping">Enable Snapping</label>
|
||||
<input type="checkbox" name="snapping" id="snapping" checked="checked" />
|
||||
<table>
|
||||
<tbody>
|
||||
<tr class="head">
|
||||
<td>targets</td><td>node</td><td>vertex</td><td>edge</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>points</td>
|
||||
<td><input type="checkbox" id="0_node" /><input id="0_nodeTolerance" type="text" size="3" /></td>
|
||||
<td><input type="checkbox" id="0_vertex" /><input id="0_vertexTolerance" type="text" size="3" /></td>
|
||||
<td><input type="checkbox" id="0_edge" /><input id="0_edgeTolerance" type="text" size="3" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>lines</td>
|
||||
<td><input type="checkbox" id="1_node" /><input id="1_nodeTolerance" type="text" size="3" /></td>
|
||||
<td><input type="checkbox" id="1_vertex" /><input id="1_vertexTolerance" type="text" size="3" /></td>
|
||||
<td><input type="checkbox" id="1_edge" /><input id="1_edgeTolerance" type="text" size="3" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>polygons</td>
|
||||
<td><input type="checkbox" id="2_node" /><input id="2_nodeTolerance" type="text" size="3" /></td>
|
||||
<td><input type="checkbox" id="2_vertex" /><input id="2_vertexTolerance" type="text" size="3" /></td>
|
||||
<td><input type="checkbox" id="2_edge" /><input id="2_edgeTolerance" type="text" size="3" /></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<p>Though all snapping types are shown here for all target layers, not all are sensible.
|
||||
Points don't have edges, for example.</p>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user