MediaLayer with control points

This sample demonstrates adding a historical map as a MediaLayer in a Map Component. The map represents the First Battle of Manassas during the US Civil War on July 21, 1861. It's georeferenced using control points to align it with the modern basemap.

A control point is composed of a sourcePoint, representing a point on the element measured in pixels from the top left corner, and a mapPoint, a known geographic location. This allows us to position the image in map space.

We also set the MediaLayerView interactive property to true to enable users to reposition the media layer on the map. When you click on the media layer view, the reshape tool will be active and allow you to move the individual control points. You can also activate the transform tool by clicking on the appropriate action at the bottom of the screen, allowing you to resize and rotate the whole image. Several keyboard shortcuts are available to help place the image and are described in a table in the top right corner of the map.

How it works

First, an array of control points is defined.

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
        // Define four points representing known map coordinates on the MediaLayer's ImageElement.
        const mapPoint0 = new Point({
          x: -8635420,
          y: 4693931,
          spatialReference: { wkid: 102100 }
        });

        const mapPoint1 = new Point({
          x: -8631401,
          y: 4699148,
          spatialReference: { wkid: 102100 }
        });

        const mapPoint2 = new Point({
          x: -8619568,
          y: 4700860,
          spatialReference: { wkid: 102100 }
        });

        const mapPoint3 = new Point({
          x: -8620170,
          y: 4692018,
          spatialReference: { wkid: 102100 }
        });

        // Create an array of four control points composed of a sourcePoint, a point
        // on the image element in pixels, and a mapPoint which is the location of the
        // sourcePoint in map coordinates.
        const controlPoint0 = {
          sourcePoint: { x: 2108, y: 3070 },
          mapPoint: mapPoint0
        };

        const controlPoint1 = {
          sourcePoint: { x: 3425, y: 1365 },
          mapPoint: mapPoint1
        };

        const controlPoint2 = {
          sourcePoint: { x: 7313, y: 794 },
          mapPoint: mapPoint2
        };

        const controlPoint3 = {
          sourcePoint: { x: 7120, y: 3685 },
          mapPoint: mapPoint3
        };

        const controlPoints = [controlPoint0, controlPoint1, controlPoint2, controlPoint3];

Then, the MediaLayer is initialized with the ImageElement and ControlPointsGeoreference.

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
        // Create a georeference for the image element using the control points,
        // image height, and image width.
        const georeference = new ControlPointsGeoreference({
          controlPoints,
          height: 5636,
          width: 8891
        });

        // Create a new image element with the image url and georeference.
        const imageElement = new ImageElement({
          image:
            "https://arcgis.github.io/arcgis-samples-javascript/sample-data/media-layer/battle-of-first-manassas.jpg",
          georeference
        });

        // Create a new MediaLayer with the image element.
        const mediaLayer = new MediaLayer({
          source: imageElement,
          title: "The Battle of First Manassas",
          copyright: "United States. National Park Service, Public domain, via Wikimedia Commons"
        });

After the map is ready, we add the media layer to the map and set the MediaLayerView interactive property to true to enable interactive placement of the media layer.

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
        // If the map is not ready, add an event listener to wait for the map to be ready
        // and call the handleArcgisMapReady function. Otherwise, call
        // the handleArcgisMapReady function immediately.
        if (!arcgisMap?.ready) {
          arcgisMap?.addEventListener("arcgisViewReadyChange", handleArcgisMapReady, {
            once: true
          });
        } else {
          handleArcgisMapReady();
        }

        // Define a function that executes after the map is ready.
        async function handleArcgisMapReady() {
          // Add the media layer to the map.
          arcgisMap?.addLayer(mediaLayer);

          // Wait for the media layer view to be available and set it to the
          // mediaLayerView variable.
          mediaLayerView = await arcgisMap?.whenLayerView(mediaLayer);

          // Set the interaction options tool to reshape.
          mediaLayerView.interactionOptions.tool = "reshape";

          // Set the media layer view's interactive property to true and when users
          // click on the layer view the reshape tool will activate.
          mediaLayerView.interactive = true;
        }

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