Skip To Content ArcGIS for Developers Sign In Dashboard

Overview

You will learn: how to add layers to a map from a custom widget.

The map widget in ArcGIS Experience Builder automatically loads layers based on what web map it is configured to load. Depending on your workflow, you may want a custom widget to dynamically add a layer to the map based on user interaction with the widget.

In this tutorial, you will add a button to the widget to add a feature layer to the map.

Before you begin

Install ArcGIS Experience Builder

Be sure to download, install, and configure ArcGIS Experience Builder Developer Edition.

Steps

Get the starter widget

  1. Download the Starter Widget template here.

  2. In the ArcGIS Experience Builder folder, extract the zip into the following path: /client/your-extensions/widgets.

Change the widget name

  1. In the terminal where the npm start is running in the client folder, stop the script by pressing ctrl + c.

  2. In your file browser, go to the folder where ArcGIS Experience Builder was extracted.

  3. In the ArcGIS Experience Builder folder, expand the following path: /client/your-extensions/widgets.

  4. In the widgets folder, rename the starter-widget folder to add-layers.

  5. In the newly-renamed add-layers folder, open the manifest.json file in the code editor.

  6. In the code editor, modify the name property to add-layers.

     {
       // *** UPDATE  ***
       // "name": "starter-widget",
       "name": "add-layers",
       "type": "widget",
       "version": "1.0.0",
    
  7. After the version property in manifest.json, add a jimu-arcgis dependency. Declaring this allows the usage of ArcGIS API for JavaScript modules within the widget.

    {
       "name": "add-layers",
       "type": "widget",
       "version": "1.0.0",
       // *** ADD ***
       "dependency": "jimu-arcgis",
    

Implement the settings panel

A settings panel can be implemented to allow Experience authors to customize the widget. The settings panel appears in the right-hand sidebar when a widget is selected in ArcGIS Experience Builder. To create the panel, implement the BaseWidgetSetting class.

  1. In the widget root folder, create a config.json file that contains an empty object.

{}
  1. In the src folder, create another folder called setting.
  2. In the setting folder, create a setting.tsx file.
  3. Open the setting/setting.tsx file and include the following import statements.
/** @jsx jsx */
import { React, jsx } from "jimu-core";
import { BaseWidgetSetting, AllWidgetSettingProps } from "jimu-for-builder";
  1. Add code to implement BaseWidgetSetting.
export default class Setting extends BaseWidgetSetting<AllWidgetSettingProps<any>, any> {
  render() {
    return <div className="widget-setting-demo">This is your starter widget setting area!</div>;
  }
}
  1. In the terminal, stop (ctrl + c) (if applicable) and start the npm start script in the client folder.

Enable selecting a map view data source

In ArcGIS Experience Builder, there can be more than one Map Widget on the page at a time. Because of this, a custom widget must have a section of its Settings Panel that allows the author to choose which map widget to use.

  1. In the setting/setting.tsx file, include the JimuMapViewSelector module from the jimu library.

    import { JimuMapViewSelector } from "jimu-ui/setting-components";
    
  2. Before the render function, define the onMapWidgetSelected function.

     // *** ADD ***
     onMapWidgetSelected = (useMapWidgetIds: string[]) => {
       this.props.onSettingChange({
         id: this.props.id,
         useMapWidgetIds: useMapWidgetIds
       });
     };
     render() {
       return <div className="widget-setting-demo">This is your starter widget setting area!</div>;
     }
    
  3. In the render function, in the return() statement, add a tag representing the JimuMapViewSelector.

     render() {
       return <div className="widget-setting-demo">
         <JimuMapViewSelector
           useMapWidgetIds={this.props.useMapWidgetIds}
           onSelect={this.onMapWidgetSelected}
         />
       </div>;
     }
    

Access the map

In the previous step, the settings panel was enhanced to allow the Map widget to be selected. The map object can be accessed using the JimuMapViewComponent.

  1. In the Widget.tsx file, add the JimuMapViewComponent and the JimuMapView type from the jimu library.

    import { JimuMapViewComponent, JimuMapView } from "jimu-arcgis";
    
  2. Before the render function, set up the default state.

     export default class Widget extends BaseWidget<AllWidgetProps<any>, any> {
     // *** ADD ***//
     state = {
       jimuMapView: null
     };
     render() {
    
  3. Add a function to update the state every time the JimuMapView data source changes.

    state = {
     jimuMapView: null
    };
    // *** ADD ***//
    activeViewChangeHandler = (jmv: JimuMapView) => {
      if (jmv) {
        this.setState({
          jimuMapView: jmv
        });
      }
    };
    
  4. In the render function, add the JimuMapViewComponent to the JSX markup.

     render() {
       return (
         <div className="widget-starter jimu-widget">
           {/* *** ADD *** */}
           {this.props.hasOwnProperty("useMapWidgetIds") &&
             this.props.useMapWidgetIds &&
             this.props.useMapWidgetIds.length === 1 && (
               <JimuMapViewComponent
                 useMapWidgetIds={this.props.useMapWidgetIds}
                 onActiveViewChange={this.activeViewChangeHandler}
               />
             )
           }
         </div>
       );
     }
    

Add the button

Add a button to the UI of the widget that when clicked will add the layer to the map.

  1. In the code editor, add form and input tag elements in the existing div in the render function.

     render() {
       return (
         <div className="widget-starter jimu-widget">
           {this.props.hasOwnProperty("useMapWidgetIds") &&
             this.props.useMapWidgetIds &&
             this.props.useMapWidgetIds.length === 1 && (
               <JimuMapViewComponent
                 useMapWidgetIds={this.props.useMapWidgetIds}
                 onActiveViewChange={this.activeViewChangeHandler}
               />
             )
           }
           <!-- Add: -->
           <form onSubmit={this.formSubmit}>
             <div>
               <button>Add Layer</button>
             </div>
           </form>
         </div>
       );
     }
    
  2. Above the render function, create a new function called formSubmit. This function will get called when the user clicks the Add Layer button.

     formSubmit = evt => {
       evt.preventDefault();
       // More here soon
     }
    

Add a layer

When the button is clicked, add the layer to the map.

  1. At the top of Widget.tsx, import the FeatureLayer class.

    import FeatureLayer = require('esri/layers/FeatureLayer');
    
  2. In the formSubmit function, update the code to create the Trailheads (points) feature layer and add it to the map.

     formSubmit = evt => {
       evt.preventDefault();
    
       // *** ADD ***
    
       // create a new FeatureLayer
       const layer = new FeatureLayer({
         url: 'https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads_Styled/FeatureServer/0'
       });
    
       // Add the layer to the map (accessed through the Experience Builder JimuMapView data source)
       this.state.jimuMapView.view.map.add(layer);
     }
    

Test the widget

Once the code changes have been made, you can test your widget by running ArcGIS Experience Builder and viewing your Experience.

  1. In a web browser, go to Experience Builder. e.g. https://localhost:3001

  2. In ArcGIS Experience Builder, click Create New to create a new experience page.

  3. Click the Create button on the Blank scrolling page template.

  1. Click the Insert widget button, and drag the Map widget onto the experience.
  2. Click the Map widget in the preview area, then click Select map in the widget settings panel.
  3. In the Select data panel, click the Add new data button.
  4. In the Add data modal, select the Public tab and search for web map eb1be6543e304b4d81ed55439c412c2c. Click the search result to select it and then click Done. (Note this web map has no operational layers intentionally.)
  5. Click the newly-added LA Parks and Trails Map in the Select data panel to select the web map.
  1. Click the Insert widget button, and drag the new add-layers widget onto the experience.

  2. In the widget settings panel, choose Map 1 from the map selection dropdown.

  3. In the ArcGIS Experience Builder toolbar, click Save then Preview and the Experience will open in a new browser tab with your custom widget and a map.

Congratulations, you're done!

In the ArcGIS Experience Builder preview, click the button to add the layer to the map. Compare your widget with our completed widget.

Challenge

Add a layer list

Add a list of layers to the Experience and allow the user to toggle the visibility of each layer.

  1. Click the Insert widget button, and drag the Map Layers widget onto the experience.
  2. Click the Map Layers widget in the preview area, then click Interact with a Map widget in the widget settings panel.
  3. Choose Map 1 from the map selection dropdown.
  4. Enable the Toggle layer visibility toggle to allow the user to set the visibility of each layer.
  5. In the ArcGIS Experience Builder toolbar, click Save then Preview and the Experience will open in a new browser tab.

Add a layer input text box

Allow the user to copy/paste a layer URL into a text box and clicking the button will add that layer to the map. (Challenge solution)