[GIS] Creating FeatureLayer from JSON data using ArcGIS API for JavaScript 4

arcgis-javascript-api-4feature-layerjsonlayers

I am trying to create a FeatureLayer from a JSON object that will be fetched from another service. I am not looking for something complicated, just something that works.

var layer = new FeatureLayer({
    fields: [{
        name: "ObjectID",
        alias: "ObjectID",
        type: "oid"
    }, {
        name: "fltid",
        alias: "FltId",
        type: "string"
    }, {
        name: "name",
        alias: "Name",
        type: "string"
    }],
    objectIdField: "ObjectID",
    geometryType: "polyline",
    spatialReference: { wkid: 4326 },
    source: [{
            geometry: new Polyline({
                paths: [
                    [-100, 38],
                    [-77, 35]
                ]
            }),
            attributes: {
                ObjectID: 1,
                name: "KATL",
                fltid: "UAL1"
            }
        },
        {
            geometry: new Polyline({
                paths: [
                    [-77, 35],
                    [-120, 40]
                ]
            }),
            attributes: {
                ObjectID: 2,
                name: "KZBW",
                fltid: "SW999"
            }
        },
        {
            geometry: new Polyline({
                paths: [
                    [-120, 40],
                    [-100, 38]
                ]
            }),
            attributes: {
                ObjectID: 3,
                name: "WKRP",
                fltid: "Fever1"
            }
        }
    ],
});

I was expecting three lines to be drawn, but nothing is shown and the error console is empty.

I am using the version 4 API and there are very few examples and most of them broken.

Best Answer

Just for the record, you have to set a renderer on the FeatureLayer for it to be shown. Otherwise your code snippet looks good. I just took it with no modification, added a renderer and it gets displayed: https://codepen.io/anon/pen/RyBjav?&editors=100

As for examples, there are two samples that show how to add a GeoJSON to the map: https://developers.arcgis.com/javascript/latest/sample-code/?search=GeoJSON But basically it's just a FeatureLayer where you set the source, fields, geometryType and objectIDField.