Select to view content in your preferred language

Not able to add Sketch widget

890
2
10-23-2024 09:02 AM
olega
by
New Contributor

Hi,

I'm trying to add Sketch widget to my map as well as add some polygons based on info previously submitted.

When adding the widget an error is shown in the console and widget is not visible. Used a lot of time to figure it out without any luck. Hopefully someone can help me.

Here is the error message:

Screenshot 2024-10-23 at 17.58.16.png

And here is my complete code:

 

require([
    "esri/widgets/Sketch",
    "esri/Map",
    "esri/views/MapView",
    "esri/Graphic",
    "esri/layers/GraphicsLayer"
], (Sketch, Map, MapView, Graphic, GraphicsLayer) => {

    let graphicsLayer = new GraphicsLayer();

    var jsonString = "${sys_json_object}"; // server-side directive to import json
    var jsonObj = JSON.parse(jsonString);


    var map = new Map({
        basemap: "streets-navigation-vector",
        layers: [graphicsLayer]
    });

    var view = new MapView({
        container: "viewDiv",
        map: map,
        zoom: jsonObj.zoom,
        center: [jsonObj.center.longitude, jsonObj.center.latitude]
    });

    view.when(() => {

        let sketch = new Sketch({
            layer: graphicsLayer,
            view: view
        });

        view.ui.add(sketch, 'top-right');


        var polygon = {
            type: "polygon",
            rings: jsonObj.rings,
            spatialReference: jsonObj.spatialReference
        };

        var fillSymbol = {
            type: "simple-fill",
            color: [227, 139, 79, 0.8], // Orange, semi-transparent
            outline: {
                color: [255, 255, 255],
                width: 1
            }
        };


        var polygonGraphic = new Graphic({
            geometry: polygon,
            symbol: fillSymbol
        });

        graphicsLayer.graphics.add(polygonGraphic);



    });
});

 

If I comment out line 34, the polygon is drawn correctly, but obviously no widget shown. 

Tags (2)
0 Kudos
2 Replies
LittleTurtle
Occasional Contributor

Hello, maybe try adding your polygonGraphic before creating your Sketch

0 Kudos
RamaniGanason
Regular Contributor

Can post your source where the viewDiv is created. The problem dont seems like to be with your Sketch.

 

0 Kudos