Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
  <title>
    Add or remove graphics from a FeatureLayer | Sample | ArcGIS API for
    JavaScript 4.17
  </title>

  <link rel="stylesheet" href="https://js.arcgis.com/4.17/esri/themes/light/main.css" />
  <script src="https://js.arcgis.com/4.17/"></script>
  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }

    #add-many {
      margin-bottom: 5px;
    }

    #actions {
      padding: 5px;
    }
  </style>

  <script>
    require([
      "esri/core/promiseUtils",
      "esri/core/watchUtils",
      "esri/Map",
      "esri/views/MapView",
      "esri/layers/FeatureLayer",
      "esri/Graphic"
    ], function(promiseUtils, watchUtils, Map, MapView,
      FeatureLayer, Graphic) {
      const map = new Map({
        basemap: "topo"
      });
      const view = new MapView({
        container: "viewDiv",
        map: map,
        zoom: 6,
        center: [-122.18, 37.49]
      });
      // create empty FeatureLayer
      const monumentLayer = new FeatureLayer({
        // create an instance of esri/layers/support/Field for each field object
        title: "National Monuments",
        fields: [{
            name: "ObjectID",
            alias: "ObjectID",
            type: "oid"
          },
          {
            name: "Name",
            alias: "Name",
            type: "string"
          },
          {
            name: "Type",
            alias: "Type",
            type: "string"
          }
        ],
        objectIdField: "ObjectID",
        geometryType: "point",
        spatialReference: {
          wkid: 4326
        },
        source: [], // adding an empty feature collection
        renderer: {
          type: "simple",
          symbol: {
            type: "web-style", // autocasts as new WebStyleSymbol()
            styleName: "Esri2DPointSymbolsStyle",
            name: "landmark"
          }
        },
        popupTemplate: {
          title: "{Name}"
        }
      });
      map.add(monumentLayer);
      // add buttons to the mapView
      view.ui.add(document.getElementById("actions"), "top-right");
      const addManyBtn = document.getElementById("add-many");
      addManyBtn.addEventListener("click", addManyFeatures);
      const remove = document.getElementById("remove");
      remove.addEventListener("click", removeFeatures);

      function getData() {
        var data = [{
            LATITUDE: 32.6735,
            LONGITUDE: -117.2425,
            TYPE: "National Monument",
            NAME: "Cabrillo National Monument"
          },
          {
            LATITUDE: 35.2234,
            LONGITUDE: -118.559,
            TYPE: "National Monument",
            NAME: "Cesar E. Chavez National Monument"
          },
          {
            LATITUDE: 37.6251,
            LONGITUDE: -119.085,
            TYPE: "National Monument",
            NAME: "Devils Postpile National Monument"
          },
          {
            LATITUDE: 35.2915,
            LONGITUDE: -115.0935,
            TYPE: "National Monument",
            NAME: "Castle Mountains National Monument"
          },
          {
            LATITUDE: 41.7588,
            LONGITUDE: -121.5267,
            TYPE: "National Monument",
            NAME: "Lava Beds National Monument"
          },
          {
            LATITUDE: 37.897,
            LONGITUDE: -122.5811,
            TYPE: "National Monument",
            NAME: "Muir Woods National Monument"
          },
          {
            LATITUDE: 41.8868,
            LONGITUDE: -121.3717,
            TYPE: "National Monument",
            NAME: "Tule Lake National Monument"
          }
        ];
        var graphics = [];
        var graphic;
        for (var i = 0; i < data.length; i++) {
          graphic = new Graphic({
            geometry: {
              type: "point",
              latitude: data[i].LATITUDE,
              longitude: data[i].LONGITUDE
            },
            attributes: data[i]
          });
          graphics.push(graphic);
        };
        return graphics;
      }

      function removeFeatures() {
        monumentLayer.queryFeatures().then((results) => {
          const deleteEdits = {
            deleteFeatures: results.features
          };
          applyEditsToLayer(monumentLayer, deleteEdits).then((results) => {
            if (results.deleteFeatureResults.length > 0) {
              console.log(results.deleteFeatureResults.length, "features have been removed");
            }
          }).catch(console.error);
        });
      }

      function addManyFeatures() {
        Promise.all([
          applyEditsToLayer(monumentLayer, {
            addFeatures: getData()
          }),
          applyEditsToLayer(monumentLayer, {
            addFeatures: getData()
          }),
          applyEditsToLayer(monumentLayer, {
            addFeatures: getData()
          }),
          applyEditsToLayer(monumentLayer, {
            addFeatures: getData()
          }),
          applyEditsToLayer(monumentLayer, {
            addFeatures: getData()
          })
        ]).then(() => {
          monumentLayer.queryFeatures().then((results) => {
            console.log(results.features.length, "features have been added.");
          }).catch(console.error);
        }).catch(console.error);
      }
      
      // 4.17 ApplyEdits Workaround

      // Create a promise that resolves after some delay
      function after(delay) {
        return new Promise(resolve => setTimeout(resolve, delay))
      }      
      
      // Wrapper around a promise to capture the resolve/reject function 
      function createResolver() {
        let resolve = null;
        let reject = null;
        
        const promise = new Promise((resolve2, reject2) => {
          resolve = resolve2;
          reject = reject2
        });
        
        return { promise, resolve, reject }
      }
      
      // Queue used to serialize calls to applyEdits
      const queue = []
      const resolvers = []
      let outstanding = false;
      
      // Safe wrapper around applyEdits that will serialize calls
      async function applyEditsToLayer(layer, edits) {
        // If we are currently applying some edit, enqueue the update to run after
        if (outstanding) {
          const resolver = createResolver()
          queue.push(edits);
          resolvers.push(resolver)
          return resolver.promise;
        }
        outstanding = true;
        
        let results = null; 
        
        try {
          results = await layer.applyEdits(edits);
          // We need to yield a frame in order for the updating status to change 
          await after(16);
          const layerView = await view.whenLayerView(layer);
          await watchUtils.whenFalseOnce(layerView, "updating");
          
        } catch (e) {
          console.debug(e)
        }
        
        outstanding = false; 
        
        // If we had enqued any promises while we were applying the edit, 
        // run the first one
        if (queue.length) {   
          const edit = queue.shift();
          const resolver = resolvers.shift();
          const results = await applyEditsToLayer(layer, edit);
          
          resolver.resolve(results);
        }
        
        return results; 
      }
    });
  </script>
</head>

<body>
  <div id="viewDiv"></div>
  <div id="actions" class="esri-widget">
    <button class="esri-button" id="add-many">Add Many Features</button>
    <button class="esri-button" id="remove">Remove all</button>
  </div>
</body>

</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console