forked from geoffmcl/map-test2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw-feature.html
213 lines (190 loc) · 9.24 KB
/
draw-feature.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<!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>Draw Feature Example</title>
<link rel="stylesheet" href="js/theme/default/style.css" type="text/css">
<link rel="stylesheet" href="css/style2.css" type="text/css">
<style type="text/css">
#controlToggle li {
list-style: none;
}
p {
width: 1024px;
}
/* avoid pink tiles */
.olImageLoadError {
background-color: transparent !important;
}
</style>
<script src="js/OpenLayers.js"></script>
<script type="text/javascript">
var add_selection = true;
var map, drawControls;
var wmsLayer, pointLayer, lineLayer, polygonLayer, boxLayer;
var highlightCtrl, selectCtrl;
function init(){
map = new OpenLayers.Map('map');
wmsLayer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://vmap0.tiles.osgeo.org/wms/vmap0?", {layers: 'basic'});
pointLayer = new OpenLayers.Layer.Vector("Point Layer");
lineLayer = new OpenLayers.Layer.Vector("Line Layer");
polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer");
boxLayer = new OpenLayers.Layer.Vector("Box layer");
map.addLayers([wmsLayer, pointLayer, lineLayer, polygonLayer, boxLayer]);
map.addControl(new OpenLayers.Control.LayerSwitcher());
map.addControl(new OpenLayers.Control.MousePosition());
drawControls = {
point: new OpenLayers.Control.DrawFeature(pointLayer,
OpenLayers.Handler.Point),
line: new OpenLayers.Control.DrawFeature(lineLayer,
OpenLayers.Handler.Path),
polygon: new OpenLayers.Control.DrawFeature(polygonLayer,
OpenLayers.Handler.Polygon),
box: new OpenLayers.Control.DrawFeature(boxLayer,
OpenLayers.Handler.RegularPolygon, {
handlerOptions: {
sides: 4,
irregular: true
}
}
)
};
for(var key in drawControls) {
map.addControl(drawControls[key]);
}
if (add_selection) {
var report = function(e) {
//OpenLayers.Console.log(e.type, e.feature.id);
console.log('type: '+e.type+' id: '+e.feature.id);
};
highlightCtrl = new OpenLayers.Control.SelectFeature(polygonLayer, {
hover: true,
highlightOnly: true,
renderIntent: "temporary",
eventListeners: {
beforefeaturehighlighted: report,
featurehighlighted: report,
featureunhighlighted: report
}
});
selectCtrl = new OpenLayers.Control.SelectFeature(polygonLayer,
{clickout: true}
);
map.addControl(highlightCtrl);
map.addControl(selectCtrl);
highlightCtrl.activate();
selectCtrl.activate();
}
map.setCenter(new OpenLayers.LonLat(3, 48), 3);
document.getElementById('noneToggle').checked = true;
}
function toggleControl(element) {
for(key in drawControls) {
var control = drawControls[key];
if(element.value == key && element.checked) {
control.activate();
} else {
control.deactivate();
}
}
}
function allowPan(element) {
var stop = !element.checked;
for(var key in drawControls) {
drawControls[key].handler.stopDown = stop;
drawControls[key].handler.stopUp = stop;
}
}
function deletePoly() {
var p = polygonLayer;
var m = map;
var msg = '';
var cnt = 0;
var key;
var arr, ri, feats, i;
var fnd = false;
if (polygonLayer.drawn) {
feats = polygonLayer.features;
for( i = 0; i < feats.length; i++ ) {
arr = feats[i];
ri = arr.renderIntent;
msg += '\nid: '+arr.id+" ri="+ri;
if (ri == 'select') {
fnd = true;
}
cnt++;
}
}
//for(key in drawControls) {
// var control = drawControls[key];
// msg += "\n"+key+ ' id='+control.id+' ';
// //var h = control.handler;
// cnt++;
//}
alert('Delete poly clicked. Found '+cnt+' arrs: '+msg);
}
</script>
</head>
<body onload="init()">
<h1 id="title">OpenLayers Draw Feature Example</h1>
<div id="tags">
point, line, linestring, polygon, box, digitizing, geometry, draw, drag
</div>
<p id="shortdesc">
Demonstrate on-screen digitizing tools for point, line, polygon and box creation.
</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="box" id="boxToggle" onclick="toggleControl(this);" />
<label for="boxToggle">draw box</label>
</li>
<li>
<input type="checkbox" name="allow-pan" value="allow-pan" id="allowPanCheckbox" checked=true onclick="allowPan(this);" />
<label for="allowPanCheckbox">allow pan while drawing</label>
</li>
</ul>
<div id="docs">
<p>With the point drawing control active, click on the map to add a point.</p>
<p>With the line drawing control active, click on the map to add the points that make up your line.
Double-click to finish drawing.</p>
<p>With the polygon drawing control active, click on the map to add the points that make up your
polygon. Double-click to finish drawing.</p>
<p>With the box drawing control active, click in the map and drag the mouse to get a rectangle. Release
the mouse to finish.</p>
<p>With any drawing control active, paning the map can still be achieved. Drag the map as
usual for that.</p>
<p>Hold down the shift key while drawing to activate freehand mode. While drawing lines or polygons
in freehand mode, hold the mouse down and a point will be added with every mouse movement.</p>
<p>Copied from openlayers-git\examples, and modified to local css and js</p>
<p>Sets up a WMS map layer, plus 4 vector layers - pointLayer "Point Layer", lineLayer "Line Layer",
polygonLayer "Polygon Layer", and boxLayer "Box layer". Has 5 radio button, the first being
just navigate. Selecting a radio button deactivates all other and establishes that type of
drawing.</p>
<p>An interesting addition would be to allow 'feature' selection, and a 'hover' change, like in
the highlight-feature.html example, at least for the polygon layer. Just cut in the code, and it
sort of worked... Which leads to another interesting addition, and that would be the ability to
delete a selected polygon. <input type="button" value="delete" onclick="deletePoly()"></p>
</div>
</body>
</html>