绘制高德地图边界并且添加点位点击出现弹窗,关闭弹窗,以及鼠标移入移出点位事件,移除点位

在这里插入图片描述
地图上的点位图片

 <div id="map" class="map" ref="map" style="width:100%;height:100%"></div>
data{
 	  popoverMarker: null,
      zhandianList: [
        {
          companyName: "金家岭街道",
          time: "2023-07-10",
          geneQty: 20,
          disposeRate: 10,
          dispose: 78,
          lat: 36.18,
          lon: 120.62,
        },
        {
          companyName: "北宅街道",
          time: "2023-07-10",
          geneQty: 10,
          disposeRate: 20,
          dispose: 45,
          lat: 36.11,
          lon: 120.47,
        },
      ],
      myMap: null,
      markers:[],
	 }
 created() {
     //标注文字上的点击事件
    window.aa = () => {
      console.log("点击关闭");
      this.popoverMarker.hide();
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.creatMap();
    });
  },
 // 获取点位数据
    getPosition(query) {
      this.addMarker(this.zhandianList);
    },
 // 地图
    creatMap() {
      let that = this;
      this.myMap = new AMap.Map("map", {
        center: [120.55, 36.19], //东西经,南北纬
        // mapStyle: "amap://styles/30a69840de521825d1c73041ed88fbb6",
        zoom: 11,
        resizeEnable: true,
      });
      //获取边界坐标点
      AMap.plugin("AMap.DistrictSearch", () => {
        var districtSearch = new AMap.DistrictSearch({
          // 关键字对应的行政区级别,共有5种级别
          level: "district",
          //  是否显示下级行政区级数,1表示返回下一级行政区
          subdistrict: 0,
          // 返回行政区边界坐标点
          extensions: "all",
        });
        // 搜索所有省/直辖市信息
        districtSearch.search("崂山区", (status, result) => {
          // 查询成功时,result即为对应的行政区信息
          console.log("打印result:", result);
        });
        that.drawBounds();
      });
    },
    // 绘制区域图
    drawBounds() {
      var district = null;
      var polygons = [];
      let that = this;
      //加载行政区划插件
      if (!district) {
        //实例化DistrictSearch
        var opts = {
          subdistrict: 0, //获取边界不需要返回下级行政区
          extensions: "all", //返回行政区边界坐标组等具体信息
          level: "district", //查询行政级别为 市
        };
        district = new AMap.DistrictSearch(opts);
      }
      //行政区查询
      district.search("崂山区", function (status, result) {
        polygons = [];
        var bounds = result.districtList[0].boundaries;
        if (bounds) {
          for (var i = 0, l = bounds.length; i < l; i++) {
            //生成行政区划polygon
            var polygon = new AMap.Polygon({
              strokeWeight: 3,
              path: bounds[i],
              fillOpacity: 0.4,
              fillColor: "#80d8ff", //区域的颜色
              strokeColor: "#a3b1c0", //描边的颜色
            });
            polygons.push(polygon);
          }
        }
        that.myMap.add(polygons);
        // that.myMap.setFitView(polygons); //视口自适应
      });
    },
    addMarker(dian) {
      // console.log("打点:", dian);
      let that = this;
      let marker;
      let icon = require("@/assets/images/productionWaste.png");
      dian.forEach((item) => {
        console.log("aaa", item);
        marker = new AMap.Marker({
          icon,
          size: new AMap.Size(29, 30),
          imgSize: new AMap.Size(29, 30),
          position: [item.lon, item.lat],
          textContent: item,
          offset: new AMap.Pixel(-10, -20),
        });
        marker.setMap(that.myMap);
        that.markers.push(marker);
        marker.on("click", function (e) {
          let arr = [];
          that.infor = e.target._originOpts.textContent;
          const position = [that.infor.lng, that.infor.lat + 0.04];
          that.myMap.setCenter(position); //设置地图中心
          let contentText = `<div style="display:flex;flex-direction: column; align-items: center;">
            <div class="popover-box">
              <div class = "popover-title"><div class="popover-title-name"><div class="line"></div>第一化工厂</div><div id="close" οnclick="aa()">x</div></div>
              <div class="popover-info">
                <div class="popover-info-item">数据时间:<span class="popover-info-item-data">${
                  item.time ? item.time : ""
                }</span></div>
                <div class="popover-info-item">固废产生量:<span class="popover-info-item-data">${
                  item.geneQty ? item.geneQty : ""
                }</span></div>
                <div class="popover-info-item">综合利用量:<span class="popover-info-item-data">${
                  item.dispose ? item.dispose : ""
                }</span></div>
                <div class="popover-info-item">处置率:<span class="popover-info-item-data">${
                  item.disposeRate ? item.disposeRate : ""
                }</span></div>
              </div>
              <div class="fatherBreathIcon">
                <div class="sonBreathIcon" style="background: rgba(70, 143, 254, 1); width: 10px; height: 10px; animation-delay: 0s;"></div>
                <div class="sonBreathIcon" style="background: rgba(70, 143, 254, 0.8); width: 15px; height: 15px; animation-delay: 0.4s;"></div>
                <div class="sonBreathIcon" style="background: rgba(70, 143, 254, 0.6); width: 20px; height: 20px; animation-delay: 0.8s;"></div>
                <div class="sonBreathIcon" style="background: rgba(70, 143, 254, 0.4); width: 25px; height: 25px; animation-delay: 1.2s;"></div>
                <div class="sonBreathIcon" style="background: rgba(70, 143, 254, 0.2); width: 35px; height: 35px; animation-delay: 1.6s;"></div>
             </div>
            </div>
            `;
          if (!that.popoverMarker) {
            that.popoverMarker = new AMap.Marker({
              map: that.myMap,
              content: contentText,
              position: e.target._position,
              offset: new AMap.Pixel(-119, -127),
            });
            arr.push(that.popoverMarker);
          } else {
            that.popoverMarker.show();
            that.popoverMarker.setContent(contentText);
            that.popoverMarker.setPosition(e.target._position);
          }
        });
        //鼠标移入事件
        //marker.on("mouseover", function (e) {}
        //鼠标移出
        // marker.on("mouseout", function (e) {
        // that.popoverMarker.hide();
        // });
      });
    },
