fence.js 8.64 KB
import EventBus from '../event-bus'
import mapService from './mapService.js'
import MAPGEO from './mapgeo'

//电子围栏
const fence = {
    polygons: [],
    circle: null,
    isEditMode: true,
    bindEvent(){
        EventBus.$on('uiMap.fence.drawDistrict', (data)=>{
            this.drawDistrict(data)
        });

        EventBus.$on('uiMap.fence.createFenceCircle', (data)=>{
            if(('isEdit' in data)&&!data.isEdit){
                this.isEditMode = false
            }else{
                this.isEditMode = true
            }
            this.createFenceCircle(data)
        });

        EventBus.$on('uiMap.fence.updateFenceRadius', (data)=>{
            this.circle.setRadius(data*1);
            mapService.setFitView()
        });

        EventBus.$on('uiMap.fence.createFenceMarker', (data)=>{
            this.createFenceMarker(data)
        });

        EventBus.$on('uiMap.fence.drawPolygon', (data)=>{
            
            if(data){
                if(Array.isArray(data)){
                    this.polygonPath = data;
                    this.isEditMode = true;
                }else if(('isEdit' in data)&&!data.isEdit){
                    this.polygonPath = data.path;
                    this.isEditMode = false;
                }
            }
            this.drawPolygon()
        });

        EventBus.$on('uiMap.fence.destroy', (data)=>{
            this.polyEditor && this.polyEditor.close()
            this.mouseTool && this.mouseTool.close()
        });
    },
    destroy(){
        const MAP = mapService.map;
        this.polygons && MAP.remove(this.polygons);
        this.circle && MAP.remove(this.circle);
        this.circleEditor && this.circleEditor.close();//MAP.remove(this.circleEditor);
        this.polygons = [];
        this.polyEditor && this.polyEditor.close()
        this.mouseTool && this.mouseTool.close()
        this.polygon && MAP.remove(this.polygon)
        this.clearTimeoutIds();
    },
    drawDistrict(data){
        const MAP = mapService.map;
        this.destroy();

        var opts = {
            subdistrict: 1,   //返回下一级行政区
            extensions: 'all',
            showbiz:false  //最后一级返回街道信息
        };
        data == "中国"?delete opts.extensions :delete opts.showbiz;
        let district = new AMap.DistrictSearch(opts);
        district.setLevel('district');
        EventBus.$emit('uiMap.fence.loading', true);
        //行政区查询
        district.search(data.toString(), function(status, result) {
            const dst = result.districtList[0];
            var bounds = dst.boundaries;
            let lng = dst.center.lng;
            let lat = dst.center.lat;
            
            if (bounds) {               
                for (var i = 0, l = bounds.length; i < l; i++) {
                    //生成行政区划polygon
                    var polygon = new AMap.Polygon({
                        map: MAP,
                        strokeWeight: 1,
                        path: bounds[i],
                        fillOpacity: 0.7,
                        fillColor: '#CCF3FF',
                        strokeColor: '#CC66CC',
                        
                    });
                    fence.polygons.push(polygon);
                  
                } 
                
                let center = mapService.getLngLat(lng,lat);
                
                mapService.setFitView();
                EventBus.$emit('uiMap.fence.loading', false);
            }
             
        });
    },
    createFenceCircle(data){
        const MAP = mapService.map;
        //this.destroy();

        this.circle = new AMap.Circle({
            map: MAP,
            center: mapService.getLngLat(data.lng, data.lat),
            radius: data.radius*1,
            strokeColor: "#F33",
            strokeOpacity: 1,
            strokeWeight: 3,
            fillColor: "ee2200",
            fillOpacity: 0.35,
            zIndex: 0,
        }); 
        
        this.circleEditor = new AMap.CircleEditor(MAP, this.circle); 
        
        AMap.event.addListener(this.circleEditor, 'adjust', (e)=>{
            EventBus.$emit('uiMap.fence.onFenceRadiusChange', e.radius/1)
            /*this.form.radius = e.radius/1;
            this.$emit('onUpdateMapData',{
                lng: this.form.lng,
                lat: this.form.lat,
                address: this.form.transferAddr,
                radius: this.form.radius
            });*/
            //this.$refs.fenceMapForm.validateField('radius');
        });
        
        AMap.event.addListener(this.circleEditor, 'move', (e)=>{     

            EventBus.$emit('uiMap.fence.onFenceLngLatChange', {
                lng: e.lnglat.lng,
                lat: e.lnglat.lat
            });
        });
        if(this.isEditMode){
            this.circleEditor.open(); 
        }else{
            this.circleEditor.close(); 
        }
        
        
        mapService.setFitView()
        MAP.setZoom(14);
    },
    createFenceMarker(data){

        this.data = [{
            elng: data.lngGPS,
            elat: data.latGPS
        }];
        if(data.lngBase && data.latBase){
            this.data.push({
                elng: data.lngBase,
                elat: data.latBase
            });
        }
        const MAP = mapService.map;
        MAPGEO.transferAddressByGroup(this, ()=>{
            data.transferAddressGPS = this.data[0].transferAddress;
            if(this.data.length > 1){
                data.transferAddressBase = this.data[1].transferAddress;
            }
            let marker = mapService.createCarMarker([data.lngGPS, data.latGPS], MAP);
            let infoWindow = mapService.bindFenceInfoWindow(marker, MAP, data);
        });
    },
    drawPolygon(){
        const MAP = mapService.map;
        const self = this;
        //this.destroy();

        if(this.polygonPath.length){
            self.polygon = new AMap.Polygon({
                path: this.polygonPath,
                strokeColor: "#FF33FF", 
                strokeWeight: 6,
                strokeOpacity: 0.2,
                fillOpacity: 0.4,
                fillColor: '#1791fc',
                zIndex: 50,
            })

            MAP.add(self.polygon)
            MAP.setFitView([ self.polygon ])

            self.polyEditor = new AMap.PolyEditor(MAP, self.polygon)
            if(self.isEditMode){
                self.polyEditor.open()
            }else{
                self.polyEditor.close()
            }
            
            self.bindPolyEvent(self.polyEditor)
        }else if(self.isEditMode){
            self.mouseTool = new AMap.MouseTool(MAP)

            self.mouseTool.polygon({
                strokeColor: "#FF33FF", 
                strokeOpacity: 1,
                strokeWeight: 6,
                strokeOpacity: 0.2,
                fillColor: '#1791fc',
                fillOpacity: 0.4,
                // 线样式还支持 'dashed'
                strokeStyle: "solid",
                // strokeStyle是dashed时有效
                // strokeDasharray: [30,10],
            })

            self.mouseTool.on('draw', function(event) {
                // event.obj 为绘制出来的覆盖物对象
                self.polygon = event.obj;
                self.polyEditor = new AMap.PolyEditor(MAP, self.polygon)
                self.polyEditor.open()
                self.bindPolyEvent(self.polyEditor)
                self.mouseTool.close()
                self.emitPolyEvent()
            })

        }
        
    },
    bindPolyEvent(polyEditor){
        const self = this
        polyEditor.on('addnode', function(event) {
            console.log('触发事件:addnode')
            self.emitPolyEvent()
        })

        polyEditor.on('adjust', function(event) {
            console.log('触发事件:adjust')
            self.emitPolyEvent()
        })

        polyEditor.on('removenode', function(event) {
            console.log('触发事件:removenode')
            self.emitPolyEvent()
        })

        polyEditor.on('end', function(event) {
            console.log('触发事件: end')
            self.emitPolyEvent()
            // event.target 即为编辑后的多边形对象
        })
    },
    emitPolyEvent(){
        let pathArr = this.polygon.getPath()

        let resultArr = pathArr.map((item)=>{
            return [item.lng, item.lat]
        })

        this.polygonPath = resultArr
        EventBus.$emit('uiMap.fence.polygonChange',{
            polygonPath: this.polygonPath
        });
        console.log(resultArr.join('|'))
    },
    clearTimeoutIds(){
        this.timeoutIds && this.timeoutIds.forEach((item)=>{
            item && window.clearTimeout(item);
        });
        this.timeoutIds = []
    }

}

export default fence;