Vue - 高德地图添加覆盖物,及覆盖物的点击方法

<template>
  <div class="wrap">
    <div id="map"></div>
  </div>
</template>

<script>
let map = null;
export default {
  data() {
    return {
      lineData: [
        {
          path: [
            [118.715995, 32.0219131],
            [118.735995, 32.0219131],
          ],
          content: "第一个线路",
        },
        {
          path: [
            [118.745995, 32.1219131],
            [118.745995, 32.0219131],
          ],
          content: "第二个线路",
        },
      ],
      markerData: [
        {
          position: [118.715995, 32.0319131],
          content: "第一个点标记",
        },
        {
          position: [118.715995, 32.0419131],
          content: "第二个点标记",
        },
      ],
    };
  },
  mounted() {
    this.getMap();
  },
  methods: {
    getMap() {
      map = new AMap.Map("map", {
        mapStyle: "amap://styles/fresh", //设置地图的显示样式
        zoom: 13, //设置地图显示的缩放级别
        center: [118.735995, 32.0219131], //设置地图中心点坐标
      });
      /**
       * 地图上画线
       */
      this.lineData.forEach((item) => {
        let path = item.path;
        let lineContent = '<div class="info">' + item.content + "</div>";
        // 创建折线实例
        var polyline = new AMap.Polyline({
          path: path,
          borderWeight: 2, // 线条宽度,默认为 1
          strokeColor: "red", // 线条颜色
          lineJoin: "round", // 折线拐点连接处样式
        });
        // 将折线添加至地图实例
        polyline.setMap(map);
        //点击地图线要素的方法
        polyline.on("click", polylineClick);
        function polylineClick(e) {
          console.log(item.content + "点击成功!");
          let infoWindow = new AMap.InfoWindow({
            content: lineContent,
            offset: new AMap.Pixel(0, -30),
          });
          infoWindow.open(map, [e.lnglat.lng, e.lnglat.lat]);
        }
      });
      /**
       * 地图上画点
       */
      this.markerData.forEach((item) => {
        var marker = new AMap.Marker({
          //   icon: '//vdata.amap.com/icons/b18/1/2.png', // 添加 Icon 图标 URL
          position: item.position, // 基点位置
          offset: new AMap.Pixel(-10, -32), // 相对于基点的偏移位置
        });
        marker.setMap(map);
        let markerContent = '<div class="info">' + item.content + "</div>";
        //点击地图点标记的方法
        marker.on("click", function (e) {
          console.log(item.content + "点击成功!");
          let infoWindow = new AMap.InfoWindow({
            content: markerContent,
            offset: new AMap.Pixel(0, -30),
          });
          infoWindow.open(map, e.target.getPosition());
        });
      });
    },
  },
};
</script>

<style scoped>
#map {
  width: 1080px;
  height: 750px;
}
</style>

转载于:https://blog.csdn.net/Jie_1997/article/details/108776383

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值