vue 调用高德地图api实现标点和显示窗体

 

一、前期工作

1.注册高德地图成为开发者

2.创建一个新应用、获取秘钥

 

 

二、引入

1.找到public  index.html文件插入js

1  <script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=秘钥&&plugin=AMap.Geocoder,AMap.AutoComplete,AMap.PlaceSearch"></script> 

需要地图工具类的把变量加入到plugin中

三、初始化地图,显示多个点标记,点击出现窗体

1.amap.vue

<template>
  <div class="my-container">
    <div id="map-demo"></div>
  </div>
</template>
    
<script>
import createInfoWindow from "@/utils/amap";
export default {
  data() {
    return {
      map: null,
      infoWindow: null,
      closeImg: require("@/assets/close.png"),
      pointList: [
        {
          address: "郑州市收到货爽肤水1",
          local: [113.651398, 34.767445],
        },
        {
          address: "郑州市收到货爽肤水2",
          local: [113.851398, 34.767445],
        },
        {
          address: "郑州市收到货爽肤水3",
          local: [113.751398, 34.667445],
        },
        {
          address: "郑州市收到货爽肤水4",
          local: [113.641398, 34.747445],
        },
      ],
      winInfo: [],
      winTitle: "",
      markList: [],
    };
  },
  mounted() {
    this.carGPSIP();
  },
  methods: {
    carGPSIP() {
      let self = this;
      this.map = new AMap.Map("map-demo", {
        resizeEnable: true,
        zoom: 8, //级别
        center: [113.663221, 34.7568711], //中心点坐标
        viewMode: "2D", //使用3D视图
        mapStyle: "amap://styles/darkblue",
      }); //初始化地图

      //遍历生成多个标记点
      for (var i = 0, marker; i < this.pointList.length; i++) {
        var marker = new AMap.Marker({
          position: this.pointList[i].local, //不同标记点的经纬度
          map: self.map,
        });

        marker.title = `<span>大厅车间</span><span>设备名称</span>`;
        marker.content = JSON.stringify([
          "电话:" + "123456789",
          "地址:" + this.pointList[i].address,
          "位置:" + this.pointList[i].local,
        ]);
        marker.on("click", self.markerClick);
        // marker.emit("click", { target: marker }); //默认初始化不出现信息窗体,打开初始化就出现信息窗体
      }

      //实例化信息窗体
      this.infoWindow = new AMap.InfoWindow({
        isCustom: true, //使用自定义窗体
        content: self.winInfo,
        offset: new AMap.Pixel(15, -35),
      });
      this.map.setFitView();
    },
    // 点标记点击事件
    markerClick(e) {
      let self = this;
      this.winInfo = JSON.parse(e.target.content);
      this.winTitle = e.target.title;
      // 设置窗体内容
      this.infoWindow.setContent(
        createInfoWindow.createInfoWindow(
          self.winTitle,
          self.winInfo.join("<br/>"),
          function () {
            // 关闭窗体
            self.map.clearInfoWindow();
          }
        )
      );
      // 打开窗体
      self.infoWindow.open(self.map, e.target.getPosition());
    },
  },
};
</script>
    
<style scoped>
#map-demo {
  width: 500px;
  height: 500px;
}
</style>

2.utils/amap.js

封装窗体结构样式

function createInfoWindow(title, content,callback) {
    var info = document.createElement("div");
    info.className = "custom-info input-card content-window-card";

    //可以通过下面的方式修改自定义窗体的宽高
    info.style.width = "400px";
    // 定义顶部标题
    var top = document.createElement("div");
    // var titleD = document.createElement("div");
    var closeX = document.createElement("img");
    top.className = "info-top";
    
    closeX.src = require("@/assets/close.png");
    closeX.onclick = callback;

    // top.appendChild(titleD);
    top.innerHTML = title;
    top.appendChild(closeX);
    info.appendChild(top);

    // 定义中部内容
    var middle = document.createElement("div");
    middle.className = "info-middle";
    middle.style.backgroundColor = "white";
    middle.innerHTML = content;
    info.appendChild(middle);

    // 定义底部内容
    var bottom = document.createElement("div");
    bottom.className = "info-bottom";
    bottom.style.position = "relative";
    bottom.style.top = "0px";
    bottom.style.margin = "0 auto";
    info.appendChild(bottom);
    return info;
  }
  
  export default {
    createInfoWindow
}

3.amap.scss

地图窗体的样式文件

.content-window-card {
    position: relative;
    box-shadow: none;
    bottom: 0;
    left: 0;
    width: auto;
    padding: 0;
  }
  
  .content-window-card p {
    height: 2rem;
  }
  
  .custom-info {
    /* border: solid 1px silver; */
  }
  
  div.info-top {
    position: relative;
    background: #0f49b5;
    height: 30px;
    line-height: 30px;
    font-size: 14px;
    color: #fff;
    padding: 0 10px;
  }
  
  div.info-top span:nth-child(2) {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    width: 200px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    text-align: center;
  }
  
  div.info-top img {
    position: absolute;
    top: 8px;
    right: 10px;
    transition-duration: 0.25s;
    width: 15px;
  }
  
  div.info-top img:hover {
    box-shadow: 0px 0px 5px #000;
  }
  
  div.info-middle {
    background: rgba(0,54,98,.8)!important;
    font-size: 12px;
    padding: 10px 6px;
    line-height: 20px;
    color: #fff;
  }
  
  div.info-bottom {
    height: 0px;
    width: 100%;
    clear: both;
    text-align: center;
    width: 0;
      height: 0;
      border-left: 5px solid transparent;
      border-right: 15px solid transparent;
      border-top: 20px solid rgba(0,54,98,.8);
  }
  
  div.info-bottom img {
    position: relative;
    z-index: 104;
  }
  
  
  .info-middle img {
    float: left;
    margin-right: 6px;
  }

 

 

 
posted @ 2021-10-21 16:51  白小杨  阅读(2185)  评论(0编辑  收藏  举报