Select to view content in your preferred language

Count featurelayer attributes displayed in a map

2386
10
Jump to solution
01-14-2014 03:32 AM
VikasNeekhra
Emerging Contributor
I have created a basemap and then created a featureLayer. Now I have to count one of the featureLayer attributes value when moving the map and render it in some part the html page using innerHTML.

Suppose on onLoad of a page we get 100 featureLayers, now when I move the map a little bit up then the featureLayer will be 40, so I have to count that and render that value in a page. So whenever we move the map then we have to count the featureLayer displayed in the screen(map) and render it.

Please help in suggesting how we can achieve this, any suggestion will be highly appreciated.

Thanks,
Vikas
0 Kudos
1 Solution

Accepted Solutions
JohnathanBarclay
Regular Contributor
This should do the trick:

map.on("extent-change", function(extent){  var query = new Query();  query.geometry = extent.extent;  flayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(result){   console.log(result.length);//This is the number of features in current view  }); });

View solution in original post

0 Kudos
10 Replies
JasonZou
Frequent Contributor
I am confused with the info provided. Are you trying to count the number of feature layers loaded or the number of features of one specific feature layer?
0 Kudos
JakubMalec
Deactivated User
All you need to do is to count how many graphics are present on the layer each time you change the extent.

To be able to do that, you have to initialize your FeatureLayer with mode set to MODE_ONDEMAND - it will get all features from current extent on every extent change.

Then, you need to listen to "update-end" event on the layer. Each time the event fires, you can fill your counter's innerHTML with following value:
yourFeatureLayerVar.graphics.length;
0 Kudos
JohnathanBarclay
Regular Contributor
This should do the trick:

map.on("extent-change", function(extent){  var query = new Query();  query.geometry = extent.extent;  flayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(result){   console.log(result.length);//This is the number of features in current view  }); });
0 Kudos
VikasNeekhra
Emerging Contributor
Thanks for all the responses!

I tried update-end event but it is not getting called.
dojo.connect(featureLayer, "update-end", function(evt) {...});


also tried extent-change event on map as below, but it is giving me total events...
map.on("extent-change", function() {
  //var len=Object.keys(featureLayer.toJson().featureSet.features).length;
  //alert("hi="+len);
  alert(featureLayer.graphics.length);
 });


I am just trying to count the no. of attributes value of the layers drawn on a map and if we move the move then the layers on that map will change, so the count should also change.

Thanks all for your help!
0 Kudos
JonathanUihlein
Esri Regular Contributor
Thanks for all the responses!

I tried update-end event but it is not getting called.
dojo.connect(featureLayer, "update-end", function(evt) {...});


also tried extent-change event on map as below, but it is giving me total events...
map.on("extent-change", function() {
        //var len=Object.keys(featureLayer.toJson().featureSet.features).length;
        //alert("hi="+len);
        alert(featureLayer.graphics.length);
    });


I am just trying to count the no. of attributes value of the layers drawn on a map and if we move the move then the layers on that map will change, so the count should also change.

Thanks all for your help!


I think it might be:

dojo.connect(featureLayer, "onUpdateEnd", function(evt) {...});


or

on(featureLayer, "update-end", function(evt) {...});
0 Kudos
JohnathanBarclay
Regular Contributor
I am just trying to count the no. of attributes value of the layers drawn on a map and if we move the move then the layers on that map will change, so the count should also change.


In that case you need to iterate through the selectFeatures callback result, something like this:

map.on("extent-change", function(extent){
 var query = new Query();
 query.geometry = extent.extent;
 flayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(result){
  var count = 0;
  array.forEach(result, function(feature){
   count = count + feature.attributes.YOURFIELD;
  });
  console.log(count);
 });
});
0 Kudos
VikasNeekhra
Emerging Contributor
All you need to do is to count how many graphics are present on the layer each time you change the extent.

To be able to do that, you have to initialize your FeatureLayer with mode set to MODE_ONDEMAND - it will get all features from current extent on every extent change.

Then, you need to listen to "update-end" event on the layer. Each time the event fires, you can fill your counter's innerHTML with following value:
yourFeatureLayerVar.graphics.length;


Hello Jakub,

I have set the feature layer mode to MODE_ONDEMAND and wrote below event which fires on every drag of a map but it is giving the total length of the featureLayer, but I need to find the count of whatever feature layers are in the map. Suppose there are 100 FL and when we drag the map then the FL is 40 then I should get the count as 40, but not 100.

So two things I am trying to fetch from featureLayer when dragging the map.
1. the count of FL whatever we have in the screen
2. count of one of the attributes of FL

dojo.connect(featureLayer, "onUpdateEnd", function(evt) {
  //alert(featureLayer.toJson());
  alert("graphics len="+featureLayer.graphics.length);
0 Kudos
VikasNeekhra
Emerging Contributor
In that case you need to iterate through the selectFeatures callback result, something like this:

map.on("extent-change", function(extent){
 var query = new Query();
 query.geometry = extent.extent;
 flayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(result){
  var count = 0;
  array.forEach(result, function(feature){
   count = count + feature.attributes.YOURFIELD;
  });
  console.log(count);
 });
});


Hello Johnathan,

I tried above code which is getting called on every drag of a map, but it is not going inside below code.

featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(result){
   alert("inside");
   var count = 0;
   array.forEach(result, function(feature){
    count = count + feature.attributes.CustomersAffected;
   });
   alert("CustomersAffected="+count);
  });
0 Kudos
JohnathanBarclay
Regular Contributor
Hello Johnathan,

I tried above code which is getting called on every drag of a map, but it is not going inside below code.

featureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW, function(result){
   alert("inside");
   var count = 0;
   array.forEach(result, function(feature){
    count = count + feature.attributes.CustomersAffected;
   });
   alert("CustomersAffected="+count);
  });


Have you required "dojo/_base/array"? You need this module to execute the forEach method.
0 Kudos