-
Notifications
You must be signed in to change notification settings - Fork 0
/
量算工具.html
265 lines (256 loc) · 8.95 KB
/
量算工具.html
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>量算工具</title>
<link rel="stylesheet" href="css/mapbox-gl.css" type="text/css"/>
<script type="text/javascript" src="js/mapbox-gl.js"></script>
<link rel="stylesheet" href="css/mapbox-gl-draw.css">
<script type="text/javascript" src="js/mapbox-gl-draw.js"></script>
<script type="text/javascript" src="js/turf.min.js"></script>
<style type="text/css">
html, body {
margin: 0px;
height: 100%;
width: 100%;
}
#map {
width: 100%;
height: 100%;
}
#controls{
position: absolute;
margin-top: -720px;
z-index: 10;
/* left: 20px;
bottom: 10px; */
width: 10%;
background: #9febef;
}
</style>
</head>
<body>
<div id="map"></div>
<div id='area_calculate' class='area-container'>
</div>
<div id="controls">
<div id="resultD"></div>
<input type="button" value="测距" onclick="distanceMea()">
<input type="button" value="测面" onclick="areaMea()">
<input type="button" value="清除" onclick="deleteAll()">
<input type="button" value="关闭测绘控件" onclick="deactivateCtrl()">
<input type="button" value="激活测绘控件" onclick="activateCtrl()">
</div>
</body>
<script>
var monument = [118.777283,32.057966];
var map = new mapboxgl.Map({
container: 'map', // container id
style: { // stylesheet location
"version": 8,
"sources": {},
"layers": []
},
center: [118.777283,32.057966], // starting position [lng, lat]
zoom: 8 ,// starting zoom
center: monument,
});
map.on("load",function(){
var arcgis_wmts ='http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer/WMTS?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=ChinaOnlineCommunity&STYLE=default&TILEMATRIXSET=default028mm&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&FORMAT=image/png'
var arcgis_layer = {
"id": "arcgis_wmts",
"type": "raster",
"source": {
"type": "raster",
"tiles": [arcgis_wmts],
"zoomOffset": 0,//wmts Capabilities信息中TileMatrix第一个对应的实际多少级
"tileSize": 256
}
};
map.addLayer(arcgis_layer);
})
//添加绘画工具
var draw = new MapboxDraw({
displayControlsDefault: false,
styles:[
{
'id': 'highlight-active-points',
'type': 'circle',
'filter': ['all',
['==', '$type', 'Point'],
['==', 'meta', 'feature'],
['==', 'active', 'true']],
'paint': {
'circle-radius': 7,
'circle-color': '#000000'
}
},
{
'id': 'points-are-blue',
'type': 'circle',
'filter': ['all',
['==', '$type', 'Point'],
['==', 'meta', 'feature'],
['==', 'active', 'false']],
'paint': {
'circle-radius': 5,
'circle-color': '#000088'
}
},
// ACTIVE (being drawn)
// line stroke
{
"id": "gl-draw-line",
"type": "line",
"filter": ["all", ["==", "$type", "LineString"], ["!=", "mode", "static"]],
"layout": {
"line-cap": "round",
"line-join": "round"
},
"paint": {
"line-color": "#D20C0C",
// "line-dasharray": [0.2, 2],
"line-width": 2
}
},
// polygon fill
{
"id": "gl-draw-polygon-fill",
"type": "fill",
"filter": ["all", ["==", "$type", "Polygon"], ["!=", "mode", "static"]],
"paint": {
"fill-color": "#D20C0C",
"fill-outline-color": "#D20C0C",
"fill-opacity": 0.1
}
},
// polygon outline stroke
// This doesn't style the first edge of the polygon, which uses the line stroke styling instead
{
"id": "gl-draw-polygon-stroke-active",
"type": "line",
"filter": ["all", ["==", "$type", "Polygon"], ["!=", "mode", "static"]],
"layout": {
"line-cap": "round",
"line-join": "round"
},
"paint": {
"line-color": "#D20C0C",
"line-dasharray": [0.2, 2],
"line-width": 2
}
},
// vertex point halos
{
"id": "gl-draw-polygon-and-line-vertex-halo-active",
"type": "circle",
"filter": ["all", ["==", "meta", "vertex"], ["==", "$type", "Point"], ["!=", "mode", "static"]],
"paint": {
"circle-radius": 7,
"circle-color": "#FFF"
}
},
// vertex points
{
"id": "gl-draw-polygon-and-line-vertex-active",
"type": "circle",
"filter": ["all", ["==", "meta", "vertex"], ["==", "$type", "Point"], ["!=", "mode", "static"]],
"paint": {
"circle-radius": 5,
"circle-color": "#D20C0C"
}
},
// INACTIVE (static, already drawn)
// line stroke
{
"id": "gl-draw-line-static",
"type": "line",
"filter": ["all", ["==", "$type", "LineString"], ["==", "mode", "static"]],
"layout": {
"line-cap": "round",
"line-join": "round"
},
"paint": {
"line-color": "#000",
"line-width": 3
}
},
// polygon fill
{
"id": "gl-draw-polygon-fill-static",
"type": "fill",
"filter": ["all", ["==", "$type", "Polygon"], ["==", "mode", "static"]],
"paint": {
"fill-color": "#000",
"fill-outline-color": "#000",
"fill-opacity": 0.1
}
},
// polygon outline
{
"id": "gl-draw-polygon-stroke-static",
"type": "line",
"filter": ["all", ["==", "$type", "Polygon"], ["==", "mode", "static"]],
"layout": {
"line-cap": "round",
"line-join": "round"
},
"paint": {
"line-color": "#000",
"line-width": 3
}
}
]
});
map.addControl(draw);
var marker_mea,bigImg_mea;
map.on("draw.create",measureFunc);//绘制创建一个要素后,进行量算处理
map.on("draw.update",measureFunc);//修改一个要素后,进行量算处理
//量算处理方法
function measureFunc(e){
var features = e.features;
//测面
if(features[0].geometry.type == "Polygon"){
if(features.length > 0){
var area = turf.area(features[0]);
var rounded_area = Math.round(area * 100) / 100;
var htmlStr = '<p>面积:<strong>' + rounded_area + '</strong> 平方米</p>';
document.getElementById('resultD').innerHTML = htmlStr;
}
};
//测距
if(features[0].geometry.type == "LineString"){
var lineDistance = turf.lineDistance(features[0]);
var rounded_distance = Math.round(lineDistance * 100) / 100;
document.getElementById('resultD').innerHTML = '<p>距离:<strong>' + rounded_distance + '</strong> 千米</p>';
}
};
//画线
function distanceMea(){
draw.changeMode(draw.modes.DRAW_LINE_STRING);
};
//画面
function areaMea(){
draw.changeMode(draw.modes.DRAW_POLYGON);
};
//清除全部
function deleteAll(){
draw.deleteAll();
if(marker_mea){
marker_mea.remove();
};
if(bigImg_mea){
$("div").remove(".marker_mea");
};
document.getElementById('resultD').innerHTML = "";
};
//关闭
function deactivateCtrl(){
draw.changeMode(draw.modes.STATIC);
};
//激活
function activateCtrl(){
draw.changeMode(draw.modes.SIMPLE_SELECT);
};
</script>
</html>