智慧农业绘制地图
guoshilong
2022-09-27 1767c2c878ec1892ae560f81b32b5afdd2ceebfd
src/components/leafletDraw.vue
@@ -2,7 +2,7 @@
    <div ref="imageWrapper">
        <div class="btn-grounp">
            <button v-if="addShow" class="draw" @click="draw()">开始绘制</button>
            <button v-if="!addShow" class="repeal" @click="repeal()">撤销</button>
            <button v-if="!addShow&&repealShow" class="repeal" @click="repeal()">撤销</button>
            <button v-if="regionFull" class="save" @click="save()">保存</button>
        </div>
        <!-- <button class="disDraw" @click="disDraw()">关闭绘制</button> -->
@@ -24,6 +24,7 @@
            lng: "",
            lat: "",
            addShow: false,
            repealShow:false,
            currentPolygons: [],
@@ -33,7 +34,8 @@
            imgUrl: "",
            file: "",
            regionFull: false
            regionFull: false,
            tempBounds:""
        }
    },
    created () {
@@ -108,11 +110,11 @@
                doubleClickZoom: false, // 禁用双击放大
                attributionControl: false, // 移除右下角leaflet标识
            })
            this.$L.tileLayer('http://t{s}.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=e9533f5acb2ac470b07f406a4d24b4f0', {
            this.$L.tileLayer('https://t{s}.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=e9533f5acb2ac470b07f406a4d24b4f0', {
                subdomains: [0, 1, 2, 3, 4, 5, 6, 7],
            }).addTo(this.map)
            this.$L.tileLayer('http://t{s}.tianditu.gov.cn/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=e9533f5acb2ac470b07f406a4d24b4f0', {
            this.$L.tileLayer('https://t{s}.tianditu.gov.cn/DataServer?T=cva_w&x={x}&y={y}&l={z}&tk=e9533f5acb2ac470b07f406a4d24b4f0', {
                subdomains: [0, 1, 2, 3, 4, 5, 6, 7],
            }).addTo(this.map)
@@ -145,7 +147,7 @@
            this.complete.remove()
            this.addShow = true
            this.repealShow = false
            this.regionFull = false
            // Object.values(this.map._layers).forEach(item => {
@@ -194,46 +196,66 @@
            var str = window.location.search.substring(1)
            var parmasObj = this.getUrlParams(str)
            if(parmasObj.status){//后台截图
                domtoimage.toPng(document.getElementById('map'), { width: 970, height: 400 })
                .then( (dataUrl) => {
                    var base64Image = dataUrl // 后台返回的base64数据
                    var imgData = base64Image.replace(/[\r\n]/g, '') // 将回车换行换为空字符''
                    var obj = {
                        url:imgData,
                        polygons:arr,
                        area:area
            if(parmasObj.status){//后台镜头移动
                this.repealShow = false
                this.regionFull = false
                if(this.rectangleLayer.getBounds() != this.tempBounds){
                    if(this.map.getZoom()>16){
                        this.rectangleLayer.getBounds()._northEast.lng = this.rectangleLayer.getBounds()._northEast.lng+0.007
                        this.tempBounds = this.rectangleLayer.getBounds()
                        this.map.flyToBounds(this.rectangleLayer.getBounds(),{maxZoom:18});
                    }else{
                        this.rectangleLayer.getBounds()._northEast.lng = this.rectangleLayer.getBounds()._northEast.lng+0.06
                        this.tempBounds = this.rectangleLayer.getBounds()
                        this.map.flyToBounds(this.rectangleLayer.getBounds(),{maxZoom:18});
                    }
                    window.parent.postMessage(obj,"*")
                })
                .catch(function (error) {
                    console.error('oops, something went wrong!', error)
                })
                }
            }else{//小程序镜头移动
                this.map.flyToBounds(this.rectangleLayer.getBounds(),{maxZoom:18});
            }
            setTimeout(() => {
                if(parmasObj.status){//后台截图
                    domtoimage.toPng(document.getElementById('map'), { width: 450, height: 450 })
                    .then( (dataUrl) => {
            }else{//小程序截图
                domtoimage.toPng(document.getElementById('map'), { width: 375, height: 750 })
                .then( (dataUrl) => {
                        var base64Image = dataUrl // 后台返回的base64数据
                        var imgData = base64Image.replace(/[\r\n]/g, '') // 将回车换行换为空字符''
                    var base64Image = dataUrl // 后台返回的base64数据
                    var imgData = base64Image.replace(/[\r\n]/g, '') // 将回车换行换为空字符''
                    wx.miniProgram.postMessage({
                        data: { polygons: arr, area, bgUrl: imgData },
                        var obj = {
                            url:imgData,
                            polygons:arr,
                            area:area
                        }
                        window.parent.postMessage(obj,"*")
                    })
                    .catch(function (error) {
                        console.error('oops, something went wrong!', error)
                    })
                    setTimeout(() => {
                        this.repealShow = true
                        this.regionFull = true
                    }, 1000);
                    wx.miniProgram.navigateBack()
                })
                .catch(function (error) {
                    console.error('oops, something went wrong!', error)
                })
            }
                }else{//小程序截图
                    domtoimage.toPng(document.getElementById('map'), { width: 375, height: 600 })
                    .then( (dataUrl) => {
                        var base64Image = dataUrl // 后台返回的base64数据
                        var imgData = base64Image.replace(/[\r\n]/g, '') // 将回车换行换为空字符''
                        wx.miniProgram.postMessage({
                            data: { polygons: arr, area, bgUrl: imgData },
                        })
                        wx.miniProgram.navigateBack()
                    })
                    .catch(function (error) {
                        console.error('oops, something went wrong!', error)
                    })
                }
            }, 1000);
        },
        getUrlParams(urlParams) {
            var obj = {}
            var arrList = urlParams.split('&')
@@ -259,7 +281,7 @@
            this.map.on('pm:drawstart', ({ workingLayer }) => {
                this.addShow = false
                this.repealShow = false
                that.rectangleLayer = workingLayer
                console.log(workingLayer, 5465456)
@@ -276,7 +298,7 @@
            })
            this.map.on("pm:create", e => {
                that.repealShow = true
                that.regionFull = true
                // 绘制坐标
@@ -314,8 +336,6 @@
                let area = this.$turf.area(this.$turf.polygon([arr]))
                window.parent.postMessage(area,"*")
                this.map.flyToBounds(this.rectangleLayer.getBounds());
            })
        },
@@ -378,9 +398,11 @@
    .repeal {
        // width: 100px;
        // height: 44px;
        border-radius: 22px;
        border: none;
        color: #000;
        background: #fff;
        width: 150px;
        height: 66px;
        font-size: 30px;
@@ -392,7 +414,8 @@
        // height: 44px;
        border-radius: 22px;
        border: none;
        color: #000;
        background: #fff;
        width: 150px;
        height: 66px;
        font-size: 30px;