drawTool.js 3.04 KB
import { loadModules } from "esri-loader";
import mapManage from './towMapObjects';
export default class DrawTool {
    static getInstance() {
        loadModules(["esri/symbols/PictureMarkerSymbol", "esri/layers/GraphicsLayer", "esri/views/draw/Draw"]).then((a) => {
            mapManage.drawTool || (mapManage.drawTool = new this(a));
        });
    }
    constructor() {
        let arg = arguments[0];
        this.view = mapManage.mapView;
        this.map = mapManage.mapView.map;
        this.active = !1;
        this.paths = [];
        this.options1 || (this.options1 = {}, this.options1.pm = {
            type: 'simple-marker', color: [165, 42, 42, 0.7], width: "5px", height: "5px"
        });
        this.map.add(this.graphicsLayer = new arg[1]({
            id: "drawtool",
            graphics: []
        }));
        this.draw = new arg[2]({ view: this.view });
    }
    activate(c, b) {
        this.active = !0;
        this.drawType = c.toUpperCase();
        this.callback = b;
        this.downCount = 0;
        // this.map.reorder(this.graphicsLayer, this.map.allLayers.length - 1);
        switch (this.drawType) {
            case "POINT1":
            loadModules(["esri/Graphic", "esri/geometry/Point"]).then(([h, f]) => {
                this.downCount = 0;
                let action = this.draw.create("point", {
                    mode: "click"
                });
                action.on("draw-complete", event => {        
                    !this.paths.length && this.graphicsLayer.graphics.add(this.graphic = new h({
                        geometry: this.construc({
                            type: f,
                            vertices: event.vertices
                        }),
                        symbol: this.options1.pm
                    }));
                    this.paths.push(event.vertices[0]);
                    this.callback(event.vertices);
                });
            });
            break;
        }
    }
    createPolyline() {
        this.graphicsLayer.graphics.remove(this.graphic);
        loadModules(["esri/Graphic", "esri/geometry/Polygon"]).then(([h, f]) => {
            this.graphic = new h({
                geometry: this.construc({
                    type: f,
                    rings: this.paths
                }),
                symbol: {
                    type: "simple-line",
                    color: [238, 44, 44, 0.8],
                    width: 2,
                    style: 'solid'
                }
            });
            this.graphicsLayer.graphics.add(this.graphic);
        });
    }
    deactivate() {
        this.active = !1;
        this.drawType = "";
        this.downCount = 0;
    }
    construc(arg) { 
        let settings = {
            hasZ: false,
            hasM: false,
            spatialReference: this.view.spatialReference, 
            paths: arg.paths, 
            rings: arg.rings
        };
        arg.vertices && Object.assign(settings, {
            x: arg.vertices[0][0],
            y: arg.vertices[0][1]
        });
        return new arg.type(settings);
    }
}