Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I got the sample called "Feature Layer with ONDEMAND mode" in the ArcGIS for js resource center. This example just adds layer with rivers to the map.

I want to use my own features instead of using features from the featureService like in this example. In order to do this I need to use another constructor of FeatureLayer so I modified the code like this:

  function initOperationalLayer(map) {

    var featureCollection = {
        layerDefinition: {
          "geometryType": "esriGeometryPoint",
          "fields": [
          {
            "name": "OBJECTID",
            "type": "esriFieldTypeOID"
          },
          {
            "name": "text",
            "type": "esriFieldTypeString"
          },
          {
            "name": "address",
            "type": "esriFieldTypeString"
          },
          {
            "name": "Shape",
            "type": "esriFieldTypeGeometry"
          }
          ]
        },
        featureSet: 
        [
        {   OBJECTID : 1, 
            text : 'a', 
            address : 'b', 
            Shape :  new esri.geometry.Point(-80.12468662, 40.42756484, map.spatialReference)
        }
        ]
    };
    var featureLayer = new esri.layers.FeatureLayer(featureCollection, {
        mode: esri.layers.FeatureLayer.MODE_ONDEMAND
    });

    map.addLayer(featureLayer);
    map.infoWindow.resize(150,105);
    mapLayers.push(featureLayer);  //this client side map layer is the maps graphics layer
  }

Unfortunately, it doesn't work. I got errors like "TypeError: _19c is undefined in utils.xd.js(line 14)". It is obfuscated code so i can't understand what't the problem. Any ideas?


The right code is the following one:

    var jsonFS = {
        "displayFieldName": "Name",
        "fieldAliases": {
            "Name": "Name"
        },
        "geometryType": "esriGeometryPoint",
        "spatialReference": {
            "wkid": 4326
        },
        "fields": [{
            "name": "Name",
            "type": "esriFieldTypeString",
            "alias": "Name",
            "length": 255
        }],
        "features": [{
            "attributes": {
                "Name": "CHINA: YUNNAN PROVINCE; VIETNAM: HANOI"
            },
            "geometry": {
                "x": -10602460.248958504,
                "y": 4716882.997019428
            }
        }]
    };
    var fs = new esri.tasks.FeatureSet(jsonFS);

    var featureCollection = {
        layerDefinition: {
          "geometryType": "esriGeometryPoint",
          "fields": [
          {
            "name": "Name",
            "type": "esriFieldTypeString",
            "alias": "Name"
          }
          ]
        },
        featureSet: fs
    };

var featureLayer = new esri.layers.FeatureLayer(featureCollection, {
        mode: esri.layers.FeatureLayer.MODE_ONDEMAND
    });
share|improve this question
    
does this code work in another mode like MODE_SNAPSHOT? –  CaptDragon Apr 21 '11 at 13:22
    
No, nothing helps. I tried also to use esri.FeatureSet class instead of this array. I always have errors. –  Kirill Lykov Apr 21 '11 at 13:42
    
Do you have a a public link to the code you are trying to run? –  Sasa Ivetic Apr 21 '11 at 14:10
add comment

3 Answers

up vote 2 down vote accepted

'featureCollection' expects a 'features' attribute which is a FeatureSet, and not a featureSet attribute like you are doing:

var featureCollection = {
    layerDefinition: {
      "geometryType": "esriGeometryPolyline",
      "fields": [
          {
            "name": "OBJECTID",
            "type": "esriFieldTypeOID"
          },
          {
            "name": "text",
            "type": "esriFieldTypeString"
          },
          {
            "name": "address",
            "type": "esriFieldTypeString"
          }
      ]
    },
    features: 
    [
        {
            "attributes": {
                OBJECTID : 1, 
                text : 'a', 
                address : 'b', 
            },
            "geometry": { "x": -80.12468662, "y": 40.42756484 }
        }
    ]
};

For future debugging, I suggest copying the ESRI obfuscated code, and making it more readable by using JS Beautifier. Then just search for the error (in my case error was with variable _190). That should help you out with figuring out where and why the error occurs.

share|improve this answer
    
Not exactly but more or less right, thank you for the JS Beautifier - I'm new in js so didn't know about this. –  Kirill Lykov Apr 21 '11 at 14:45
add comment

maybe it's expecting the alias? Give it a shot.

...
"fields": [
          {
            "name": "OBJECTID",
            "type": "esriFieldTypeOID",
            "alias": "ID"
          },
...
share|improve this answer
add comment

You could try moving the spatial reference in the layer definition:

...
  "spatialReference" : {
    "wkid" : 4326
  }
...

And adding quotes around field names and values, and defining the geometry JSON style:

...  
{   
    "OBJECTID" : 1, 
    "text" : "a", 
    "address" : "b", 
    "geometry" : {
      "x" : -80.12468662,
      "y" : 40.42756484
    }
 }
...

I got inspired by JSON response to querying a feature service e.g.: http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/Since_1970/MapServer/0/query?text=&geometry=&geometryType=esriGeometryPoint&inSR=&spatialRel=esriSpatialRelIntersects&relationParam=&objectIds=&where=MAGNITUDE%3E7&time=&returnIdsOnly=false&returnGeometry=true&maxAllowableOffset=&outSR=&outFields=&f=pjson

Good luck.

share|improve this answer
    
it is not this way you described, but anyway thank you for this json - it helped me to find solution. –  Kirill Lykov Apr 21 '11 at 14:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.