DkMap.vue
4.54 KB
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
<template>
<div>
<button @click="removeGraphicLayer">移出graphic图层</button>
<div id="map">
</div>
</div>
</template>
<script>
import * as i2d from "i2d";
import 'leaflet/dist/leaflet.css'
import 'i2d/dist/i2d.css'
import L from "leaflet";
import {bbox} from "@turf/bbox";
export default {
name: 'DkMap',
data() {
return {
mapOptions: {
copyright: false,
basemaps: [
{
type: 'group',
name: '天地图电子',
layers: [
{
type: 'tdt',
layer: 'vec_d',
key: i2d.Token.tiandituArr,
crs: i2d.CRS.EPSG4490,
},
{
type: 'tdt',
layer: 'vec_z',
key: i2d.Token.tiandituArr,
crs: i2d.CRS.EPSG4490,
}
],
icon: './dz.png',
show: true
}
],
zoom: 12,
minZoom: 1,
maxZoom: 16,
// 纬度在前
center: [34.247161, 108.944213],
crs: i2d.CRS.EPSG4490
},
colors: ["#FFEDA0", "#FED976", "#FEB24C", "#FD8D3C", "#FC4E2A", "#E31A1C", "#BD0026", "#800026"],
map: null,
graphicLayer: null
}
},
methods: {
getArrayRandomOne(arr) {
const n = Math.floor(Math.random() * arr.length)
return arr[n]
},
removeGraphicLayer() {
if (this.graphicLayer) {
// 移出图层
this.map.removeLayer(this.graphicLayer)
// 销魂图层
this.graphicLayer.destroy()
}
},
addMarker() {
let graphic = new i2d.Graphic.Marker({
latlng: [32.3102954498753, 119.19403010080875],
style: {
rotationAngle: 49,
image: "./sdt.png",
width: 32,
height: 44,
horizontalOrigin: i2d.HorizontalOrigin.CENTER,
verticalOrigin: i2d.VerticalOrigin.BOTTOM
}
})
this.graphicLayer.addGraphic(graphic)
graphic = new i2d.Graphic.Marker({
latlng: [32.318242830742236, 119.21534353237826],
style: {
rotationAngle: 190,
image: "./sdt.png",
width: 32,
height: 44,
horizontalOrigin: i2d.HorizontalOrigin.CENTER,
verticalOrigin: i2d.VerticalOrigin.BOTTOM
}
})
this.graphicLayer.addGraphic(graphic)
graphic = new i2d.Graphic.Marker({
latlng: [32.310656694051374, 119.22235167838745],
style: {
rotationAngle: 280,
image: "./sdt.png",
width: 32,
height: 44,
horizontalOrigin: i2d.HorizontalOrigin.CENTER,
verticalOrigin: i2d.VerticalOrigin.BOTTOM
}
})
this.graphicLayer.addGraphic(graphic)
},
},
mounted() {
// 从数据库读取的地块信息
const dkCoord = {"type": "Feature", "geometry": {"bbox": [119.19403010080875, 32.3102954498753, 119.22235167838748, 32.31824283074223], "type": "Polygon", "coordinates": [[[119.19403010080875, 32.3102954498753], [119.21534353237826, 32.318242830742236], [119.22235167838745, 32.310656694051374], [119.20791069412338, 32.31047249760787], [119.19403010080875, 32.3102954498753]]]}}
this.map = new i2d.Map('map', this.mapOptions)
const _self = this;
const geoJsonLayer = new i2d.Layer.GeoJsonLayer({
name: "dk",
data: dkCoord,
// 渲染颜色
symbol: {
type: "polygon",
styleOptions: {
fill: true,
// image: "img/fill/redLine.png",
// imageOpacity: 1,
fillOpacity: 0.5,
outline: true,
outlineWidth: 2,
outlineOpacity: 1,
outlineColor: "white"
},
callback: function () {
return {
fillColor: _self.getArrayRandomOne(_self.colors)
}
}
}
})
this.map.addLayer(geoJsonLayer)
//地块自带外包矩形
if (dkCoord.bbox) {
//定位到矩形
this.map.fitBounds(L.latLngBounds([
[dkCoord.bbox[1], dkCoord.bbox[0]],
[dkCoord.bbox[3], dkCoord.bbox[2]]
]));
} else {
//地块不含外包矩形 自己计算
const result = bbox(dkCoord);
this.map.fitBounds(L.latLngBounds([
[result[1], result[0]],
[result[3], result[2]]
]));
}
// 创建矢量数据图层
this.graphicLayer = new i2d.Layer.GraphicLayer()
this.map.addLayer(this.graphicLayer)
this.addMarker()
}
}
</script>
<style scoped>
#map {
position: absolute;
margin: 0;
height: 60%;
width: 60%;
}
</style>