当前位置:   article > 正文

vue2+高德地图_vue2中amap.infowindow

vue2中amap.infowindow
vue2+高德地图
1. 高德地图的引入
2. 自定义高德地图
3. 高德地图多个地方打点
4, 打点后显示弹窗内容
5. 关闭弹窗
<template>
  <div>
    <div id="container"></div>
    <div ref="infoData">
      <div class="info-top">
        <div>
          <span>{{ devInfo.title }}</span>
          <span style="font-size: 11px; margin-left: 20px; color: #f00"
            >状态:{{ devInfo.state }}</span
          >
        </div>
        <img
          @click="closeInfoWindow"
          src="https://webapi.amap.com/images/close2.gif"
        />
      </div>
      <div class="info-middle" style="background-color: white">
        <img class="info-img" src="img/dev.png" /><a
          class="info-a-title"
          href="https://ditu.amap.com/detail/B000A8URXB?citycode=110105"
          >XXXXXXXXXXXX</a
        ><br />地址:{{ devInfo.address }}<br />
        <div class="info-div">综合能耗:{{ devInfo.electricity }}</div>
        <span class="info-span">
          电{{ devInfo.gas }}<br />
          <div class="info-div">水{{ devInfo.guan }}</div>
          <span class="info-span"> 焦炉煤气{{ devInfo.water }}</span
          ><br />
          <div class="info-div">高炉煤气:{{ devInfo.years }}</div>
        </span>
      </div>
      <div
        class="info-bottom"
        style="position: relative; top: 0px; margin: 0px auto"
      >
        <img src="https://webapi.amap.com/images/sharp.png" />
      </div>
    </div>
  </div>
</template>
 
<script>
import AMapLoader from "@amap/amap-jsapi-loader";

export default {
  name: "MapComponent",
  data() {
    return {
      devInfo: {
        title: "1",
        icon: "1",
        state: "1",
        address: "1",
        electricity: "1",
        gas: "1",
        guan: "1",
        water: "1",
        years: "1",
        time: "1",
        mianji: "1",
      },
      map: null, //初始化 map 对象
      markers: [], // 存储标点
      infoWindowData: [
        //数据可自行修改
        {
          title: "1号厂区",
          icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png", //点标记图片路径
          state: "正常使用",
          address: "XXXXXXXXXXX",
          electricity: "5428542°",
          gas: "454575NM",
          guan: "210个",
          water: "3235T",
          years: "10年",
          position: [108.971642, 34.247119],
          lng: 116.39,
          lat: 39.92,
        },
        {
          title: "2号厂区",
          icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png", //点标记图片路径
          state: "已废弃",
          address: "XXXXXXXXXXXXX",
          electricity: "5428542°",
          gas: "454575NM",
          guan: "210个",
          water: "3235T",
          years: "15年",
          position: [108.9768933198242, 34.24537248278517],
          lng: 116.42,
          lat: 39.92,
        },
        {
          title: "3号厂区",
          icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png", //点标记图片路径
          state: "正常使用",
          address: "XXXXXXXXXXXX",
          electricity: "5428542°",
          gas: "454575NM",
          guan: "210个",
          water: "3235T",
          years: "10年",
          position: [108.979965, 34.250659],
          lng: 116.41,
          lat: 39.91,
        },
      ],
    };
  },
  mounted() {
    this.initMap();
  },
  methods: {
    initMap() {
      window._AMapSecurityConfig = {
        securityJsCode: "", // 高德地图申请的秘钥
      };
      AMapLoader.load({
        key: "",  // 申请好的Web端开发者Key,首次调用 load 时必填
        version: "2.0",
        plugins: [
          "AMap.Geolocation",
          "AMap.PlaceSearch",
          "AMap.Scale",
          "AMap.OverView",
          "AMap.ToolBar",
          "AMap.MapType",
          "AMap.PolyEditor",
          "AMap.CircleEditor",
        ],
      })
        .then((AMap) => {
          const map = new AMap.Map("container", {
            zoom: 11,
            center: [116.397428, 39.90923], // 设置中心点坐标
            mapStyle: "amap://styles/951766ea7040afc523d1b3bdbe33a894", //自定义样式id
          });
          this.map = map;
          // 创建点标点
          this.createMarkers();
        })
        .catch((e) => {
          console.log(e);
        });
    },
    // 1. 创建点标点
    createMarkers() {
      const markers = [];
      let arr = [];
      for (let i = 0; i < this.infoWindowData.length; i++) {
        const positions = [
          this.infoWindowData[i].lng,
          this.infoWindowData[i].lat,
        ];
        const icon = new AMap.Icon({
          size: new AMap.Size(20, 30), // 设置图标的宽度和高度
          imageSize: new AMap.Size(20, 30), // 设置图标显示时的大小
          image: "https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png", // 自定义图标的 URL
        });
        const marker = new AMap.Marker({
          position: positions,
          map: this.map,
          icon: icon,
        });
        markers.push(
          Object.assign(marker, {
            mapId: marker._amap_id,
          })
        );
        arr.push(
          Object.assign(this.infoWindowData[i], {
            mapId: marker._amap_id,
          })
        );
        marker.on("click", () => {
          this.markerClick(arr, marker); // 调用处理点击事件的方法
        });
      }
      this.markers = markers;
    },
    //2.点击标记 获取所点击标记的信息以及窗体要展示的数据,创建信息窗体
    markerClick(arr, marker) {
      let arrNew = arr.filter((x) => x.mapId == marker._amap_id);
      this.devInfo = arrNew && arrNew[0];
      let infoWindow = this.createInfoWindow();
      this.openInfoWindow(infoWindow, marker);
    },
    //3.构建自定义窗体
    createInfoWindow() {
      let infoWindowData = new AMap.InfoWindow({
        isCustom: true, //使用自定义窗体
        content: this.$refs.infoData,
        // content: this.getContent(),
        offset: new AMap.Pixel(16, -45),
      });
      return infoWindowData;
    },
    //4.打开窗体
    openInfoWindow(infoWindow, marker) {
      infoWindow.open(this.map, marker.getPosition());
    },
    //5.关闭窗体
    closeInfoWindow() {
      this.map.clearInfoWindow();
    },
  },
};
</script>
<style>
html,
body,
#container {
  height: 100%;
  width: 100%;
}