//移除点位
this.markers.forEach((ele) => {
 this.myMap.remove(ele);
});
<style lang="scss">
.fatherBreathIcon {
  width: 60px;
  height: 60px;
  position: absolute;
  bottom: 0;
  left: 49.75%;
  transform: translate(-50%, 40%) rotateX(60deg);
  .sonBreathIcon {
    position: absolute;
    top: 210%;
    left: 55%;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    animation-name: iconBreathLight;
    animation-iteration-count: infinite;
    animation-timing-function: linear;
    animation-duration: 2s;
    -webkit-user-drag: none;
    filter: none;
    opacity: 0;
    z-index: -1;
  }
}
.fatherBreathIcon-table {
  width: 60px;
  height: 60px;
  transform: translate(-50%, 40%) rotateX(60deg);
  .sonBreathIcon {
    position: absolute;
    top: 326%;
    left: 255.5%;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    animation-name: iconBreathLight;
    animation-iteration-count: infinite;
    animation-timing-function: linear;
    animation-duration: 2s;
    -webkit-user-drag: none;
    filter: none;
    opacity: 0;
    z-index: -1;
  }
}

@keyframes iconBreathLight {
  0% {
    opacity: 0;
  }
  10% {
    opacity: 0.2;
  }
  20% {
    opacity: 0.4;
  }
  30% {
    opacity: 0.6;
  }
  40% {
    opacity: 0.8;
  }
  50% {
    opacity: 1;
  }
  60% {
    opacity: 0.8;
  }
  70% {
    opacity: 0.6;
  }
  80% {
    opacity: 0.4;
  }
  90% {
    opacity: 0.2;
  }
  100% {
    opacity: 0;
  }
}
.popover-box-table {
  position: relative;
  margin-top: -70px;
  margin-left: -126px;
}
.popover-box {
  width: 248px;
  background: #fff;
  white-space: nowrap;
  font-size: 14px;
  padding: 15px 15px 6px;
  box-shadow: 0 0 12px 0 #0000001a;
  border-radius: 4px;
  margin-top: -93px;
  margin-left: -2px;
  position: relative;
  &::before {
    content: "";
    border-top: 10px solid #797979;
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    position: absolute;
    left: 50%;
    -webkit-transform: translateX(-50%);
    transform: translateX(-50%);
    bottom: -8px;
    border-radius: 5px;
  }
  &::after {
    content: "";
    border-top: 8px solid #fff;
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    position: absolute;
    left: 50%;
    -webkit-transform: translateX(-50%);
    transform: translateX(-50%);
    bottom: -6px;
    border-radius: 5px;
  }
  .popover-title {
    border-bottom: 1px solid #e1e6f0;
    padding: 0px 0px 10px 0px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .popover-title-name {
      font-weight: 700;
      display: flex;
      align-items: center;
      .line {
        border-radius: 10px;
        width: 3px;
        height: 14px;
        background: #3886ff;
        margin-right: 10px;
      }
    }
  }
  .popover-info {
    margin-top: 6px;
    .popover-info-item {
      color: #909299;
      padding: 7px 0;
      display: flex;
      .popover-info-item-data {
        font-weight: 700;
        color: #303133;
        display: block;
        overflow-x: auto;
      }
    }
  }
}
// ::-webkit-scrollbar{
//     height: 9px;
//     width: 9px;
// }

