index.vue
27.2 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
<!--
* @Date: 2023-05-19 14:54:27
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2023-05-29 06:15:20
* @FilePath: /tswj/src/views/Map.vue
* @Description: 文件描述
-->
<template>
<div class="">
<div id="container"></div>
<div class="input-card">
<div class="input-item">
<span @click="showPublic" class="btn">显示公共设施</span>
<span @click="hidePublic" class="btn">隐藏公共设施</span>
</div>
<div class="input-item">
<button class="btn" @click="addLayer" style="margin-right:1rem;">添加图层</button>
<button class="btn" @click="removeLayer">删除图层</button>
</div>
<div class="input-item">
<button class="btn" @click="setLocation" style="margin-right:1rem;">定位服务</button>
<button class="btn" @click="addLocation" style="margin-right:1rem;">添加定位路线</button>
<button class="btn" @click="removeLocation" style="margin-right:1rem;">删除定位路线</button>
<button class="btn" @click="setTitleLayer" style="margin-right:1rem;">添加贴片</button>
</div>
<div class="input-item">
<button class="btn" @click="setSpotLayer" style="margin-right:1rem;">景点信息</button>
<button class="btn" @click="setToiletLayer" style="margin-right:1rem;">卫生间</button>
</div>
<div class="input-item">
<button class="btn" @click="setWalkRoute" style="margin-right:1rem;">步行导航</button>
<button class="btn" @click="removeWalkRoute" style="margin-right:1rem;">移除步行导航</button>
</div>
</div>
<van-popup v-model:show="show_popup" position="bottom" :overlay="false" closeable
:style="{ padding: '64px' }">内容</van-popup>
<van-dialog v-model:show="dialog_show" title="温馨提示">
<div style="padding: 1rem; text-align: center;">您不在景区范围内</div>
</van-dialog>
</div>
</template>
<script>
// import { Flexbox, FlexboxItem } from 'vux'
// import { mapState } from 'vuex'
import coord from '@/common/tiantan_v2'
// import AMap from 'AMap'
import _ from 'lodash';
import { makeRequiredProp } from 'vant/lib/utils';
const GPS = {
PI: 3.14159265358979324,
x_pi: 3.14159265358979324 * 3000.0 / 180.0,
delta: function (lat, lon) {
var a = 6378245.0; // a: 卫星椭球坐标投影到平面地图坐标系的投影因子。
var ee = 0.00669342162296594323; // ee: 椭球的偏心率。
var dLat = this.transformLat(lon - 105.0, lat - 35.0);
var dLon = this.transformLon(lon - 105.0, lat - 35.0);
var radLat = lat / 180.0 * this.PI;
var magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
var sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * this.PI);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * this.PI);
return {
'lat': dLat,
'lon': dLon
};
},
//WGS-84 to GCJ-02
gcj_encrypt: function (wgsLat, wgsLon) {
if (this.outOfChina(wgsLat, wgsLon))
return {
'lat': wgsLat,
'lon': wgsLon
};
var d = this.delta(wgsLat, wgsLon);
return {
'lat': wgsLat + d.lat,
'lon': wgsLon + d.lon
};
},
outOfChina: function (lat, lon) {
if (lon < 72.004 || lon > 137.8347)
return true;
if (lat < 0.8293 || lat > 55.8271)
return true;
return false;
},
transformLat: function (x, y) {
var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * this.PI) + 40.0 * Math.sin(y / 3.0 * this.PI)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * this.PI) + 320 * Math.sin(y * this.PI / 30.0)) * 2.0 / 3.0;
return ret;
},
transformLon: function (x, y) {
var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * this.PI) + 20.0 * Math.sin(2.0 * x * this.PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * this.PI) + 40.0 * Math.sin(x / 3.0 * this.PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * this.PI) + 300.0 * Math.sin(x / 30.0 * this.PI)) * 2.0 / 3.0;
return ret;
}
};
export default {
// components: { Flexbox, FlexboxItem },
data() {
return {
map: '',
facilities: [],
imageLayer: '',
location_options: {
'showButton': true, // 是否显示定位按钮
'buttonPosition': 'LB', // 定位按钮的位置
/* LT LB RT RB */
'buttonOffset': new AMap.Pixel(10, 20), // 定位按钮距离对应角落的距离
'showMarker': true, // 是否显示定位点
'markerOptions': { // 自定义定位点样式,同Marker的Options
'offset': new AMap.Pixel(-18, -36),
'content': '<img src="https://a.amap.com/jsapi_demos/static/resource/img/user.png" style="width:36px;height:36px"/>'
},
'showCircle': true, // 是否显示定位精度圈
'circleOptions': { // 定位精度圈的样式
'strokeColor': '#0093FF',
'noSelect': true,
'strokeOpacity': 0.5,
'strokeWeight': 1,
'fillColor': '#02B0FF',
'fillOpacity': 0.25
},
enableHighAccuracy: true
},
geolocation: '',
current_lng: '',
current_lat: '',
current_route: '',
spotInfo: [], // 景点信息实体
toiletInfo: [], // 厕所信息实体
show_popup: false,
dialog_show: false,
xys_lng: '',
xys_lat: '',
walk_route: '',
}
},
mounted() {
// 初始化地图
this.initMap();
this.loadPublicEquipment();
this.loadScenicSpot();
this.loadPlant();
// this.setMapBoundary();
// 使用之前都要再获取一下地址
this.getLocation()
},
methods: {
// addMarker () {
// // axios.get('http://api.map.baidu.com/location/ip?ak=CzpAXV4bhyqCKnWpQqLRkS1i&coor=bd09ll')
// axios.get('https://restapi.amap.com/v5/ip?ip=114.88.176.153&type=4&key=d50d6e58d65408db18af05c0a9422a44')
// .then(res => {
// if (res.statusText === 'OK') {
// console.warn(res.data);
// //
// let marker = new AMap.Marker({
// icon: 'https://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png',
// position: [121.54431, 31.30164],
// offset: new AMap.Pixel(-13, -30)
// });
// marker.setMap(this.map);
// } else {
// console.warn(res);
// this.$vux.toast.show({
// type: 'warn',
// text: res.data.msg
// });
// }
// })
// .catch(err => {
// console.error(err);
// })
// },
initMap() {
// 初始化地图
this.map = new AMap.Map('container', {
viewMode: '3D', // 设置地图模式
turboMode: false,
showIndoorMap: false,
defaultCursor: 'pointer', // 地图默认鼠标样式
showBuildingBlock: false, // 是否展示地图 3D 楼块
zooms: [17, 19], // 地图显示的缩放级别范围, 默认为 [2, 20] ,取值范围 [2 ~ 30]
showLabel: true, // 是否展示地图文字和 POI 信息
zoom: 18, // 设置地图显示的缩放级别
pitch: 0, // 俯仰角度,默认 0,最大值根据地图当前 zoom 级别不断增大,2D地图下无效 。
rotation: 0, // 地图顺时针旋转角度,取值范围 [0-360] ,默认值:0
center: [120.587382, 31.314504], // 设置地图中心点坐标
forceVector: false,
rotateEnable: false,
layers: [
// new AMap.TileLayer.RoadNet(),
]
});
// 添加地图点击事件
this.map.on("click", this.showInfoClick);
},
layerMap() {
// 图层地图
this.imageLayer = new AMap.ImageLayer({
url: 'http://amappc.cn-hangzhou.oss-pub.aliyun-inc.com/lbs/static/img/dongwuyuan.jpg',
// url: 'https://img.zcool.cn/community/01462356c80e976ac7252ce62bc446.jpg@1280w_1l_0o_100sh.jpg',
// url: 'https://cdn.ipadbiz.cn/tmp/map_test/xys.jpg',
bounds: new AMap.Bounds(
[116.406897, 39.874346], [116.417451, 39.887843] // 对象西南角经纬度和东北角经纬度值
),
opacity: 0.5, // 图层透明度,默认为 1
zooms: [2, 18],
zIndex: 6 // 图层的层级,默认为 6
});
this.map = new AMap.Map('container', {
viewMode: '3D', // 设置地图模式
turboMode: false,
showIndoorMap: false,
defaultCursor: 'pointer', // 地图默认鼠标样式
showBuildingBlock: false, // 是否展示地图 3D 楼块
zooms: [2, 20], // 地图显示的缩放级别范围, 默认为 [2, 20] ,取值范围 [2 ~ 30]
showLabel: false, // 是否展示地图文字和 POI 信息
zoom: 18, // 设置地图显示的缩放级别
pitch: 0, // 俯仰角度,默认 0,最大值根据地图当前 zoom 级别不断增大,2D地图下无效 。
rotation: 0, // 地图顺时针旋转角度,取值范围 [0-360] ,默认值:0
center: [116.408967, 39.880101], // 设置地图中心点坐标
forceVector: false,
rotateEnable: false,
layers: [
new AMap.TileLayer.RoadNet(),
this.imageLayer
]
});
},
layerMap1() {
// 图层地图
this.imageLayer = new AMap.ImageLayer({
// url: 'http://amappc.cn-hangzhou.oss-pub.aliyun-inc.com/lbs/static/img/dongwuyuan.jpg',
// url: 'https://img.zcool.cn/community/01462356c80e976ac7252ce62bc446.jpg@1280w_1l_0o_100sh.jpg',
url: 'images/xys1.png',
bounds: new AMap.Bounds(
[120.585202, 31.312918], [120.588828, 31.316511] // 对象西南角经纬度和东北角经纬度值
// [116.327911, 39.939229],
// [116.342659, 39.946275] // 对象西南角经纬度和东北角经纬度值
),
opacity: 1, // 图层透明度,默认为 1
zooms: [2, 20],
zIndex: 6 // 图层的层级,默认为 6
});
this.map = new AMap.Map('container', {
viewMode: '3D', // 设置地图模式
turboMode: false,
showIndoorMap: false,
defaultCursor: 'pointer', // 地图默认鼠标样式
showBuildingBlock: false, // 是否展示地图 3D 楼块
zoom: 18, // 设置地图显示的缩放级别
zooms: [2, 20], // 地图显示的缩放级别范围, 默认为 [2, 20] ,取值范围 [2 ~ 30]
showLabel: false, // 是否展示地图文字和 POI 信息
pitch: 0, // 俯仰角度,默认 0,最大值根据地图当前 zoom 级别不断增大,2D地图下无效 。
rotation: 0, // 地图顺时针旋转角度,取值范围 [0-360] ,默认值:0
center: [120.587237, 31.314805], // 设置地图中心点坐标
forceVector: false,
rotateEnable: false,
resizeEnable: true, //是否监控地图容器尺寸变化
layers: [
new AMap.TileLayer.RoadNet(),
this.imageLayer,
]
});
},
layerMap2() {
// 图层地图
this.imageLayer = new AMap.ImageLayer({
url: 'https://amappc.cn-hangzhou.oss-pub.aliyun-inc.com/lbs/static/img/dongwuyuan.jpg',
bounds: new AMap.Bounds(
[116.327911, 39.939229],
[116.342659, 39.946275]
),
zooms: [15, 20]
});
this.map = new AMap.Map('container', {
center: [120.587237, 31.314805],
zoom: 15,
layers: [
AMap.createDefaultLayer(),
// this.imageLayer,
]
});
},
loadPublicEquipment() { // 加载公共设备图标
var zoomStyleMapping = { 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0 };
_.each(coord.sheshi, (x, i) => {
var marker = new AMap.ElasticMarker({
position: coord.sheshi[i].position,
zooms: [14, 20],
styles: [{
icon: {
img: coord.sheshi[i].icon,
size: [16, 16], // 可见区域的大小
anchor: 'bottom-center', // 锚点
fitZoom: 14, // 最合适的级别
scaleFactor: 2, // 地图放大一级的缩放比例系数
maxScale: 1.4, // 最大放大比例
minScale: 0.8 // 最小放大比例
}
}],
zoomStyleMapping
});
let infoWindowContent =
'<div className="custom-infowindow input-card">' +
'<label style="color:grey">公共厕所</label>' +
// 为 infowindow 添加自定义事件
'<div id="lnglat2container" class="btn">我要走过去</div>' +
'</div>';
// 创建一个自定义内容的 infowindow 实例
let infoWindow = new AMap.InfoWindow({
position: coord.sheshi[i].position,
offset: new AMap.Pixel(0, -30),
content: infoWindowContent
});
if (clickListener) {
makeRequiredProp.off('click', clickListener)
}
// 绑定景点的点击事件 - 文字出现才能触发
var clickListener = marker.on('click', (e) => {
infoWindow.open(this.map);
})
//
this.facilities.push(marker);
})
this.map.add(this.facilities);
},
loadScenicSpot() { // 加载景点图标
var spots = [];
var zoomStyleMapping = { 14: 0, 15: 0, 16: 1, 17: 1, 18: 1, 19: 1, 20: 1 }
_.each(coord.touristSpots, (x, i) => {
var marker = new AMap.ElasticMarker({
position: coord.touristSpots[i].position,
zooms: [14, 20],
styles: [{
icon: {
img: coord.touristSpots[i].smallIcon,
size: [16, 16], // 可见区域的大小
anchor: 'bottom-center', // 锚点
fitZoom: 14, // 最合适的级别
scaleFactor: 2, // 地图放大一级的缩放比例系数
maxScale: 2, // 最大放大比例
minScale: 1 // 最小放大比例
},
label: {
content: coord.touristSpots[i].name,
position: 'BM',
minZoom: coord.touristSpots[i].minZoom
}
}, {
icon: {
img: coord.touristSpots[i].bigIcon,
size: coord.touristSpots[i].size,
anchor: coord.touristSpots[i].anchor,
fitZoom: 17.5,
scaleFactor: 2,
maxScale: 2,
minScale: 0.125
},
label: {
content: coord.touristSpots[i].name,
position: 'BM',
minZoom: coord.touristSpots[i].minZoom
}
}],
zoomStyleMapping
});
if (clickListener) {
AMap.Event.removeListener(clickListener); // 移除地图事件,以绑定时返回的对象作为参数
}
// 绑定景点的点击事件 - 文字出现才能触发
var clickListener = AMap.Event.addListener(marker, 'click', function (e) {
alert(coord.touristSpots[i].name)
});
spots.push(marker);
})
this.map.add(spots);
},
loadPlant() { // 加载植物图标
var zoomStyleMapping = { 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0 };
var trees = new AMap.ElasticMarker({
position: [116.410908, 39.88057],
zooms: [15.5, 20],
styles: [{
icon: {
img: 'https://a.amap.com/jsapi_demos/static/resource/img/trees.png',
size: [366, 201],
anchor: 'center',
imageSize: [865, 1156],
imageOffset: [-44, -480],
fitZoom: 17.5,
scaleFactor: 2,
maxScale: 2,
minScale: 0.125
}
}],
zoomStyleMapping
});
this.map.add(trees);
},
loadSpotMaker() { // 加载景点标记
var zoomStyleMapping = { 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0 };
_.each(coord.spotInfo, (x, i) => {
var marker = new AMap.ElasticMarker({
position: coord.spotInfo[i].position,
zooms: [17, 19],
styles: [{
icon: {
img: coord.spotInfo[i].icon,
size: [16, 16], // 可见区域的大小
anchor: 'bottom-center', // 锚点
fitZoom: 14, // 最合适的级别
scaleFactor: 2, // 地图放大一级的缩放比例系数
maxScale: 1.4, // 最大放大比例
minScale: 0.8 // 最小放大比例
},
label: {
content: coord.spotInfo[i].name,
position: 'TM',
minZoom: 18
}
}],
zoomStyleMapping
});
let infoWindowContent =
'<div className="custom-infowindow input-card">' +
'<label style="color:grey">公共厕所</label>' +
// 为 infowindow 添加自定义事件
'<div id="lnglat2container" class="btn">我要走过去</div>' +
'</div>';
// 创建一个自定义内容的 infowindow 实例
let infoWindow = new AMap.InfoWindow({
position: coord.spotInfo[i].position,
offset: new AMap.Pixel(0, -30),
content: infoWindowContent
});
if (clickListener) {
marker.off('click', clickListener)
}
// 绑定景点的点击事件 - 文字出现才能触发
var clickListener = marker.on('click', (e) => {
infoWindow.open(this.map);
})
//
this.spotInfo.push(marker);
})
this.map.add(this.spotInfo);
},
loadToiletMaker() { // 加载厕所标记
var zoomStyleMapping = { 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0 };
_.each(coord.toiletInfo, (x, i) => {
var marker = new AMap.ElasticMarker({
position: coord.toiletInfo[i].position,
zooms: [17, 19],
styles: [{
icon: {
img: coord.toiletInfo[i].icon,
size: [16, 16], // 可见区域的大小
anchor: 'bottom-center', // 锚点
fitZoom: 14, // 最合适的级别
scaleFactor: 2, // 地图放大一级的缩放比例系数
maxScale: 1.4, // 最大放大比例
minScale: 0.8 // 最小放大比例
},
label: {
content: coord.toiletInfo[i].name,
position: 'TM',
minZoom: 18
}
}],
zoomStyleMapping
});
// let infoWindowContent =
// '<div className="custom-infowindow input-card">' +
// '<label style="color:grey">公共厕所</label>' +
// // 为 infowindow 添加自定义事件
// '<div id="lnglat2container" class="btn">我要走过去</div>' +
// '</div>';
// // 创建一个自定义内容的 infowindow 实例
// let infoWindow = new AMap.InfoWindow({
// position: coord.spotInfo[i].position,
// offset: new AMap.Pixel(0, -30),
// content: infoWindowContent
// });
if (clickListener) {
marker.off('click', clickListener)
}
// 绑定景点的点击事件 - 文字出现才能触发
var clickListener = marker.on('click', (e) => {
// infoWindow.open(this.map);
this.show_popup = true;
})
//
this.toiletInfo.push(marker);
})
this.map.add(this.toiletInfo);
},
setMapBoundary() { // 地图描边
new AMap.Polygon({
cursor: 'pointer',
bubble: true,
path: [[120.587704, 31.312785], [120.587669, 31.313028], [120.587554, 31.313086], [120.586883, 31.313019], [120.586826, 31.314066], [120.586736, 31.314426], [120.585872, 31.314466], [120.585675, 31.315276], [120.585817, 31.315397], [120.586251, 31.31542], [120.586229, 31.31618], [120.588248, 31.316367], [120.588533, 31.314761], [120.588479, 31.31474], [120.588482, 31.314172], [120.588251, 31.314145], [120.588337, 31.313184], [120.588337, 31.313184], [120.588154, 31.313163], [120.588152, 31.312835]],
map: this.map,
fillOpacity: 0.5,
strokeWeight: 1,
fillColor: '#CFE7AA'
});
},
showPublic() {
this.map.add(this.facilities);
},
hidePublic() {
this.map.remove(this.facilities);
},
addLayer() {
this.layerMap();
// this.layerMap1();
// this.layerMap2();
this.loadPublicEquipment();
this.loadScenicSpot();
this.loadPlant();
this.setMapBoundary();
},
removeLayer() {
this.initMap();
this.loadPublicEquipment();
this.loadScenicSpot();
this.loadPlant();
this.setMapBoundary();
},
setLocation() { // 开启定位服务
// this.map.addControl(this.geolocation); // 添加定位图标 自动跳转当前位置
// 使用纠正偏移后的地址,打一个定位标记
var marker = new AMap.Marker({
position: new AMap.LngLat(this.current_lng, this.current_lat), // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
});
this.map.add(marker);
// 定位到地图中心-西园寺
this.map.setCenter([120.587334, 31.314823]);
// 判断是否在范围内
let isPointInRing = AMap.GeometryUtil.isPointInRing([this.current_lng, this.current_lat], [
[120.585111, 31.316084], [120.585111, 31.316084], [120.589488, 31.313197], [120.585422, 31.313005]
]);
if (!isPointInRing) {
this.dialog_show = true;
}
},
getLocation() {
AMap.plugin(['AMap.Geolocation'], () => {
this.geolocation = new AMap.Geolocation(this.location_options);
this.geolocation.getCurrentPosition((status, result) => {
if (status === 'complete') {
let lat = result.position.lat;
let lng = result.position.lng;
// 行动路线
if (lng && lat) {
this.current_lng = GPS.gcj_encrypt(lat, lng).lon;
this.current_lat = GPS.gcj_encrypt(lat, lng).lat;
// 西园寺的经纬度
this.xys_lng = GPS.gcj_encrypt(31.315021, 120.58848).lon;
this.xys_lat = GPS.gcj_encrypt(31.315021, 120.58848).lat;
}
}
})
});
},
setRoute() { // 生成路径
// 行动路线
var path = [
[116.413384, 39.874071],
[116.413141, 39.877934],
[116.411469, 39.878175],
[this.current_lng, this.current_lat]
];
this.current_route = new AMap.Polyline({
path: path,
isOutline: true,
outlineColor: 'red',
borderWeight: 1,
strokeColor: '#FFF',
strokeOpacity: 1,
strokeWeight: 1,
// 折线样式还支持 'dashed'
strokeStyle: 'solid',
// strokeStyle是dashed时有效
strokeDasharray: [10, 5],
lineJoin: 'round',
lineCap: 'round',
zIndex: 50
})
},
addLocation() { // 新增路径
this.setRoute();
this.map.add([this.current_route]);
},
removeLocation() { // 删除路径
this.map.remove([this.current_route]);
},
setTitleLayer() {
//该demo可模拟水印效果
var layer = new AMap.TileLayer.Flexible({
cacheSize: 30,
opacity: 1,
zIndex: 2,
createTile: function (x, y, z, success, fail) {
// if ((x + y) % 3) {
// fail();
// return;
// }
var img = document.createElement('img');
img.onload = function () {
success(img)
};
img.crossOrigin = "anonymous";// 必须添加,同时图片要有跨域头
img.onerror = function () {
fail()
};
// img.src = `fileHost + '/tilefile/' + zoom + '/${x} + '_' + ${y} + '.png`;
img.src = `images/tiles/${z}/${x}_${y}.jpg`
}
});
this.map.addLayer(layer);
// Canvas作为切片
var layer = new AMap.TileLayer.Flexible({
// tileSize: 128,
cacheSize: 300,
zIndex: 200,
createTile: function (x, y, z, success, fail) {
var c = document.createElement('canvas');
c.width = c.height = 256;
var cxt = c.getContext("2d");
cxt.font = "15px Verdana";
cxt.fillStyle = "#ff0000";
cxt.strokeStyle = "#FF0000";
cxt.strokeRect(0, 0, 256, 256);
cxt.fillText('(' + [x, y, z].join(',') + ')', 10, 30);
// 通知API切片创建完成
success(c);
}
});
layer.setMap(this.map);
// 只显示相应区域,移动会回到选定范围
this.lockMapBounds()
},
setSpotLayer() { // 设置景点图层
this.removeToiletLayer()
// 加载地图标记
this.loadSpotMaker()
},
setToiletLayer() { // 设置景点图层
this.removeSpotLayer()
// 加载厕所标记
this.loadToiletMaker()
},
removeSpotLayer() { // 删除景点标记
this.map.remove(this.spotInfo);
},
removeToiletLayer() { // 删除景点标记
this.map.remove(this.toiletInfo);
},
// 限制地图范围
lockMapBounds() {
// var bounds = this.map.getBounds();
var myBounds = new AMap.Bounds(
[120.585138, 31.312908],
[120.588839, 31.316492]
);
this.map.setLimitBounds(myBounds);
},
showInfoClick(e) {
console.log(e);
var zoom = this.map.getZoom(); //获取当前地图级别
var text =
"您在 [ " +
e.lnglat.getLng() +
"," +
e.lnglat.getLat() +
" ] 的位置单击了地图!当前层级" +
zoom;
console.warn(text);
},
setWalkRoute() {
//步行导航
let walking_CallBack = (result) => {
if (result.type === 'complete') {
console.warn(result);
console.warn('绘制步行路线完成');
} else {
console.error('步行路线数据查询失败' + result);
}
}
AMap.plugin(["AMap.Walking"], () => { //加载步行导航插件
this.walk_route = new AMap.Walking ({
map: this.map,
}); //构造步行导航类
AMap.Event.addListener(this.walk_route, "complete", walking_CallBack); //返回导航查询结果
//根据起、终点坐标规划步行路线
this.walk_route.search(new AMap.LngLat(120.587799, 31.313276), new AMap.LngLat(120.587912, 31.315169));
});
},
removeWalkRoute () {
this.walk_route.clear()
}
}
}
</script>
<style lang="less">
#container {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
}
/* 标记文字样式 */
.amap-marker-label {
/* width: 60px; */
font-size: 0.85rem;
line-height: 18px;
text-align: center;
height: 18px;
font-family: STLiti;
color: #000;
border: 0;
/* border-radius: 15px; */
background-color: rgba(255, 255, 255, 0.4);
/* border: solid 2px rgba(100, 255, 200, 0.8); */
}
.input-card {
display: flex;
flex-direction: column;
min-width: 0;
word-wrap: break-word;
background-color: #fff;
background-clip: border-box;
border-radius: .25rem;
width: 20rem;
border-width: 0;
border-radius: 0.4rem;
box-shadow: 0 2px 6px 0 rgba(114, 124, 245, .5);
position: fixed;
top: 1rem;
right: 1rem;
-ms-flex: 1 1 auto;
flex: 1 1 auto;
padding: 0.75rem 1.25rem;
}
</style>