1
13693261870
2022-09-16 762f2fb45db004618ba099aa3c0bd89dba1eb843
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
///////////////////////////////////////////////////////////////////////////
// Copyright © 2014 - 2016 Esri. All Rights Reserved.
//
// Licensed under the Apache License Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///////////////////////////////////////////////////////////////////////////
 
define(['dojo/query', 'esri/geometry/geodesicUtils', 'esri/geometry/webMercatorUtils', 'esri/symbols/PictureMarkerSymbol', 'esri/symbols/SimpleLineSymbol', 'esri/geometry/Polyline', 'esri/geometry/Point', 'esri/graphic', 'dojo/on', 'esri/toolbars/draw', 'esri/layers/GraphicsLayer', 'dojo/_base/declare', 'dojo/_base/lang', 'dojo/aspect', 'dojo/Deferred', 'dijit/_WidgetsInTemplateMixin', 'jimu/BaseWidget', 'jimu/portalUtils', 'jimu/dijit/Message', 'esri/units', 'esri/dijit/Measurement', "esri/symbols/jsonUtils"], function(query, geodesicUtils, webMercatorUtils, PictureMarkerSymbol, SimpleLineSymbol, Polyline, Point, Graphic, on, Draw, GraphicsLayer, declare, lang, aspect, Deferred, _WidgetsInTemplateMixin, BaseWidget, PortalUtils, Message, esriUnits, Measurement, jsonUtils) {
    var clazz = declare([BaseWidget, _WidgetsInTemplateMixin], {
 
        name : 'Measurement',
        baseClass : 'jimu-widget-measurement',
        measurement : null,
        _pcDef : null,
        allPath : null,
        movegraphic:null,
        startup : function() {
            if (this.measurement || this._pcDef) {
                return;
            }
            this.inherited(arguments);
 
            var json = this.config.measurement;
            json.map = this.map;
            if (json.lineSymbol) {
                json.lineSymbol = jsonUtils.fromJson(json.lineSymbol);
            }
            if (json.pointSymbol) {
                json.pointSymbol = jsonUtils.fromJson(json.pointSymbol);
            }
 
            this._processConfig(json).then(lang.hitch(this, function(measurementJson) {
                //debugger;
                this.measurement = new Measurement(measurementJson, this.measurementDiv);
                this.own(aspect.after(this.measurement, 'setTool', lang.hitch(this, function() {
                    if (this.measurement.activeTool) {
                        this.disableWebMapPopup();
                    } else {
                        this.enableWebMapPopup();
                    }
                })));
 
                this.measurement.startup();
 
                this._hideToolsByConfig();
            }), lang.hitch(this, function(err) {
                new Message({
                    message : err.message || err
                });
            }));
        },
 
        _processConfig : function(configJson) {
            //debugger;
            this._pcDef = new Deferred();
            if (configJson.defaultLengthUnit && configJson.defaultAreaUnit) {
                configJson.defaultAreaUnit = esriUnits.HECTARES;
                configJson.defaultLengthUnit = esriUnits.METERS;
                this._pcDef.resolve(configJson);
            } else {
                if (this.appConfig.portalUrl) {
                    PortalUtils.getUnits(this.appConfig.portalUrl).then(lang.hitch(this, function(units) {
                        configJson.defaultAreaUnit = esriUnits.HECTARES;
                        configJson.defaultLengthUnit = esriUnits.METERS;
                        this._pcDef.resolve(configJson);
                    }), lang.hitch(this, function(err) {
                        console.error(err);
                        configJson.defaultAreaUnit = esriUnits.HECTARES;
                        configJson.defaultLengthUnit = esriUnits.METERS;
                        this._pcDef.resolve(configJson);
                    }));
                } else {
                    configJson.defaultAreaUnit = esriUnits.HECTARES;
                    configJson.defaultLengthUnit = esriUnits.METERS;
                    this._pcDef.resolve(configJson);
                }
            }
 
            return this._pcDef.promise;
        },
 
        _hideToolsByConfig : function() {
            if (false === this.config.showArea) {
                this.measurement.hideTool("area");
            }
            if (false === this.config.showDistance) {
                this.measurement.hideTool("distance");
            }
            if (false === this.config.showLocation) {
                this.measurement.hideTool("location");
            }
        },
 
        disableWebMapPopup : function() {
            this.map.setInfoWindowOnClick(false);
            if (this.measurement.activeTool == "distance") {
 
                if (this.map.drawToolbar != undefined) {
                    this.map.drawToolbar.deactivate();
                } else {
                    this.map.drawToolbar = new Draw(this.map);
                }
                var Drawtype;
                if (this.map.DrawonEvent) {
                    this.map.DrawonEvent.remove();
                }
                if (this.map.DrawwatchEvent) {
                    this.map.DrawwatchEvent.remove();
                }
                if(this.map.getLayer("displayLayersurvey")==null){
                    var eSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 128, 255, .55]), 1);
                    var displayLayer = new GraphicsLayer();
                    displayLayer.id = "displayLayersurvey";
                    this.map.addLayer(displayLayer);
                    this.map.drawToolbar.lineSymbol = eSymbol;
                }
                this.map.getLayer("displayLayersurvey").clear();
                Drawtype = Draw.POLYLINE;
                //////////////////
                
                var txtLayer = this.map.getLayer("displayLayerTxt");
                 if (txtLayer == null) {
                     txtLayer = new GraphicsLayer({
                     id : "displayLayerTxt"
                     });
                  this.map.addLayer(txtLayer);
                }
                txtLayer.clear();
                if(this.movegraphic==null){                    
                  var eTxtSymbol = new esri.symbol.TextSymbol().setColor(new esri.Color([224, 34, 34]));
                  this.movegraphic = new Graphic(new Point(10,10),eTxtSymbol);
                  this.movegraphic.hide();
                  txtLayer.add(this.movegraphic);
                }
                //
                this.map.DrawonEvent = this.map.drawToolbar.on("draw-end", lang.hitch(this, this.getDrawEnd));
                this.map.DrawwatchEvent = this.map.on("click", lang.hitch(this, this.getDrawline));
                this.map.on("mouse-move", lang.hitch(this, this.getMovelength));
                this.map.drawToolbar.activate(Drawtype);
 
            } else {
                if (this.map.getLayer("displayLayersurvey") != undefined) {
                    this.map.getLayer("displayLayersurvey").clear();
                    this.map.getLayer("displayLayerTxt").clear();
                    this._deleteNode();
                }
                if (this.map.drawToolbar != undefined) {
                    this.map.drawToolbar.deactivate();
                }
                if (this.map.DrawwatchEvent) {
                    this.map.DrawwatchEvent.remove();
                }
                if (this.map.DrawonEvent) {
                    this.map.DrawonEvent.remove();
                }
                if(this.movegraphic){
                    this.movegraphic = null;
                }
            }
            if (this.measurement.activeTool == "area") {
                if (this.map.drawToolbar != undefined) {
                    this.map.drawToolbar.deactivate();
                } else {
                    this.map.drawToolbar = new Draw(this.map);
                }
                var Drawtype;
                if (this.map.DrawonEvent) {
                    this.map.DrawonEvent.remove();
                }
                if (this.map.DrawwatchEvent) {
                    this.map.DrawwatchEvent.remove();
                }
                var txtLayer = this.map.getLayer("displayLayerTxt");
                 if (txtLayer == null) {
                     txtLayer = new GraphicsLayer({
                     id : "displayLayerTxt"
                     });
                  this.map.addLayer(txtLayer);
                }
                txtLayer.clear();
                if(this.map.getLayer("displayLayersurvey")==null){
                    var eSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0, 128, 255, .55]), 1);
                    var displayLayer = new GraphicsLayer();
                    displayLayer.id = "displayLayersurvey";
                    this.map.addLayer(displayLayer);
                    this.map.drawToolbar.lineSymbol = eSymbol;
                }
                this.map.getLayer("displayLayersurvey").clear();
                Drawtype = Draw.POLYGON;
                this.map.DrawonEvent = this.map.drawToolbar.on("draw-end", lang.hitch(this, this.getDrawEnd));
                //this.map.DrawwatchEvent = this.map.on("click", lang.hitch(this, this.getDrawline));
                this.map.drawToolbar.activate(Drawtype);
            };
        },
        getDrawEnd : function(result) {
            
            if (result.geometry.type == "polyline") {
                this.map.graphics.clear();
                var pathslist = result.geometry.paths;
                var sum = pathslist.length;
                var index = pathslist[sum - 1].length;
                //得到最后点击的点
                var uPoint = new Point(pathslist[sum-1][index-1][0], pathslist[sum-1][index-1][1]);
                //前一次点击的点
                var pPoint = this.map.previousdot;
 
                ///绘制点
                var eSymbol = new PictureMarkerSymbol("widgets/Measurement/images/esriGreenPin16x26.png", 16, 26);
                eSymbol.setOffset(1, 12);
                var graphic = new Graphic(uPoint, eSymbol);
                //this.map.getLayer("displayLayersurvey").add(graphic, );
 
                //绘制线
                var lSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new dojo.Color([207, 42, 36]), 3);
                var geometry = new Polyline(this.spatialReference);
                geometry.addPath([pPoint, uPoint]);
                var graphicline = new Graphic(geometry, lSymbol);
 
                //this.map.getLayer("displayLayersurvey").add(graphicline);
 
                this.map.previousdot = undefined;
 
                var Node = query('#lineindex')[0];
                //////////显示总长度////////////////
                this.movegraphic.hide();
                var paths = this.allPath;
                var geo = webMercatorUtils.webMercatorToGeographic(result.geometry);
                var text = query('#dijit_form_DropDownButton_0_label')[0].innerHTML;
                var fruit = geodesicUtils.geodesicLengths([geo], this.measurement.currentDistanceUnit);
                var isNow = true;
                this.map.getLayer("displayLayersurvey").clear();
                //绘制线
                geometry = new Polyline(this.map.spatialReference);
                geometry.addPath(paths);
                var lSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new dojo.Color([34, 117, 76]), 2);
                var lineGraphic = new Graphic(geometry, lSymbol);
                this.map.getLayer("displayLayerTxt").add(lineGraphic);
                for (var i = 0; i < paths.length; i++) {
                    var pPoint = paths[i];
                    var eTxtSymbol = new esri.symbol.TextSymbol().setColor(new esri.Color([224, 34, 34]));
                    eTxtSymbol.setOffset(30, 2);
                    if (i == 0) {
                        eTxtSymbol.setText("起点");
                    } else if (i > 0 && (i < paths.length - 1)) {
                        geometry = new Polyline(this.map.spatialReference);
                        geometry.addPath([paths[i - 1], pPoint]);
                        var geo = webMercatorUtils.webMercatorToGeographic(geometry);
                        var fruit = geodesicUtils.geodesicLengths([geo], this.measurement.currentDistanceUnit);
                        eTxtSymbol.setText(this.formatMoney(fruit[0].toFixed(1),1) + text);
                    } else if (i == paths.length - 1) {
                        geometry = new Polyline(this.map.spatialReference);
                        geometry.addPath(paths);
                        geo = webMercatorUtils.webMercatorToGeographic(geometry);
                        fruit = geodesicUtils.geodesicLengths([geo], this.measurement.currentDistanceUnit);
                        eTxtSymbol.setText("总长度:" + this.formatMoney(fruit[0].toFixed(1),1) + text);
                    }
                    ////////////////绘制点
                    var symbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 5, new esri.symbol.SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new esri.Color([34, 117, 76]), 1), new esri.Color([0, 255, 0, 0.25]));
                    var graphic = new Graphic(pPoint, symbol);
                    this.map.getLayer("displayLayerTxt").add(graphic);
                    //绘制线长度文本
                    var eTgraphic = new Graphic(pPoint, eTxtSymbol);
                    this.map.getLayer("displayLayerTxt").add(eTgraphic);
                };
                this.map.lineindex = 1;
            }
            else if(result.geometry.type == "polygon")//POLYGON
            {
                this.map.graphics.remove(this.map.graphics.graphics[this.map.graphics.graphics.length-1]);//移除黑色面
                var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, 
                    new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([224, 34, 34, 1]), 2), 
                    new dojo.Color([159, 146, 226, 0]));
                var graphic = new Graphic(result.geometry,symbol);
                this.map.getLayer("displayLayerTxt").add(graphic);
                var text = query('#dijit_form_DropDownButton_0_label')[0].innerHTML;
                var geo = webMercatorUtils.webMercatorToGeographic(result.geometry);
                var areas = geodesicUtils.geodesicAreas([geo],this.measurement.currentAreaUnit);
                var mapPoint = result.geometry.getExtent().getCenter();
                text = query("#dijit_layout_ContentPane_3")[0].innerHTML;
                var tSymbol = new esri.symbol.TextSymbol("面积:"+text).setColor(new esri.Color([224, 34, 34]));
                //tSymbol.setOffset(-10, 2);
                var areaGraphic = new Graphic(mapPoint,tSymbol);                
                this.map.getLayer("displayLayerTxt").add(areaGraphic);
            }
            
        },
        //线
        getDrawline : function(result, h, e) {
            //当前点
            var uPoint = result.mapPoint;
            if (this.map.previousdot === undefined) {
                //首次跳出----因原码有绘制首次代码
                this.map.getLayer("displayLayersurvey").clear();
                this.map.previousdot = uPoint;
                //this._deleteNode();
                this.map.lineindex = 1;
                //绘制起点文本
                var eTxtSymbol = new esri.symbol.TextSymbol("起点").setColor(new esri.Color([224, 34, 34]));
                eTxtSymbol.setOffset(30, 2);
                var eTgraphic = new Graphic(uPoint, eTxtSymbol);
                this.map.getLayer("displayLayersurvey").add(eTgraphic);                
                this.allPath = new Array(uPoint);        
                
                return;
            }
            this.allPath.push(uPoint);
            //前一次点击的点
            var pPoint = this.map.previousdot;
 
            //绘制线
            var lSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new dojo.Color([34, 117, 76]), 3);
            var geometry = new Polyline(this.map.spatialReference);
            geometry.addPath([pPoint, uPoint]);
 
            var graphicline = new Graphic(geometry, lSymbol);
            this.map.getLayer("displayLayersurvey").add(graphicline);
            ///绘制点
            var eSymbol = new PictureMarkerSymbol("widgets/Measurement/images/esriGreenPin16x26.png", 16, 26);
            eSymbol.setOffset(1, 12);
            var graphic = new Graphic(pPoint, eSymbol);
            this.map.getLayer("displayLayersurvey").add(graphic);
 
            var geo = webMercatorUtils.webMercatorToGeographic(geometry);
            var fruit = geodesicUtils.geodesicLengths([geo], this.measurement.currentDistanceUnit);
 
            if (query('#lineindex')[0] == undefined) {
                var paneNode = query('#dijit_layout_ContentPane_2')[0];
                dojo.place('<div id="lineindex"></div>', paneNode, "below");
            }
            var text = query('#dijit_form_DropDownButton_0_label')[0].innerHTML;
            if (fruit[0] != 0) {
                var divText = "第" + this.map.lineindex + "段距离:" + this.formatMoney(fruit[0].toFixed(1),1) + text;
                var Node = query('#lineindex')[0];
 
                dojo.place('<div>' + divText + '</div>', Node, "below");
                this.map.previousdot = uPoint;
                this.map.lineindex++;
                //绘制长度文本
                 var line = new Polyline(this.map.spatialReference);
                 line.addPath(this.allPath);
                 var geos = webMercatorUtils.webMercatorToGeographic(line);
                 var length = geodesicUtils.geodesicLengths([geos], this.measurement.currentDistanceUnit);
                 var TxtSymbol = new esri.symbol.TextSymbol(this.formatMoney(length[0].toFixed(1),1) + text).setColor(new esri.Color([224, 34, 34]));
                 TxtSymbol.setOffset(40, 2);
                 var Tgraphic = new Graphic(uPoint, TxtSymbol);
                 this.map.getLayer("displayLayersurvey").add(Tgraphic);
            }
 
        },
        getMovelength : function(result, h, e) {
             if(this.movegraphic&&this.map.previousdot){
                 var text = query('#dijit_form_DropDownButton_0_label')[0].innerHTML;
                 var line = new Polyline(this.map.spatialReference);
                line.addPath([this.map.previousdot,result.mapPoint]);
                var geos = webMercatorUtils.webMercatorToGeographic(line);
                var length = geodesicUtils.geodesicLengths([geos], this.measurement.currentDistanceUnit);
                var TxtSymbol = this.movegraphic.symbol;
                TxtSymbol.setText(this.formatMoney(length[0].toFixed(1),1) + text);
                 this.movegraphic.setGeometry(result.mapPoint);
                 this.movegraphic.show();
             }
        },
        _deleteNode : function() {
            query('#lineindex').forEach(function(Node) {
                dojo.destroy(Node);
            });
        },
        enableWebMapPopup : function() {
            if (this.map.getLayer("displayLayersurvey") != undefined) {
                this.map.getLayer("displayLayersurvey").clear();
                this.map.getLayer("displayLayerTxt").clear();
                this._deleteNode();
            }
            if (this.map.drawToolbar != undefined) {
                this.map.drawToolbar.deactivate();
            }
            if (this.map.DrawwatchEvent) {
                this.map.DrawwatchEvent.remove();
            }
            if(this.movegraphic){
               this.movegraphic = null;
            }
            this.map.setInfoWindowOnClick(true);
 
        },
        formatMoney : function(s,type){//按千位逗号分割数字
            if (/[^0-9]\./.test(s)) {
                return "0";
            }
            if (s===null || s== "") {return "0";};
            s= s.toString().replace(/^(\d*)$/,"$1.");
            s= (s +"00").replace(/(\d*\.\d\d)\d*/,"$1");
            s= s.replace(".",",");
            var re=/(\d)(\d{3},)/;
            while(re.test(s))
            {
                s = s.replace(re,"$1,$2");
            }
            s = s.replace(/,(\d\d)$/,".$1");
            if(type==0){
                var a = s.split(".");
                if(a[1]=="00")s=a[0];
            }
            return s;
        },
        onDeActive : function() {
            this.onClose();
        },
 
        onClose : function() {
            if (this.measurement && this.measurement.activeTool) {
                this.measurement.clearResult();
                this.measurement.setTool(this.measurement.activeTool, false);
            }
        },
 
        destroy : function() {
            if (this.measurement) {
                this.measurement.destroy();
            }
            this.inherited(arguments);
        }
    });
    return clazz;
});