Blame view

wenjuan/src/components/MapContainer/MapXian.vue 4.01 KB
e5b57447   杨鑫   '分包问卷'
1
  <template>
d64cd58f   wesley88   上传验收小程序
2
    <div id="container2" />
e5b57447   杨鑫   '分包问卷'
3
4
5
6
  </template>
  
  <script>
  import AMapLoader from '@amap/amap-jsapi-loader'
d64cd58f   wesley88   上传验收小程序
7
  window._AMapSecurityConfig = {
e5b57447   杨鑫   '分包问卷'
8
    		securityJsCode: '0909896c00dc4bf28c6ee99a43533c53'
d64cd58f   wesley88   上传验收小程序
9
10
11
  }
  var map = {}; // 最好在data外声明要不然地图会一卡一卡
  var markers = []; // 最好在data外声明要不然地图会一卡一卡
e5b57447   杨鑫   '分包问卷'
12
13
  export default {
    name: 'MapXian',
d64cd58f   wesley88   上传验收小程序
14
15
16
    components: {},
    props: ['message', 'edit', 'sendMap'],
    data () {
e5b57447   杨鑫   '分包问卷'
17
18
19
      return {
        auto: null,
        map: null,
d64cd58f   wesley88   上传验收小程序
20
21
22
23
24
25
26
27
28
29
30
31
32
        newValue: false,
        mapData: { longitude: 104.113368, // 经纬度
          latitude: 30.518559, // 经纬度
        },
      }
    },
    watch: {
      // 监听父组件传递的值
      edit (newValue, oldValue) {
        console.log(newValue, oldValue)
        if (newValue == false) {
          this.newValue = true
        }
e5b57447   杨鑫   '分包问卷'
33
34
      }
    },
d64cd58f   wesley88   上传验收小程序
35
36
37
38
39
40
41
    mounted () {
      // DOM初始化完成进行地图初始化
      this.initMap()
    },
    beforeDestroy () {
      this.destroyMap();
    },
e5b57447   杨鑫   '分包问卷'
42
    methods: {
d64cd58f   wesley88   上传验收小程序
43
      initMap () {
e5b57447   杨鑫   '分包问卷'
44
45
46
47
        AMapLoader.load({
          key: '438a727f8ff61d71f33694f386c108c3', // 申请好的Web端开发者Key,首次调用 load 时必填
          terrain: true,
          version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
d64cd58f   wesley88   上传验收小程序
48
          plugins: ['AMap.ToolBar', 'AMap.Scale', 'AMap.HawkEye', 'AMap.Geolocation', 'AMap.AutoComplete', 'AMap.Marker'], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
e5b57447   杨鑫   '分包问卷'
49
50
51
        })
          .then(AMap => {
            this.map = new AMap.Map('container2', {
d64cd58f   wesley88   上传验收小程序
52
53
54
55
              // 设置地图容器id
              viewMode: '3D', // 是否为3D地图模式
              zoom: 10, // 初始化地图级别
              center: [this.message.longitude || 104.113368, this.message.latitude || 30.518559], // 初始化地图中心点位置
e5b57447   杨鑫   '分包问卷'
56
57
58
59
60
            });
            this.map.addControl(new AMap.Scale())
            this.map.addControl(new AMap.ToolBar())
            this.map.addControl(new AMap.HawkEye())
            this.map.addControl(new AMap.Geolocation())
d64cd58f   wesley88   上传验收小程序
61
62
            this.markPoints()
            this.latitude()
e5b57447   杨鑫   '分包问卷'
63
64
65
66
          })
          .catch(e => {
            console.log(e)
          })
e5b57447   杨鑫   '分包问卷'
67
      },
d64cd58f   wesley88   上传验收小程序
68
      markPoints () {
e5b57447   杨鑫   '分包问卷'
69
        this.map.remove(markers);
d64cd58f   wesley88   上传验收小程序
70
71
72
73
        let marker;
        // 创建一个 Marker 实例:
        if (this.newValue === false) {
          marker = new AMap.Marker({
e5b57447   杨鑫   '分包问卷'
74
            icon: new AMap.Icon({
d64cd58f   wesley88   上传验收小程序
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
              // size: new AMap.Size(19, 32),
              // anchor:new AMap.Pixel(20, 30),
              image: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png', // 默认图标的URL
            }),
            position: new AMap.LngLat(this.message.longitude || 104.113368, this.message.latitude || 30.518559),
          });
        } else {
          marker = new AMap.Marker({
            icon: new AMap.Icon({
              // size: new AMap.Size(19, 32),
              // anchor:new AMap.Pixel(20, 30),
              image: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png', // 默认图标的URL
            }),
            position: new AMap.LngLat(this.mapData.longitude, this.mapData.latitude),
          });
e5b57447   杨鑫   '分包问卷'
90
        }
d64cd58f   wesley88   上传验收小程序
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
        // 将创建的点标记添加到已有的地实例:
        markers.push(marker);
        this.map.add(marker);
        this.map.setFitView();
      },
      // 点击获取经纬度
      latitude () {
        this.map.on('click', (ev) => {
          if (this.newValue) {
            // 触发事件的地理坐标,AMap.LngLat 类型
            const lnglat = ev.lnglat;
            // 触发事件的像素坐标,AMap.Pixel 类型
            const pixel = ev.pixel;
            // 触发事件类型
            const type = ev.type;
            this.mapData.longitude = lnglat.lng
            this.mapData.latitude = lnglat.lat
            this.markPoints()
            console.log(lnglat)
            this.sendMap(this.mapData)
          }
        });
      },
      destroyMap () {
        if (this.map) {
          this.map.destroy();
          this.map = null;
        }
      }
e5b57447   杨鑫   '分包问卷'
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    }
  }
  </script>
  
  <style lang="less">
  #container2 {
    position: relative;
    padding: 0px;
    margin: 0px;
    width: 100%;
    height: 100%;
  }
  .amap-icon{
    img {
       width:19px;
       height:30px;
     }
  }
  
  </style>