draw.js 2.03 KB
import {maps} from '@/libs/map/mapUtils'   
import {loadModules} from 'esri-loader'

export default {
    data() {
        return {
            drawAction:null,
            graphicLayer:null,
            creatEvent:null,
        }
    },
    methods: {
        initDraw(type,viewId,creationMode,callBackFunction){
            var self = this;
             loadModules([
                 "esri/widgets/Sketch",
                 "esri/layers/GraphicsLayer"
             ]).then(([
                 Sketch,
                 GraphicsLayer
             ]) => {
                 var view  = maps[viewId];
                 if(!self.drawAction){
                    self.graphicLayer = new GraphicsLayer({
                         id:"_drawLayer"
                     })
                     self.drawAction = new Sketch({
                         view: view,
                         layer:self.graphicLayer,
                         creationMode: "single"
                     });
                 }else {
                   // this.drawAction.cancel();
                     if(creationMode){
                        self.drawAction.creationMode = creationMode;
                     }
                   //  graphicLayer = view.map.findLayerById("_drawLayer");
                 }
                 self.drawAction.create(type);
                 if(self.creatEvent){
                    self.creatEvent.remove();
                 }
                 self.creatEvent = self.drawAction.on("create", function(event) {
                    if (event.state === "complete") {
                       self.graphicLayer.remove(event.graphic);
                       if(callBackFunction && typeof callBackFunction == 'function'){
                        callBackFunction(event.graphic.geometry);
                       }
                      
                    }
                   })
             }).catch(err=>{
                 throw(err);
             });
        },
        destroyeDraw() {
         if(this.drawAction){
            this.drawAction.cancel();
         }
        }
     }
}