.content-window-card {
  position: relative;
  box-shadow: none;
  bottom: 0;
  left: 0;
  /* width: auto; */
  width: 100px;
  padding: 0;
}

.content-window-card p {
  height: 50px;
}

.custom-info {
  border: solid 1px silver;
}

div.info-top {
  position: relative;
  background: none repeat scroll 0 0 #f9f9f9;
  border-bottom: 1px solid #ccc;
  border-radius: 5px 5px 0 0;
}

div.info-top div {
  display: inline-block;
  color: #333333;
  font-size: 14px;
  font-weight: bold;
  line-height: 31px;
  padding: 0 10px;
}

div.info-top img {
  position: absolute;
  top: 10px;
  right: 10px;
  transition-duration: 0.25s;
}

div.info-top img:hover {
  box-shadow: 0px 0px 5px #000;
}

div.info-middle {
  font-size: 12px;
  padding: 10px 6px;
  line-height: 20px;
}

div.info-bottom {
  height: 0px;
  width: 100%;
  clear: both;
  text-align: center;
}

div.info-bottom img {
  position: relative;
  z-index: 104;
}

/* span {
  margin-left: 5px;
  font-size: 11px;
} */

.info-middle img {
  float: left;
  margin-right: 6px;
}

.info-span {
  /* margin-left: 35px; */
  font-size: 11px;
}

.info-div {
  width: 140px;
  display: inline-block;
  margin-left: 10px;
}

.info-img {
  width: 40px;
  height: 40px;
}

.info-a-title {
  /* color: #000000; */
  font-size: 16px;
}
#container {
  /*因为我自己的组件是在一个套了很多层的页面上使用的,所以这里需要给地图给一个固定的高,宽是100%可省略不写,否则地图会因为它自身的定位而不显示*/
  height: 820px;
}
#container .amap-icon img,
.amap-marker-content img {
  width: 64px;
  height: 64px;
}
</style>
  • 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

在这里插入图片描述

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号