Select to view content in your preferred language

FeatureLayer from FeatureCollection

5991
15
Jump to solution
12-15-2015 05:53 PM
LefterisKoumis
Frequent Contributor

I get the Converting circular structure to JSON error when I create the feature layer, thislayer. Any ideas? Thank you..

var requestHandle = esriRequest({

            "url": myurl, //I set the variable to a featureservice url

            "content": {

              "f": "json"

            },

            "callbackParamName": "callback"

          });

 

          myfields =requestHandle.then(this.requestSucceeded, this.requestFailed);

 

   var layerDefinition = {

        "geometryType": "esriGeometryPoint",

        "fields": myfields,

         "objectIdField": "OBJECTID",

  }     

  var featureCollection = {

  layerDefinition: layerDefinition,

  featureSet: myfeatureSet  //it's derived from a query

  };

  var thislayer = new esri.layers.FeatureLayer(featureCollection, {

  id: "query layer",

        mode: FeatureLayer.MODE_SNAPSHOT,

  outFields:["*"]}

  );

 

  this._openResultInAttributeTable(thislayer);

      

      },

      requestSucceeded:function(response, io){

            fields = array.map(response.fields);

  return fields;

   },

        requestFailed: function(error, io){

  console.log("got an error");       

  }, 

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

  Your code was a mess. You need to spend more time cleaning up your code so you don't get confused in there. Here is a pretty clean working version.

View solution in original post

0 Kudos
15 Replies
LefterisKoumis
Frequent Contributor

I modified it but still get the same error.

  mapServiceUrl= <map service>;

  var requestHandle = esriRequest({

            "url": dojo.replace("{0}/layers",[mapServiceUrl]),

            "content": {

              "f": "json"

            },

            "callbackParamName": "callback"

        });

       requestHandle.then(lang.hitch(this, function(response){

  dojo.forEach(response.layers, function(layerInfo) {                

              console.log(layerInfo); //checking layer name           

            if(layerInfo.fields) {                

              dojo.forEach(layerInfo.fields, function(fieldInfo) {

  console.log(fieldInfo); //checking fields              

              }); 

            }

  myfields =layerInfo.fields;

        });     

  }));

     

  var featureCollection = {

          "layerDefinition": null,

          "featureSet": {

            "features": [],

            "geometryType": "esriGeometryPoint"

          }

        };

  layerDefinition = {

          "geometryType": "esriGeometryPoint",

          "objectIdField": "ObjectID",         

   "field": [myfields]

  };

  var featureCollection = {

  layerDefinition: layerDefinition,

  featureSet: featureSet

  }; 

  var thislayer = new esri.layers.FeatureLayer(featureCollection);

  },

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

  Something more like this:

var mapServiceUrl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Louisville/LOJIC_LandRecords_Louisville/M..." ;
var thislayer = null, featureCollection = null;
esriRequest({
  url: mapServiceUrl,
  content: {
    f: 'json'
  },
  handleAs: 'json',
  callbackParamName: 'callback'
}).then(lang.hitch(this, function (layerInfo) {
  featureCollection = {
    layerDefinition: layerInfo,
    featureSet: null
  };
  thislayer = new FeatureLayer(featureCollection);
}));
0 Kudos
LefterisKoumis
Frequent Contributor

Thank you.

It seems that the featureSet was causing the error. But how do I define the new layer based on the query selection. By defining the  featureSet: null then the selection is null.

Also, when I pass on thislayer to the _openResultInAttributeTable function, I get the error that it cannot read the 'name' of undefined even though the currentlayer is not null!

_openResultInAttributeTable: function (currentLayer) { 

        var aLayer = { 

          layerObject: currentLayer, 

          title: currentLayer.name, 

          id: currentLayer.id, 

          getLayerObject: function () { 

            var def = new Deferred(); 

            if (this.layerObject) { 

              def.resolve(this.layerObject); 

            } else { 

              def.reject("layerObject is null"); 

------------

---------

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

  The code provided in my last reply will setup the FeatureLayer (only the shell) for you then you have to add features to it (as I do in the eSearch). The name is null because you have not defined it yet. you should notice that I set several properties to the layerInfo once I receive it:

layerInfo.name = this.nls.search + ' ' + this.nls.results + ': ' + layerConfig.name;
layerInfo._titleForLegend = layerInfo.name;
layerInfo.minScale = 0;
layerInfo.maxScale = 0;
layerInfo.effectiveMinScale = 0;
layerInfo.effectiveMaxScale = 0;
layerInfo.defaultVisibility = true;
0 Kudos
LefterisKoumis
Frequent Contributor

Sorry to drag it on, but it is confusing. Why do I need to assign a name to the layerinfo when it already has it?

I verified it by using the code below. So by declaring

layerDefinition: layerInfo,

in the featurecollection the name should be passed on as well since I will use the same layerinfo for the new featurelayer as the myurl. However it is not passed on when a featurelayer is created using that feature collection. What am I  missing here? Thank you for your patience.

  esriRequest({

  url: myurl,

  content: {

  f: 'json'

  },

  handleAs: 'json',

  callbackParamName: 'callback'

  }).then(lang.hitch(this, function (layerInfo) {

  console.log(layerInfo.name);  //name is printed in console

                 }

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

   I really have no idea then. As always working with portions of code is difficult as I have to assume how you are handling other parts.

0 Kudos
LefterisKoumis
Frequent Contributor

Ok. I didn't want to post a long script. I will create a smaller version of it and test it again. If still has problems, I will post it Thank you for your commitment helping others.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

   You can always zip the widget and then attach the zip using the advanced editor.

0 Kudos
LefterisKoumis
Frequent Contributor

Ok. Here you go.

The ultimate goal is to pass on the thislayer to the _openResultInAttributeTable so it can publish the table with the query results. As you can see from the line 276, the layer has the layer name, however the _openResultInAttributeTable cannot access it. The second issue is how to pass on the featureSet of the quesry results to the feature collection. When I including it it causes the Converting circular structure to JSON error.

Thank you.

0 Kudos