Load WFS with jQuery Ajax and Check Number of loaded features
HTML
<script src="http://openlayers.org/api/OpenLayers.js"></script>
<body>
<div id="map" class="smallmap"></div>
</body>
CSS
#map {
width: 400px;
height: 400px;
border: 1px solid #dddddd;
}
JavaScript
var map = new OpenLayers.Map('map', {
maxExtent: OpenLayers.Bounds.fromString("-180,-90,180,90"),
projection: new OpenLayers.Projection("EPSG:4326"),
displayProjection: new OpenLayers.Projection("EPSG:4326")
});
var basic = new OpenLayers.Layer.WMS("Basic", "http://vmap0.tiles.osgeo.org/wms/vmap0", {
layers: 'basic'
}, {
singleTile: true,
ratio: 1,
transitionEffect: 'resize',
opacity: 0.5
});
var url = 'http://geospatial.ga:8080/geoserver/newdata/ows?service=WFS&' +
'version=1.1.0&request=GetFeature&typeName=newdata:missouri_building&' +
'outputFormat=text/javascript&format_options=callback:loadFeatures&maxFeatures=10000&' +
'srsname=EPSG:4326';
$.ajax({
url: url,
dataType: 'jsonp',
success: function (result) {
console.log(result);
},
error: function (data) {
console.log(data);
}
});
var wfsSuscribers = new OpenLayers.Layer.Vector("WFS");
wfsSuscribers.events.register('featuresadded', wfsSuscribers, function () {
console.log("Number of Features:" + wfsSuscribers.features.length);
map.zoomToExtent(wfsSuscribers.getDataExtent());
});
map.addLayers([basic, wfsSuscribers]);
map.zoomToMaxExtent();
var i_o_options = {
'internalProjection': map.projection,
'externalProjection': new OpenLayers.Projection("EPSG:4326")
};
var geojson_format = new OpenLayers.Format.GeoJSON(i_o_options);
var loadFeatures = function (response) {
wfsSuscribers.addFeatures(geojson_format.read(response, "FeatureCollection"));
};
window.loadFeatures = loadFeatures;
map.setCenter(new OpenLayers.LonLat(11.52, 3.83), 3);