张延森 преди 2 години
родител
ревизия
a1cbb1c83b
променени са 5 файла, в които са добавени 113 реда и са изтрити 95 реда
  1. 0
    14
      index.html
  2. 75
    79
      src/components/CustMap.vue
  3. 5
    2
      src/main.js
  4. 9
    0
      src/style.css
  5. 24
    0
      src/utils/amap.js

+ 0
- 14
index.html Целия файл

@@ -10,20 +10,6 @@
10 10
     <script src="./qrcode.min.js"></script>
11 11
     <script src="./config.js"></script>
12 12
     <title>和“宁好”一起,跟着诗歌游南京</title>
13
-    <script>
14
-      window._AMapSecurityConfig = {
15
-        securityJsCode: AMAP_SEC_CODE,
16
-      };
17
-    </script>
18
-    <style type="text/css">
19
-      .amap-logo{
20
-      display: none;
21
-      opacity:0 !important;
22
-      }
23
-      .amap-copyright {
24
-          opacity:0;
25
-      }
26
-    </style>
27 13
     <!-- <script src="./vconsole.min.js"></script>
28 14
     <script>
29 15
       const vConsole = new VConsole();

+ 75
- 79
src/components/CustMap.vue Целия файл

@@ -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>

+ 5
- 2
src/main.js Целия файл

@@ -1,10 +1,13 @@
1 1
 import { createApp } from 'vue'
2
+import VueViewer from 'v-viewer'
3
+import 'viewerjs/dist/viewer.css'
2 4
 import './style.css'
3 5
 import App from './App.vue'
4 6
 import store from './store'
5 7
 import wxsdk from './utils/wx'
6
-import 'viewerjs/dist/viewer.css'
7
-import VueViewer from 'v-viewer'
8
+import { amapInit } from './utils/amap'
9
+
10
+amapInit();
8 11
 
9 12
 const { share } = wxsdk(location.href)
10 13
 

+ 9
- 0
src/style.css Целия файл

@@ -17,4 +17,13 @@ html, body, #app {
17 17
 
18 18
 img {
19 19
   border: none;
20
+}
21
+
22
+/* 隐藏高德地图 logo */
23
+.amap-logo{
24
+  display: none;
25
+  opacity:0 !important;
26
+}
27
+.amap-copyright {
28
+  opacity:0;
20 29
 }

+ 24
- 0
src/utils/amap.js Целия файл

@@ -0,0 +1,24 @@
1
+import AMapLoader from '@amap/amap-jsapi-loader';
2
+
3
+export function amapInit() {
4
+  window._AMapSecurityConfig = {
5
+    securityJsCode: AMAP_SEC_CODE,  // 来源 public/config.js
6
+  };
7
+  AMapLoader.load({
8
+    "key": AMAP_KEY,  // 来源 public/config.js
9
+    "version": "2.0",
10
+    "plugins": [],
11
+  }).then((AMap)=>{
12
+    window.AMap = AMap;
13
+  }).catch((e)=>{
14
+    console.error(e);  //加载错误提示
15
+  });
16
+}
17
+
18
+export function newMarker(options = {}) {
19
+  return new AMap.Marker({
20
+    content: '<div class="cust-map-marker"></div>',  // 自定义点标记覆盖物内容
21
+    offset: new AMap.Pixel(-30, -10), // 相对于基点的偏移位置
22
+    ...options
23
+  });
24
+}