PopupTemplate with promise

This sample shows how to populate the content of a PopupTemplate using a function that returns a promise. This function returns a formatted string when a query executes successfully.

The feature layer's PopupTemplate.content is set via the queryChargingStations function.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
          // Find the counties layer in the map and set the popup template.
          const countiesLayer = map.layers.find((layer) => {
            return layer.title === "USA Counties Generalized Boundaries";
          });
          countiesLayer.outFields = ["*"];
          countiesLayer.popupTemplate = {
            title: "{NAME}",
            content: queryChargingStations
          };

When a county is clicked, the queryChargingStations function executes and its associated data is passed into the function. The function executes a query to return statistics on the county layer. After successfully executing, the function returns string-formatted content for the PopupTemplate per each county feature.

Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
          function queryChargingStations(target) {
            // Count and types of electric charging stations that intersect the county.
            const countLevel1 = new StatisticDefinition({
              statisticType: "count",
              onStatisticField: "EV_Level1_EVSE_Num",
              outStatisticFieldName: "numLevel_1"
            });
            const countLevel2 = new StatisticDefinition({
              statisticType: "count",
              onStatisticField: "EV_Level2_EVSE_Num",
              outStatisticFieldName: "numLevel_2"
            });
            const countLevel3 = new StatisticDefinition({
              statisticType: "count",
              onStatisticField: "EV_DC_Fast_Count",
              outStatisticFieldName: "numLevel_3"
            });
            // Create a query object with the target graphic's geometry and the outStatistics.
            const queryObject = new Query({
              geometry: target.graphic.geometry,
              outFields: ["*"],
              spatialRelationship: "intersects",
              outStatistics: [countLevel1, countLevel2, countLevel3]
            });
            // Execute the query with the query object.
            return query.executeQueryJSON(queryUrl, queryObject).then((result) => {
              const stats = result.features[0].attributes;
              // Format the query result for the counties popupTemplate's content.
              return (
                "<b>" +
                (stats.numLevel_1 + stats.numLevel_2 + stats.numLevel_3) +
                "</b> electric charging stations intersect the boundary of {NAME}. <br><br>" +
                "The number and type of stations: <br>" +
                "<ul>" +
                "<li> Level 1 Charging Stations (120V, AC): " +
                stats.numLevel_1 +
                "</li>" +
                "<li> Level 2 Charging Stations (240V, AC): " +
                stats.numLevel_2 +
                "</li>" +
                "<li> Level 3 Charging Stations (480V, DC): " +
                stats.numLevel_3 +
                "</li>" +
                "</ul>"
              );
            });
          }

The map displays the population in 2020 of each county in the United States. The counties have been generalized, so some distortion may be noticeable at large scales. Counts of 3 different types of electric charging stations are displayed in the popup.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.