Wednesday, 15 February 2017

FeatureLayer creating in javascript ArcGIS API



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

});

Answer



'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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...