// ::-webkit-scrollbar-thumb {
//     border-radius: 10px;
//     background-color: rgba(13, 78, 230, 0.4);
// }
::-webkit-scrollbar-thumb:hover {
  background: rgba(192, 192, 192, 1);
}
</style>
可以使用KonvaJS的事件监听器来实现鼠标移入group时动态添加虚线边框,移出时消失,并且鼠标点击group后添加实线边框并且不显示虚线边框。具体实现步骤如下: 1. 首先,创建一个group对象,并将其添加到KonvaJS的舞台(stage)中。 2. 使用KonvaJS的事件监听器,监听group对象的"mouseenter"、"mouseleave"和"click"事件。 3. 在"mouseenter"事件处理函数中,创建一个矩形(rect)对象,并设置其填充色为透明,边框颜色和宽度为想要的颜色和宽度,线型为虚线,将其添加到group对象中。 4. 在"mouseleave"事件处理函数中,移除矩形对象。 5. 在"click"事件处理函数中,获取矩形对象并更新其线型为实线,再重新绘制group对象。 以下是示例代码: ```javascript // 创建一个group对象 var group = new Konva.Group({ x: 50, y: 50, width: 100, height: 100 }); // 将group对象添加到舞台中 stage.add(group); // 监听group对象的"mouseenter"、"mouseleave"和"click"事件 group.on("mouseenter", function() { // 创建一个矩形对象 var rect = new Konva.Rect({ width: group.width(), height: group.height(), stroke: "red", strokeWidth: 2, dash: [5, 5], fillEnabled: false }); // 将矩形对象添加到group对象中 group.add(rect); // 重新绘制group对象 group.draw(); }); group.on("mouseleave", function() { // 移除矩形对象 group.children[0].destroy(); }); group.on("click", function() { // 获取矩形对象 var rect = group.children[0]; // 更新矩形对象的线型 rect.dashEnabled(false); // 重新绘制group对象 group.draw(); }); ``` 以上代码实现了鼠标移入group后动态添加虚线边框,移出后消失,并且鼠标点击group后添加实线边框并且不显示虚线边框的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值