|
@@ -6,8 +6,8 @@
|
6
|
6
|
</template>
|
7
|
7
|
|
8
|
8
|
<script setup>
|
9
|
|
- import AMapLoader from '@amap/amap-jsapi-loader';
|
10
|
|
- import { onMounted, reactive, ref, watch } from "vue";
|
|
9
|
+ import { ref } from "vue";
|
|
10
|
+ import { newMarker } from '@/utils/amap'
|
11
|
11
|
|
12
|
12
|
const props = defineProps({
|
13
|
13
|
image: {
|
|
@@ -22,85 +22,73 @@
|
22
|
22
|
|
23
|
23
|
const elRef = ref();
|
24
|
24
|
const mapStyle = ref({});
|
25
|
|
- const mapState = reactive({ AMap: undefined, img: undefined, inited: false });
|
26
|
|
-
|
27
|
|
- const onLoad = e => (mapState.img = e.target);
|
28
|
|
-
|
29
|
|
- watch(mapState, (nw, od) => {
|
30
|
|
- if (nw.AMap && nw.img && !nw.inited) {
|
31
|
|
- nw.inited = true;
|
32
|
|
-
|
33
|
|
- const zooms = [15, 20];
|
34
|
|
- const { AMap, img } = nw;
|
35
|
|
- mapStyle.value = { width: `${img.width}px`, height: `${img.height}px` };
|
36
|
|
-
|
37
|
|
- // 初始化地图
|
38
|
|
- const center = [118.798954,31.963517];
|
39
|
|
- const map = new AMap.Map(elRef.value, {
|
40
|
|
- center,
|
41
|
|
- zooms,
|
42
|
|
- zoom: 15,
|
43
|
|
- });
|
44
|
|
-
|
45
|
|
- // 获取左上角经纬度
|
46
|
|
- // 正常情况坐标应该是 0,0 ; 但是实际 0,0 正好是地图的一半位置, 不清楚为啥
|
47
|
|
- const lt = map.containerToLngLat(new AMap.Pixel(0, -img.height / 2));
|
48
|
|
- // 获取右下角经纬度
|
49
|
|
- const rb = map.containerToLngLat(new AMap.Pixel(img.width, img.height / 2));
|
50
|
|
-
|
51
|
|
- // 图片层
|
52
|
|
- const imageLayer = new AMap.ImageLayer({
|
53
|
|
- url: props.image,
|
54
|
|
- bounds: new AMap.Bounds(
|
55
|
|
- [lt.lng, lt.lat],
|
56
|
|
- [rb.lng, rb.lat]
|
57
|
|
- ),
|
58
|
|
- zooms,
|
59
|
|
- });
|
60
|
|
- map.setLayers([imageLayer])
|
61
|
|
-
|
62
|
|
- // 背景层
|
63
|
|
- const backCovers = [
|
64
|
|
- [
|
65
|
|
- new AMap.LngLat(-360,90,true),
|
66
|
|
- new AMap.LngLat(-360,-90,true),
|
67
|
|
- new AMap.LngLat(360,-90,true),
|
68
|
|
- new AMap.LngLat(360,90,true),
|
69
|
|
- ],
|
70
|
|
- [
|
|
25
|
+
|
|
26
|
+ const onLoad = e => {
|
|
27
|
+ const img = e.target;
|
|
28
|
+
|
|
29
|
+ const zooms = [15, 16.5];
|
|
30
|
+ mapStyle.value = { width: `${img.width}px`, height: `${img.height}px` };
|
|
31
|
+
|
|
32
|
+ // 初始化地图
|
|
33
|
+ const center = [118.798954,31.963517];
|
|
34
|
+ const map = new AMap.Map(elRef.value, {
|
|
35
|
+ center,
|
|
36
|
+ zooms,
|
|
37
|
+ zoom: zooms[0],
|
|
38
|
+ });
|
|
39
|
+
|
|
40
|
+ // 获取左上角经纬度
|
|
41
|
+ // 正常情况坐标应该是 0,0 ; 但是实际 0,0 正好是地图的一半位置, 不清楚为啥
|
|
42
|
+ const lt = map.containerToLngLat(new AMap.Pixel(0, -img.height / 2));
|
|
43
|
+ // 获取右下角经纬度
|
|
44
|
+ const rb = map.containerToLngLat(new AMap.Pixel(img.width, img.height / 2));
|
|
45
|
+
|
|
46
|
+ // 图片层
|
|
47
|
+ const bounds = new AMap.Bounds(
|
71
|
48
|
[lt.lng, lt.lat],
|
72
|
|
- [rb.lng, lt.lat],
|
73
|
|
- [rb.lng, rb.lat],
|
74
|
|
- [lt.lng, rb.lat],
|
75
|
|
- ]
|
|
49
|
+ [rb.lng, rb.lat]
|
|
50
|
+ );
|
|
51
|
+ const imageLayer = new AMap.ImageLayer({
|
|
52
|
+ url: props.image,
|
|
53
|
+ bounds,
|
|
54
|
+ zooms,
|
|
55
|
+ });
|
|
56
|
+ map.setLayers([imageLayer])
|
|
57
|
+
|
|
58
|
+ // 背景层
|
|
59
|
+ const backCovers = [
|
|
60
|
+ [
|
|
61
|
+ new AMap.LngLat(-360,90,true),
|
|
62
|
+ new AMap.LngLat(-360,-90,true),
|
|
63
|
+ new AMap.LngLat(360,-90,true),
|
|
64
|
+ new AMap.LngLat(360,90,true),
|
|
65
|
+ ],
|
|
66
|
+ [
|
|
67
|
+ [lt.lng, lt.lat],
|
|
68
|
+ [rb.lng, lt.lat],
|
|
69
|
+ [rb.lng, rb.lat],
|
|
70
|
+ [lt.lng, rb.lat],
|
76
|
71
|
]
|
77
|
|
-
|
78
|
|
- const polygon = new AMap.Polygon({
|
79
|
|
- pathL: backCovers,
|
80
|
|
- strokeWeight: 0,
|
81
|
|
- fillColor: '#71B3ff',
|
82
|
|
- fillOpacity: 0.5
|
83
|
|
- });
|
84
|
|
- polygon.setPath(backCovers);
|
85
|
|
- map.add(polygon);
|
86
|
|
-
|
87
|
|
- map.on('click',function(e){
|
88
|
|
- console.log('--------->', e)
|
89
|
|
- })
|
90
|
|
- }
|
91
|
|
- });
|
92
|
|
-
|
93
|
|
- onMounted(() => {
|
94
|
|
- AMapLoader.load({
|
95
|
|
- "key": props.amapKey,
|
96
|
|
- "version": "2.0",
|
97
|
|
- "plugins": [],
|
98
|
|
- }).then((AMap)=>{
|
99
|
|
- mapState.AMap = AMap;
|
100
|
|
- }).catch((e)=>{
|
101
|
|
- console.error(e); //加载错误提示
|
102
|
|
- });
|
103
|
|
- })
|
|
72
|
+ ]
|
|
73
|
+
|
|
74
|
+ const polygon = new AMap.Polygon({
|
|
75
|
+ pathL: backCovers,
|
|
76
|
+ strokeWeight: 0,
|
|
77
|
+ fillColor: '#71B3ff',
|
|
78
|
+ fillOpacity: 0.5
|
|
79
|
+ });
|
|
80
|
+ polygon.setPath(backCovers);
|
|
81
|
+ map.add(polygon);
|
|
82
|
+
|
|
83
|
+ // 限制拖动范围
|
|
84
|
+ map.setLimitBounds(bounds);
|
|
85
|
+
|
|
86
|
+ map.add(newMarker({ position: [118.795993, 31.966611] }))
|
|
87
|
+
|
|
88
|
+ map.on('click',function(e){
|
|
89
|
+ console.log('--------->', e)
|
|
90
|
+ })
|
|
91
|
+ };
|
104
|
92
|
</script>
|
105
|
93
|
|
106
|
94
|
<style lang="less" scoped>
|
|
@@ -126,3 +114,11 @@
|
126
|
114
|
}
|
127
|
115
|
|
128
|
116
|
</style>
|
|
117
|
+
|
|
118
|
+<style lang="less">
|
|
119
|
+.cust-map-marker {
|
|
120
|
+ width: 60px;
|
|
121
|
+ height: 20px;
|
|
122
|
+ background: rgba(255, 0, 0, .5);
|
|
123
|
+}
|
|
124
|
+</style>
|