-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrage-route.html
308 lines (289 loc) · 15 KB
/
drage-route.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<title>可拖拽的路线规划</title>
<link rel="stylesheet" type="text/css" href="./index.css"/>
<!-- 重要,请到 https://console.amap.com 申请 JS API 的 key和密钥 -->
<script>
window._AMapSecurityConfig = {
securityJsCode: '290ddc4b0d33be7bc9b354bc6a4ca614',
}
</script>
<link rel="stylesheet" href="https://unpkg.zhimg.com/element-ui/lib/theme-chalk/index.css">
<script src="https://unpkg.zhimg.com/vue@2/dist/vue.js"></script>
<script src="https://unpkg.zhimg.com/element-ui/lib/index.js"></script>
<link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
<script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=6f025e700cbacbb0bb866712d20bb35c&plugin=AMap.PolylineEditor"></script>
<!-- <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script> -->
</head>
<body>
<div id="app">
<div id="container"></div>
<div class="tools">
<div class="item">
名称:<el-input type="text" size="small" v-model="form.road" ></el-input>
</div>
<div class="item">
序号:<el-input type="number" size="small" v-model="form.index" ></el-input>
</div>
<div class="item">
状态:<el-input type="number" size="small" v-model="form.status" ></el-input>
</div>
<div class="item">
描述:<input type="text" size="small" v-model="form.description" ></el-input>
</div>
<div class="item">
起点:<input type="text" size="small" v-model="form.firstPosition" ></el-input>
<el-button @click="fill('firstPosition')" type="text" >点击获取</el-button>
</div>
<div class="item">
终点:<input type="text" size="small" v-model="form.lastPosition" ></el-input>
<el-button @click="fill('lastPosition')" type="text" >点击获取</el-button>
</div>
<div class="item">
距离:{{distance / 1000}} km
</div>
<div style="display: flex; gap:4px;margin: 10px 0;">
<el-button type="button" size="small" @click="drawPath">规划路线</el-button>
<el-button type="button" size="small" @click="myPath">自定义路线</el-button>
</div>
<el-button type="primary" @click="submit" style="width: 100%;">保存</el-button>
</div>
</div>
<script>
new Vue({
el: '#app',
data: function() {
return {
map: null,
route: null,
type: null,
form: {
road: '',
description: '',
firstPosition: '',
lastPosition: '',
status: 1,
index: 0
},
distance: null,
polyEditor: null,
editablePolyline: null,
startMarker: null,
lastMarker: null,
}
},
mounted() {
this.map = new AMap.Map("container", {
resizeEnable: true
});
// 初始化AMap.DragRoute插件
this.map.plugin("AMap.DragRoute", function() {});
// 点击地图拾取坐标
this.map.on('click', (e) => {
let { lng, lat } = e.lnglat;
if(this.type) {
this.form[this.type] = `${lng},${lat}`;
// 创建marker的图标
const iconStart = new AMap.Icon({
size: new AMap.Size(19, 31),
image: "https://webapi.amap.com/theme/v1.3/markers/b/start.png",
imageSize: new AMap.Size(19, 31),
});
const iconEnd = new AMap.Icon({
size: new AMap.Size(19, 31),
image: "https://webapi.amap.com/theme/v1.3/markers/b/end.png",
imageSize: new AMap.Size(19, 31),
});
// 移除已存在的对应marker
if (this.type === 'firstPosition' && this.startMarker) {
this.map.remove(this.startMarker);
} else if (this.type === 'lastPosition' && this.lastMarker) {
this.map.remove(this.lastMarker);
}
// 创建新的marker
const marker = new AMap.Marker({
position: new AMap.LngLat(lng, lat),
title: this.type === 'firstPosition' ? '起点' : '终点',
anchor: 'bottom-center',
icon: this.type === 'firstPosition' ? iconStart : iconEnd,
});
// 将marker添加到地图并保存引用
this.map.add(marker);
if (this.type === 'firstPosition') {
this.startMarker = marker;
} else if (this.type === 'lastPosition') {
this.lastMarker = marker;
}
console.log(this.type);
this.type = null;
}
});
// 从URL获取参数
const urlParams = new URLSearchParams(window.location.search);
const firstPositionParam = urlParams.get('firstPosition');
const nameParam = urlParams.get('name');
const index = urlParams.get('index');
const description = urlParams.get('description');
// 如果参数存在,则更新表单数据
if (firstPositionParam) {
this.form.firstPosition = firstPositionParam;
// 将起点设置为地图中心
const [lng, lat] = firstPositionParam.split(',');
const icon = new AMap.Icon({
size: new AMap.Size(19, 31), //图标尺寸
// image: "//a.amap.com/jsapi_demos/static/demo-center/icons/dir-marker.png", //Icon 的图像
image: "https://webapi.amap.com/theme/v1.3/markers/b/start.png", //Icon 的图像
imageSize: new AMap.Size(19, 31), //根据所设置的大小拉伸或压缩图片
});
// 创建标记
this.startMarker = new AMap.Marker({
position: new AMap.LngLat(parseFloat(lng), parseFloat(lat)),
title: '起点',
anchor: 'bottom-center',
icon,
});
// 将标记添加到地图
this.map.add(this.startMarker);
this.map.setCenter([parseFloat(lng), parseFloat(lat)]);
}
if (nameParam) {
this.form.road = nameParam;
this.form.index = index || 0;
this.form.description = description || "";
}
},
methods: {
drawPath() {
// 清除现有的自定义路线
if (this.polyEditor) {
this.polyEditor.close();
this.polyEditor = null;
}
if (this.editablePolyline) {
this.map.remove(this.editablePolyline);
this.editablePolyline = null;
}
// 87.02526,48.696618
this.map.clearMap();
const { firstPosition, lastPosition } = this.form;
if (firstPosition && lastPosition) {
this.route?.destroy();
let path = [firstPosition.split(','), lastPosition.split(',')];
this.route = new AMap.DragRoute(
this.map,
path,
// AMap.DrivingPolicy.LEAST_FEE,
AMap.DrivingPolicy.LEAST_DISTANCE,
// AMap.DrivingPolicy.LEAST_TIME,
// AMap.DrivingPolicy.LEAST_FEE,
{
showTraffic: false,
polyOptions: {
strokeWeight: 4,
strokeOpacity: 0.6,
}
}
);
this.route.on('complete', (e) => {
console.log(e.data.routes[0].distance, 2222);
let { distance } = e.data.routes[0];
console.log(distance/1000, 'km');
this.distance = distance;
})
this.route.search();
// var distance = Math.round(AMap.GeometryUtil.distanceOfLine(arr));
}
},
fill(actionType = 'firstPosition') {
this.type = actionType;
},
myPath() {
// 清除现有的规划路线
if (this.route) {
this.route.destroy();
this.route = null;
}
// 创建可编辑折线
const { firstPosition, lastPosition } = this.form;
if (firstPosition && lastPosition) {
// 清除现有的折线
if (this.editablePolyline) {
this.map.remove(this.editablePolyline);
}
// 创建路径数组
const path = [
firstPosition.split(',').map(Number),
lastPosition.split(',').map(Number)
];
// 创建可编辑折线
this.editablePolyline = new AMap.Polyline({
path: path,
isOutline: true,
outlineColor: '#ffeeff',
borderWeight: 3,
strokeColor: "#3366FF",
strokeOpacity: 1,
strokeWeight: 3,
strokeStyle: "solid",
strokeDasharray: [10, 5],
lineJoin: 'round',
lineCap: 'round',
zIndex: 50,
editingTool: true,
// draggable: true
});
// 将折线添加到地图
this.map.add(this.editablePolyline);
// 缩放地图以适应折线
this.map.setFitView();
this.polyEditor = new AMap.PolylineEditor(this.map, this.editablePolyline);
this.polyEditor.setTarget(this.editablePolyline);
this.polyEditor.open();
}
},
async submit() {
const { road, description, index, status } = this.form;
let distance;
let coordinates = [];
if (this.polyEditor) {
coordinates = this.polyEditor.getTarget().getPath().map(item => [item.lng, item.lat]);
distance = Math.round(AMap.GeometryUtil.distanceOfLine(coordinates));
} else {
coordinates = this.route.getRoute().map(item => [item.lng, item.lat]);
distance = this.distance;
}
let data = {
road,
description,
index,
coordinates,
distance,
status,
}
fetch(`/api/addPath`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
// "Authorization": Authorization,
},
body: JSON.stringify(data)
}).then(res => res.json()).then(data => {
console.log(data);
this.$message({
message: '保存成功',
type: 'success'
});
setTimeout(() => {
window.close();
}, 2000);
})
},
}
})
</script>
</body>
</html>