0

I want to add test feature on my ArcGIS map using feature layer. I create layer using Graphic as a source. After a lot of googling I create my point the following way:

require([
"esri/Map",
"esri/views/MapView",
"esri/layers/TileLayer",
"esri/layers/FeatureLayer",
"esri/Graphic",
"esri/geometry/Point",
"esri/symbols/SimpleMarkerSymbol",
"esri/geometry/SpatialReference",
"esri/renderers/SimpleRenderer",
"dojo/domReady!"
],
    function (Map, MapView, TileLayer, FeatureLayer, Graphic, Point, SimpleMarkerSymbol, SpatialReference, SimpleRenderer) {
        var map = new Map({
            layers: [
                new TileLayer({
                    url: "https://gis.ngdc.noaa.gov/arcgis/rest/services/arctic_ps/arctic_basemap/MapServer"
                })
            ]
        });

        var viewSpatialReference = 3995;

        var point = new Point({
            x: 1000000,
            y: -1000000,
            spatialReference: new SpatialReference({ wkid: viewSpatialReference })
        });

        var renderer = new SimpleRenderer({
            symbol: new SimpleMarkerSymbol({
                size: 100000,
                color: "#FF4000",
                outline: {
                    color: [255, 64, 0, 0.4],
                    width: 7
                }
            })
        });
        var graphic = new Graphic({
            geometry: point,
            attributes: {
                id: 0
            }
        });

        var fields = [
            {
                name: "id",
                alias: "id",
                type: "oid"
            }
        ];
        var featureLayer = new FeatureLayer({
                source: graphic,
                fields: fields,
                objectIdField: "id",
                renderer: renderer,
                spatialReference: {
                    wkid: viewSpatialReference
                },
                geometryType: "point"
        });

        //map.on("load", function () {
              console.log('load event called');
              map.layers.add(featureLayer);
        //});

        var centerPoint = {
            x: 1000000,
            y: -1000000,
            spatialReference: viewSpatialReference
        };

        var viewOptions = {
            container: "map",
            map: map,
            viewingMode: "local",
            spatialReference: viewSpatialReference,
            scale: 12000000,
            center: centerPoint
        };

        var view = new MapView(viewOptions);
    });

Map is loading fine, but test point isn't appear. If I use this code, I get two errors in console:

[esri.layers.FeatureLayer] #load() Failed to load layer (title: 'null', id: '16a2638ea4d-layer-1') TypeError: this.source.load is not a function at Object.r.createGraphicsSource (dojo.js:2178)

[esri.views.LayerViewFactory] Failed to create view for layer 'null, id:16a2638ea4d-layer-1' of type 'feature'. {error: TypeError: this.source.load is not a function at Object.r.createGraphicsSource (https://js.arcgis.com/4.10/dojo/dojo.js:2178:377)}

I think cause of this errors is map is not yet loaded when I create try to create layer. I commented map.on("load", function (){}) call as if I use it there is no errors in console at all. I research script using browser debugger and find out that function body isn't executed for some reason.

Maybe I miss something in layer creating. I set huge size for marker to exclude possibility, that marker is just small. I also make example in Fiddle for your comfort.

2

First, your graphics should be wrapped in an array. Simply change

var featureLayer = new FeatureLayer({
                source: graphic,
                fields: fields,
                objectIdField: "id",
                renderer: renderer,
                spatialReference: {
                    wkid: viewSpatialReference
                },
                geometryType: "point"
        });

to

var featureLayer = new FeatureLayer({
                source: [graphic],
                fields: fields,
                objectIdField: "id",
                renderer: renderer,
                spatialReference: {
                    wkid: viewSpatialReference
                },
                geometryType: "point"
        });

Next, I'm not sure how familiar you were with the 3.x JS API, but in that map.on("load"... was a common workflow.

I haven't worked with 4.x for a while, but I think you want to apply the same idea, but use the view.when() syntax. Instead of map.on("load"... use view.when(... after you created the view at the bottom of the script.

working fiddle: https://jsfiddle.net/vgf5cryz

  • Thanks, this work for me too. However, map.on("load", function (){}) call still doesn't execute its body. It's strange that adding marker work without waiting for map loading – QuarK Apr 17 at 8:39
  • I'm not sure how familiar you were with the 3.x JS API, but in that map.on("load"... was a common workflow. I haven't worked with 4.x for a while, but I think you want to apply the same idea, but use the view.when() syntax. See the fiddle here: jsfiddle.net/vgf5cryz Instead of map.on("load"... I used view.when after you created the view at the bottom of the script. – countrytoast Apr 17 at 13:23
  • I am totally new to ArcGIS JS API, thanks for example. Maybe I will use it, for now I struggle with GeometryService.project and Promise as my map not in webmercator. – QuarK Apr 17 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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