zengh
2022-05-09 52c39f5e2711e5535b689b66dfa5e100c7882b7f
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
// import myDomMove from './move'
var me = new Vue({
    el: '#mapVue',
    data: {
        map: null, //map
        getdata: null,
        marker: null,
        layuiLayer: null,
        nowIndex: 1,
 
        //基础路径
        // pathUrl: "http://localhost:89",
        pathUrl: this.$store.state.piAPI + "",
 
        layer: null,
 
        snumber: null,
 
        activeName: 'first',
 
        move: null, //存放移动实例
 
        seedata: '',
 
        //路线数据
        routeName: "",
        lxbz: "",
 
        //区域数据
        regionName: "",
        qybz: ""
    },
    created() {},
    mounted() {
        this.getDataList(1);
    },
    methods: {
        getDataList(tab) {
            var that = this;
            that.beginCome(tab);
        },
 
        //巡逻路线区域点击事件
        handleClick(tab) {
            this.tab = tab;
            //路线
            if (tab == 1) {
                //修改样式
                document.getElementById("line").style.display = "block";
                document.getElementById("area").style.display = "none";
                document.getElementById("routeBtn").style.backgroundColor = "cornflowerblue";
                document.getElementById("areaBtn").style.backgroundColor = "darkgrey";
                // document.getElementById("seedata").style.height = "20%";
                // document.getElementById("map").style.height = "80%";
                //展示巡逻路线
                this.showPolyLine(this.map);
 
            }
 
            //区域
            if (tab == 2) {
                //修改样式
                document.getElementById("line").style.display = "none";
                document.getElementById("area").style.display = "block";
                document.getElementById("routeBtn").style.backgroundColor = "darkgrey";
                document.getElementById("areaBtn").style.backgroundColor = "cornflowerblue";
                // document.getElementById("seedata").style.height = "20%";
                // document.getElementById("map").style.height = "80%";
                //展示巡逻区域
                this.showPolygon(this.map);
            }
        },
 
        beginCome(tab) {
            //url解码
            this.snumber = this.getQueryVariable('snumber');
            //创建并接受map
            this.map = this.beginMap(this.map, this.getdata, tab);
            this.move = new myDomMove('#seedata', '.route-area-tab', '#map');
            //应用地图高度
            this.map.invalidateSize(true);
            this.move.followFinger(this.map, 'down');
        },
 
        //获取参数
        getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                //解码url 和 JSON
                if (pair[0] == variable) { return JSON.parse(decodeURI(pair[1])); }
            }
            return (false);
        },
 
        //开始
        beginMap(map, data, tab) {
            var createMap = () => {
                //初始化地图
                map = L.map('map', {
                    center: [28.7012481186582, 115.852228372357],
                    zoom: 12,
                    minZoom: 2,
                    maxZoom: 17,
                    attributionControl: false, //去掉右下角
                    zoomControl: false, //去掉缩放
                });
                //添加切片图层
                L.tileLayer(
                    //顺丰地图
                    // "https://webmap-tile.sf-express.com/MapTileService/rt?x={x}&y={y}&z={z}", {
                    "https://webmap-tile.sf-express.com/MapTileService/rt?fetchtype=static&x={x}&y={y}&z={z}&project=sfmap&pic_size=256&pic_type=png8&data_name=361100&data_format=merged-dat&data_type=normal", {}
                ).addTo(map);
            }
            createMap();
 
            //画巡逻路线
            if (tab == 1) {
                this.showPolyLine(map);
            }
 
            //画巡逻区域
            if (tab == 2) {
                this.showPolygon(map);
            }
 
            // map.locate({
            //     setView: true,
            //     maxZoom: 16
            // });
            //抛出map
            return map;
        },
 
        //巡逻路线
        showPolyLine(map) {
            var that = this;
            //获取巡逻路线信息
            $.ajax({
                url: that.pathUrl + '/duty/duty/selectInfo',
                type: 'POST',
                dataType: 'JSON',
                data: {
                    snumber: that.snumber
                },
                success: function(result) {
                    //如果巡逻路线信息,展示
                    if (result.code == 200) {
                        if (JSON.stringify(result.data) != "{}") {
                            var entityArr = [];
                            var coordinate = result.data[0].routeInfo.match(/\(([^)]*)\)/).toString();
                            var a = coordinate.indexOf("(");
                            var b = coordinate.indexOf(")");
                            coordinate = coordinate.substring(a + 1, b) + "";
                            if (coordinate && coordinate != '') {
                                coordinate = coordinate.split(',');
                                for (var i = 0; i < coordinate.length; i++) {
                                    entityArr.push([Number(coordinate[i].split(' ')[1]), Number(coordinate[i].split(' ')[0])]);
                                }
                            }
                            if (that.nowIndex == 2) {
                                map.removeLayer(that.layer);
                            }
                            that.routeName = result.data[0].routeName;
                            that.lxbz = result.data[0].lxbz;
                            that.layer = L.polyline(entityArr, { color: 'red' }).addTo(map);
                            map.addLayer(that.layer);
                            that.nowIndex = 2;
                            // zoom the map to the polyline
                            map.fitBounds(that.layer.getBounds());
                        }
                    }
                }
            });
 
        },
 
 
        //巡逻区域
        showPolygon(map) {
            var that = this;
            $.ajax({
                url: that.pathUrl + '/duty/duty/selectInfo',
                type: 'POST',
                dataType: 'JSON',
                data: {
                    snumber: that.snumber
                },
                success: function(result) {
                    //如果巡逻区域信息,展示
                    if (result.code == 200) {
                        if (JSON.stringify(result.data) != "{}") {
                            var entityArr = [];
                            var coordinate = result.data[0].coordinater.match(/\(([^)]*)\)/).toString();
                            var a = coordinate.indexOf("(");
                            var b = coordinate.indexOf(")");
                            coordinate = coordinate.substring(a + 1, b) + "";
                            if (coordinate && coordinate != '') {
                                coordinate = coordinate.split(',');
                                for (var i = 0; i < coordinate.length; i++) {
                                    entityArr.push([Number(coordinate[i].split(' ')[1]), Number(coordinate[i].split(' ')[0])]);
                                }
                            }
                            if (that.nowIndex == 2) {
                                map.removeLayer(that.layer);
                            }
                            that.layer = L.polygon(entityArr, { color: 'red' }).addTo(map);
                            that.regionName = result.data[0].regionName;
                            that.qybz = result.data[0].qybz;
                            map.addLayer(that.layer);
                            that.nowIndex = 2;
                            // zoom the map to the polygon
                            map.fitBounds(that.layer.getBounds());
                        }
                    }
                }
            });
 
        }
    }
})