OpenLayers WFS Sum Attributed and loadend event
HTML
<script src="http://cdnjs.cloudflare.com/ajax/libs/openlayers/2.11/OpenLayers.js"></script>
<body onload="init()">
<p>OpenLayers WFS with select control and Popups</p>
<div id="map"></div>
</body>
CSS
#map {
width: 400px;
height: 600px;
}
JavaScript
var map;
var layer, wms, selectControl;
function init() {
map = new OpenLayers.Map('map', {
projection: "EPSG:25832",
tileSize: new OpenLayers.Size(256, 256),
units: "m",
maxExtent: new OpenLayers.Bounds(120000, 5661139.2, 958860.8, 6500000),
resolutions: [1638.4, 819.2, 409.6, 204.8, 102.4, 51.2, 25.6, 12.8, 6.4, 3.2, 1.6, 0.8, 0.4, 0.2]
});
wms = new OpenLayers.Layer.WMS("Baggrundskort (FOT)", ["http://kortforsyningen.kms.dk/"], {
servicename: "topo_fot",
layers: "basis",
format: "image/jpeg",
login: "gmcb3",
password: "3bcmg"
}, {
isBaseLayer: true,
buffer: 1,
visibility: false,
displayInLayerSwitcher: true
});
layer = new OpenLayers.Layer.Vector("WFS", {
strategies: [new OpenLayers.Strategy.BBOX()],
protocol: new OpenLayers.Protocol.WFS({
url: "http://wfs.gmpark.dk/wfs/albertslund/ows?service=WFS&typeName=albertslund:elements",
featureType: "albertslund:elements",
featureNS: "albertslund",
srsName: "EPSG:25832",
geometryName: "albertslund:geometry"
})
});
layer.events.register("loadend", map, function () {
console.log("number of Features: " + layer.features.length);
var layer_sum = 0;
for (var i = 0; i < layer.features.length; i++) {
layer_sum = layer_sum + parseInt(layer.features[i].attributes.LABELRANK);
}
console.log(layer.features[0].attributes);
console.log("Sum of attribute LABELRANK= " + layer_sum);
});
map.addLayers([wms, layer]);
map.zoomToExtent(new OpenLayers.Bounds(708087, 6171295, 713490, 6178841));
selectControl = new OpenLayers.Control.SelectFeature(map.layers[1], {
onSelect: onFeatureSelect,
onUnselect: onFeatureUnselect
});
map.addControl(selectControl);
selectControl.activate();
}
function onPopupClose(evt) {
selectControl.unselect(selectedFeature);
}
function...