智慧农业绘制地图
guoshilong
2022-09-27 1767c2c878ec1892ae560f81b32b5afdd2ceebfd
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
<template>
    <div ref="imageWrapper">
        <div id="map"></div>
    </div>
</template>
 
<script>
 
export default {
    props:["longitude","latitude"],
    data () {
        return {
            map: "",
            polygons: [],
            lng: "",
            lat: "",
            addShow: false,
 
            currentPolygons: [],
 
            complete: null,
 
            rectangleLayer: null,
 
            // 链接中的数据做处理的图层
            currentPolygonLayer: null,
 
            plotPolygonLayers: [],
 
            imgUrl: "",
            file: "",
 
            regionFull: false,
 
            flagList: false,
            clickPosition: {}
        }
    },
    created () {
        let type = this.getQueryString('type')
 
        if (type == 1) {
            // 用户编辑
            this.addShow = true
            this.regionFull = true
        } else {
            // 单纯展示
            this.addShow = false
        }
    },
    mounted () {
        let that = this
        // this.lng = 115.89886924863
        // this.lat = 28.6780931500551
        this.lng = this.longitude
        this.lat = this.latitude
        this.initDate()
 
        this.map.pm.addControls({
            position: "topleft",
            drawPolygon: false, // 添加绘制多边形
            drawMarker: false, //添加按钮以绘制标记
            drawCircleMarker: false, //添加按钮以绘制圆形标记
            drawPolyline: false, //添加按钮绘制线条
            drawRectangle: false,    //添加按钮绘制矩形
            drawCircle: false, //  添加按钮绘制圆圈
            editMode: false, //  添加按钮编辑多边形
            dragMode: false, //   添加按钮拖动多边形
            cutPolygon: false, // 添加一个按钮以删除图层里面的部分内容
            removalMode: false  // 清除图层
        })
        // 设置绘制后的线条颜色等
        this.map.pm.setPathOptions({
            color: "orange",
            fillColor: "green",
            fillOpacity: 0.4
        })
 
        this.map.pm.setLang('zh')
 
        this.map.on('click', (e) => {
 
            if (e.latlng.lng != that.clickPosition.lng || e.latlng.lat != that.clickPosition.lat) {
                that.flagList = false
 
                that.$parent.isShowLandDetails = false
                that.$parent.isShowFooter = true
 
                that.currentPolygonLayer.setStyle({
                    color: 'rgba(238, 166, 108, 1)', // res.color
                    weight: 2,
                    fillColor: 'rgb(238, 166, 108)', // res.color
                    fillOpacity: 0.5,
                })
            }
        })
 
    },
    methods: {
        /**
         * 添加面
         * @param {*} position 经纬度数组
         * @param {*} param 详细信息--返回的数据
         */
        addPolygon (position, param) {
            let obj = []
 
            position.forEach(item => {
                obj.push({ lng: Number(item[0]), lat: Number(item[1]) })
            })
 
            const that = this
 
            let latlngs = []
            let options = {
                color: 'rgba(238, 166, 108, 1)', // res.color
                weight: 2,
                fillColor: 'rgb(238, 166, 108)', // res.color
                fillOpacity: 0.5,
                data: {
                    rangePoints: obj,
                }
            }
 
            obj.forEach(item => {
                latlngs.push([item.lat, item.lng])
            })
 
            let polygonLayer = that.$L.polygon(latlngs, options).addTo(that.map)
 
            polygonLayer.params = param
 
            polygonLayer.on('click', e => {
                that.clickPosition = e.latlng
 
                that.flagList = true
 
                that.$parent.goTODetails({
                    pageSize: 20,
                    currentPage: 1,
                    total: 0,
                }, e.target.params, 'children')
 
                if (that.currentPolygonLayer != null) {
                    that.currentPolygonLayer.setStyle({
                        color: 'rgba(238, 166, 108, 1)', // res.color
                        weight: 2,
                        fillColor: 'rgb(238, 166, 108)', // res.color
                        fillOpacity: 0.5,
                    })
                }
 
                polygonLayer.setStyle({
                    color: 'rgb(255, 255, 255)',
                    weight: 4,
                    fillColor: 'rgb(238, 166, 108)',
                    fillOpacity: 0.5,
                    opacity: 1,
                })
 
                that.map.setView(that.$L.latLng(e.target.params.positionCenter[1], e.target.params.positionCenter[0]), 12)
 
                that.currentPolygonLayer = polygonLayer
            })
 
            that.plotPolygonLayers.push(polygonLayer)
        },
 
        /**
         * 显示详情
         * @param {*} plotDetail 地块的详情
         */
        showPlotDetails (plotDetail) {
            this.plotPolygonLayers.forEach(item => {
                if (item.params.id == plotDetail.id) {
                    item.setStyle({
                        color: 'rgb(255, 255, 255)',
                        weight: 4,
                        fillColor: 'rgb(238, 166, 108)',
                        fillOpacity: 0.5,
                        opacity: 1,
                    })
 
                    this.map.setView(this.$L.latLng(item.params.positionCenter[1], item.params.positionCenter[0]), 12)
 
                    this.currentPolygonLayer = item
                }
            })
        },
 
        /**
         * 关闭详情
         */
        closePlotDetails () {
            if (this.currentPolygonLayer != null) {
                this.currentPolygonLayer.setStyle({
                    color: 'rgba(238, 166, 108, 1)', // res.color
                    weight: 2,
                    fillColor: 'rgb(238, 166, 108)', // res.color
                    fillOpacity: 0.5,
                })
 
                this.map.setView(this.$L.latLng(this.lat, this.lng), 10)
            }
        },
 
 
        /**
         * 地图初始化
         */
        initDate () {
            this.map = this.$L.map("map", {
                center: [this.lat, this.lng], // 地图中心
                zoom: 15, //缩放比列
                zoomControl: false, //禁用 + - 按钮
                doubleClickZoom: false, // 禁用双击放大
                attributionControl: false // 移除右下角leaflet标识
            })
            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('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)
        },
 
        /**
         * 获取url信息
         * @param {*} name url地址中的key
         */
        getQueryString (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i")
            var r = window.location.search.substr(1).match(reg)
            if (r != null) {
                return unescape(r[2])
            }
            return null
        }
    }
};
</script>
 
<style lang="scss" scoped>
#map {
    position: relative;
    width: 100vw;
    height: 100vh;
    z-index: 1;
}
</style>