Problems creating a feature layer from a feature collection

3308
3
Jump to solution
02-12-2013 01:54 PM
StephenLead
Regular Contributor III
Is anyone able to explain why the attached code isn't working? Also available live here.

I'm trying to create a featureLayer from a featureCollection based on the results of a query.

I'm finding that the featureLayer is apparently created, and I can see that it contains graphics (map.getLayer("query layer").graphics.length shows there are 167 features in the new layer), but they are not visible on the map. There should be a polygon shown in the centre of the map.

The relevant code is:
queryTask.execute(query, selectShowResults, SQLerror);  function selectShowResults(results) {     //Create a feature layer from the results     var featureSet = new esri.tasks.FeatureSet({         features: results.features     });          var fields = [         {             "name": "NAME",             "type": "esriFieldTypeString",             "alias": "Name"         },         {             "name": "OBJECTID",             "type": "esriFieldTypeOID",             "alias": "OBJECTID"         }     ]          var layerDefinition = {       "geometryType": "esriGeometryPolygon",       "fields": fields,       "objectIdField": "OBJECTID",     }       var featureCollection = {       layerDefinition: layerDefinition,       featureSet: featureSet     };          var layer = new esri.layers.FeatureLayer(featureCollection, {         id: "query layer",         mode: esri.layers.FeatureLayer.MODE_SNAPSHOT     })          var renderer = new esri.renderer.SimpleRenderer(         new esri.symbol.SimpleFillSymbol("solid", null, new dojo.Color([255, 0, 255, 0.75])     ));          layer.setRenderer(renderer);     map.addLayer(layer);                 }


Thanks,
Steve
0 Kudos
1 Solution

Accepted Solutions
StephenLead
Regular Contributor III

The query that I was using wasn't returning a feature within the map extent, so it was never going to work. I've updated the sample with a new extent and slightly different query, and it's working OK now.

View solution in original post

0 Kudos
3 Replies
StephenLead
Regular Contributor III

Stepping through it, I can see that this line:

var featureSet = new esri.tasks.FeatureSet({
  features: results.features
});

is stripping the geometry from the results, meaning that the feature layer has no geometry.

function selectShowResults(results) {
 //Create a feature layer from the results
 
 var graphics = [];
 var sfs = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID,
  new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT,
  new dojo.Color([255,0,0]), 2),new dojo.Color([255,255,0,0.25]));
  
 var infoTemplate = new esri.InfoTemplate();
 for (var i = 0; i < results.features.length; i++) {
  var feature = results.features;
  var graphic = new esri.Graphic(feature.geometry,sfs,feature.attributes,infoTemplate);
  graphics.push(graphic);
 }
 
 var sr = new esri.SpatialReference(102100);
 var featureSet = new esri.tasks.FeatureSet({
  geometryType: "esriGeometryPolygon",
  features: graphics,
  spatialReference: sr
 });

Still no luck, though.... Thanks for any advice.

0 Kudos
StephenLead
Regular Contributor III

The query that I was using wasn't returning a feature within the map extent, so it was never going to work. I've updated the sample with a new extent and slightly different query, and it's working OK now.

0 Kudos
LefterisKoumis
Occasional Contributor III

Hello. The link you provided in your last post is not working. Can you post your solution? Thanks.

0 Kudos