|
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | <html xmlns="http://www.w3.org/1999/xhtml"> <head> <link rel="stylesheet" href="../theme/default/style.css" type="text/css" /> <link rel="stylesheet" href="style.css" type="text/css" /> <style type="text/css"> .float-left { float: left; } .clear-left { clear: left; } </style> <script src="../lib/Gears/gears_init.js"></script> <script src="../lib/OpenLayers.js"></script> <script type="text/javascript"> var map, vector, protocol, modify; function init() { // create Gears protocol protocol = new OpenLayers.Protocol.SQL.Gears({ databaseName: "db_name", tableName: "table_name", saveFeatureState: false }); if (!GearsIsSupported()) { return; } map = new OpenLayers.Map("map"); // create base layer var layer = new OpenLayers.Layer.WMS("OpenLayers WMS", "http://labs.metacarta.com/wms/vmap0", {"layers": "basic"} ); map.addLayer(layer); // create vector layer vector = new OpenLayers.Layer.Vector("Vector", { protocol: protocol, strategies : [new OpenLayers.Strategy.Fixed()], eventListeners: { featuremodified: function(obj) { displayStatus(); } } }); map.addLayer(vector); // create modify feature control modify = new OpenLayers.Control.ModifyFeature(vector); // create editing panel var panel = new OpenLayers.Control.Panel({ displayClass: "olControlEditingToolbar" }); var navigation = new OpenLayers.Control.Navigation({ eventListeners: { activate: function(obj) { modify.activate(); }, deactivate: function(obj) { modify.deactivate(); } } }); var editing = new OpenLayers.Control.DrawFeature( vector, OpenLayers.Handler.Polygon, { displayClass: "olControlDrawFeaturePolygon", eventListeners: { featureadded: function(obj) { obj.feature.state = OpenLayers.State.INSERT; displayStatus(); } } }); panel.addControls([navigation, editing]); panel.defaultControl = navigation; // add controls to the map map.addControl(modify); map.addControl(panel); // center the map map.setCenter(new OpenLayers.LonLat(5, 40), 5); } function displayResult(txt) { if (window.resultDomElement === undefined) { window.resultDomElement = OpenLayers.Util.getElement("last-result"); } resultDomElement.innerHTML = txt; displayStatus(); } function displayStatus() { if (window.statusDomElement === undefined) { window.statusDomElement = OpenLayers.Util.getElement("status"); } var createCnt = 0; var updateCnt = 0; var deleteCnt = 0; var i, len, state; for (i = 0, len = vector.features.length; i < len; i++) { state = vector.features[i].state; if (state == OpenLayers.State.INSERT) { createCnt++; } else if (state == OpenLayers.State.UPDATE) { updateCnt++; } else if (state == OpenLayers.State.DELETE) { deleteCnt++; } } statusDomElement.innerHTML = createCnt + " features to create, " + updateCnt + " features to update, " + deleteCnt + " features to delete"; } function GearsIsSupported() { if (!protocol.supported()) { OpenLayers.Console.userError("You must install Gears prior to using this example"); return false; } return true; } function featuresWithState(state) { var list = []; var i, len, feature; for (i = 0, len = vector.features.length; i < len; i++) { feature = vector.features[i]; if (feature.state == state) { list.push(feature); } } return list; } function _sync() { if (!GearsIsSupported()) { return; } var resp = protocol.read(); if (!resp.success()) { OpenLayers.Console.error("reading from Gears DB failed"); return; } vector.destroyFeatures(); if (!resp.features || resp.features.length <= 0) { displayResult("No features to read"); return; } vector.addFeatures(resp.features); displayResult("features successfully read"); } function _commit() { if (!GearsIsSupported()) { return; } var error = false; function callback(resp) { if (error) { return; } if (!resp.success()) { OpenLayers.Console.error("Commiting to Gears DB failed"); error = true; return; } modify.selectControl.unselectAll() if (resp.reqFeatures) { vector.destroyFeatures(resp.reqFeatures); } if (resp.features) { vector.addFeatures(resp.features); } } if (vector.features.length > 0) { protocol.commit(vector.features, { "create": { callback: callback }, "update": { callback: callback }, "delete": { callback: callback } }); if (!error) { displayResult("features successfully committed"); } } else { displayResult("no features to commit"); } } function _delete() { if (!GearsIsSupported()) { return; } var feature = vector.selectedFeatures[0]; if (feature) { modify.selectControl.unselectAll() feature.state = OpenLayers.State.DELETE; displayStatus(); } } </script> </head> <body onload="init()"> <h1 id="title">Gears Protocol Example</h1> <div id="tags"> </div> <p id="shortdesc"> Shows the usage of the Gears protocol. </p> <div class="float-left"> <div id="map" class="smallmap"></div> </div> <div> <a href="javascript:_sync()">Sync</a> <p>The Sync link destroys the features currently in the layer, reads features from the Gears database, and adds them to the layer. Uncommitted features will be lost.</p> <a href="javascript:_commit()">Commit</a> <p>The Commit link commits to the Gears database the features that are marked as INSERT, UPDATE or DELETE.</p> <a href="javascript:_delete()">Delete</a> <p>The Delete link marks the selected feature as DELETE. To select a feature click choose the navigation control in the editing toolbar.</p> </div> <div style="margin-top: 30px"> <p>Status: <span id="status"></span></p> <p>Result: <span id="last-result"></span></p> </div> <div class="clear-left" id="docs"> <p>This example demonstrates the usage of OpenLayers Gears protocol to read/create/update/delete features from/to the Gears database. <a href="http://gears.google.com/">Gears</a> must obviously be installed in your browser for this example to work.</p> </div> </body> </html> |