Blame view

admin-web-master/src/components/newmap/map.vue 2.73 KB
a182f238   wesley88   1
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
  <template>
    <div style="position: relative;width: 100%;">
      <div id="mapContainer" class="map"></div>
    </div>
  </template>
  
  <script>
  export default {
    name: 'TencentMap',
    props: {
      lat: {
        type: Number,
        default: 30.67
      },
      lng: {
        type: Number,
        default: 104.06
      },
      isonloed: {
        type: Boolean,
        default: false
      },
      isx: {
        type: Boolean,
        default: true
      },
      ontype: {
        type: String,
        default: '1'
      },
      message: {
        type: Array,
        default: () => []
      },
    },
    data() {
      return {
        map: null,
        marker: null,
        address: '',
        markers: [],
        infoWindow: null,
      };
    },
    mounted() {
      // 初始化地图
      this.initMap(this.message);
    },
    methods: {
      initMap(e) {
        // 将经纬度转换为腾讯地图的LatLng对象
        const centerLatLng = new qq.maps.LatLng(this.lat, this.lng);
  
        // 创建地图实例
        this.map = new qq.maps.Map(document.getElementById('mapContainer'), {
          center: centerLatLng,
          zoom: 13
        });
  
        // 创建信息窗口实例
        this.infoWindow = new qq.maps.InfoWindow({
          map: this.map
        });
  
        // 遍历 message 数组,为每个位置创建一个标记
        if (e.length > 0) {
          e.forEach(item => {
            let list = this.ontype == '1' ? item.mapPunctuation.split(',') : this.ontype == '2' ? item.mapPunctuation.split(',') : this.ontype == '3' ? item.mapMarker.split(',') : [];
            item.lat = parseFloat(list[0]);
            item.lng = parseFloat(list[1]);
            item.name = this.ontype == '1' ? item.shopName : this.ontype == '2' ? item.advertisingName : this.ontype == '3' ? item.venueName :'无';
            const markerLatLng = new qq.maps.LatLng(item.lat, item.lng);
            const marker = new qq.maps.Marker({
              position: markerLatLng,
              map: this.map
            });
  
            // 为标记添加点击事件监听器
            qq.maps.event.addListener(marker, 'click', () => {
              this.showInfoWindow(marker, item);
            });
  
            this.markers.push(marker);
          });
        }
      },
      showInfoWindow(marker, item) {
        // 设置信息窗口的内容
        const content = `
          <div>
            <p><strong>资源名称:</strong> ${item.name}</p>
            <p><strong>位置:</strong> ${item.detailedLocation}</p>
          </div>
        `;
  
        // 设置信息窗口的位置和内容
        this.infoWindow.setPosition(marker.getPosition());
        this.infoWindow.setContent(content);
        this.infoWindow.open();
      }
    }
  }
  </script>
  
  <style scoped>
  .map {
    width: 100%;
    height: 400px;
  }
  
  .address-info {
    position: absolute;
    bottom: 0;
    left: 0;
    z-index: 999999;
    background: #fff;
  }
  </style>