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
});
-
does this code work in another mode like MODE_SNAPSHOT?CaptDragon– CaptDragon2011年04月21日 13:22:16 +00:00Commented Apr 21, 2011 at 13:22
-
No, nothing helps. I tried also to use esri.FeatureSet class instead of this array. I always have errors.Kirill Lykov– Kirill Lykov2011年04月21日 13:42:04 +00:00Commented Apr 21, 2011 at 13:42
-
Do you have a a public link to the code you are trying to run?Sasa Ivetic– Sasa Ivetic2011年04月21日 14:10:37 +00:00Commented Apr 21, 2011 at 14:10
4 Answers 4
'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.
-
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– Kirill Lykov2011年04月21日 14:45:27 +00:00Commented Apr 21, 2011 at 14:45
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.
-
it is not this way you described, but anyway thank you for this json - it helped me to find solution.Kirill Lykov– Kirill Lykov2011年04月21日 14:43:53 +00:00Commented Apr 21, 2011 at 14:43
maybe it's expecting the alias? Give it a shot.
...
"fields": [
{
"name": "OBJECTID",
"type": "esriFieldTypeOID",
"alias": "ID"
},
...
esri json to feature layer
Lots of api change over years. In 2018, you do this.
example arcgis rest api request url:
http://xxxx.com/xxx/rest/services/Maps_and_Indices/MapServer/2/query?f=pjson&returnCountOnly=false&outFields=*&outSR=4326&where=DISTRICT%20=%20%27129B149%27%20or%20TILE_NAME%20=%20%27129B149%27%20or%20TOOLTIP%20=%20%27129B149%27%20or%20NLA_URL%20=%20%27129B149%27
decode URL:
http://xxxx.com/xxx/rest/services/Maps_and_Indices/MapServer/2/query?f=pjson&returnCountOnly=false&outFields=*&outSR=4326&where=DISTRICT = '129B149' or TILE_NAME = '129B149' or TOOLTIP = '129B149' or NLA_URL = '129B149'
data: any arcgis rest api return (response)
example (data) :
{
"displayFieldName": "TILE_NAME",
"fieldAliases": {
"OBJECTID": "OBJECTID",
"DISTRICT": "DISTRICT",
"TILE_NAME": "TILE_NAME",
"TOOLTIP": "TOOLTIP",
"NLA_URL": "NLA_URL"
},
"geometryType": "esriGeometryPolygon",
"spatialReference": {
"wkid": 4326,
"latestWkid": 4326
},
"fields": [
{
"name": "OBJECTID",
"type": "esriFieldTypeOID",
"alias": "OBJECTID"
},
{
"name": "DISTRICT",
"type": "esriFieldTypeString",
"alias": "DISTRICT",
"length": 16
},
{
"name": "TILE_NAME",
"type": "esriFieldTypeString",
"alias": "TILE_NAME",
"length": 32
},
{
"name": "TOOLTIP",
"type": "esriFieldTypeString",
"alias": "TOOLTIP",
"length": 66
},
{
"name": "NLA_URL",
"type": "esriFieldTypeString",
"alias": "NLA_URL",
"length": 78
}
],
"features": [
{
"attributes": {
"OBJECTID": 1120,
"DISTRICT": "WESTLA",
"TILE_NAME": "129B149",
"TOOLTIP": "District: WESTLA\\nTile: 129B149",
"NLA_URL": "navigatela/reports/cadastral_map_index.cfm?pk=129B149"
},
"geometry": {
"rings": [
[
[
-118.45870522159555,
34.053800267113971
],
[
-118.44549967992378,
34.053813128253687
],
[
-118.44548872993352,
34.045569527150292
],
[
-118.45869298464309,
34.045556665428208
],
[
-118.45870522159555,
34.053800267113971
]
]
]
}
}
]
}
For example you want add arcgis rest api response( esri json) to feature layer
var search_layer_result = JSON.parse(data)
console.log(search_layer_result)
// ............ add esri json -> feature layer -> map ..............
var featureCollection = {
layerDefinition : search_layer_result,
featureSet : search_layer_result
};
search_result_featureLayer = new FeatureLayer(featureCollection);
//var symbol = new SimpleMarkerSymbol().setColor(new esri.Color([255,0,0,0.5]));
var renderer = new SimpleRenderer(your_Symbol);
search_result_featureLayer.setRenderer(renderer);
map.addLayer(search_result_featureLayer, your_layer_index_optional);
Explore related questions
See similar questions with these tags.