高德.js2.0绘制点聚合,并点击点出现自定义样式弹窗

在这里插入图片描述
我这里依旧使用AMapLoader插件

代码如下

  // 初始化高德地图
    initMap() {
      AMapLoader.load({
        key: "fb35c92d4019cfafeca876fd5514bb47", //key值是key值 和安全密钥不同
        version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: [
          "AMap.GeoJSON",
          "AMap.MarkerClusterer",
          "AMap.MouseTool",
          "AMap.Geocoder",
          "AMap.Marker",
          "AMap.Polyline",
          "AMap.InfoWindow",
        ], // 需要使用的的插件列表,用到的再次注册,如比例尺'AMap.Scale'等
      }).then((AMap) => {
        // 初始化地图
        this.map = new AMap.Map("mapContainer", {
          viewMode: "3D", // 是否为3D地图模式
          zoom: 14.0, // 初始化地图级别
          center: [114.885796, 40.768555], //中心点坐标
          resizeEnable: true,
        });
        //使用CSS默认样式定义地图上的鼠标样式
        this.map.setDefaultCursor("pointer");

        let styleName = "amap://styles/whitesmoke";
        this.map.setMapStyle(styleName);
        this.setMraker();
      });
    },
    setMraker() {
      let arr = this.mapPointsList.map((item) => {
        return {
          lnglat: [item.cordsArr[0], item.cordsArr[1]],
          extData: item,
        };
      });
     // 此处为点聚合
      this.map.plugin(["AMap.MarkerCluster"], () => {
        this.markerClusterer = new AMap.MarkerCluster(this.map, arr, {
          gridSize: 100,
          renderMarker: this.renderMarker,
        });

        this.mapPointMrksClu.push(this.markerClusterer);
      });
    },
    // 自定义点位渲染方法
    renderMarker(context) {
      const marker = context.marker;

      const item = context.data[0].extData;

      let bgtTM = "https://union.huaxindata.com.cn/dma/img/icon_19.png";
      // 获取当 marker 类型的覆盖物
      // 创建点面覆盖物实例
      let icon = new AMap.Icon({
        // 图标尺寸
        size: new AMap.Size(53, 53),
        // 图标的取图地址
        image: item.iconUrl,
        // 图标所用图片大小
        // imageSize: new AMap.Size(12, 12),
      });
      // 点标记显示内容,HTML要素字符串

      // <img src="img/icon_19.png">
      let markerContent = `
  <div class="map-item__base" id='mark${item.id}'>
    <div class="arrowBox">
      <img src="${bgtTM}">
    </div>
    <div style="height:50px"><i class="fontfamily ali-icon-dingweisvg" style="color:${item.typeColour}"></i></div>
    <div class="pointBox"> <img src="${item.iconUrl}"></div>
  </div>
  `;

      let content = `<div class='pointName' id='pointName${item.id}'><div class="pointTxt">${item.name}</div></div>`;
      let offset = new AMap.Pixel(0, 0); //设置偏移量
      marker.setIcon(icon);
      marker.setContent(markerContent);
      marker.setOffset(offset);
      marker.setLabel({
        direction: "top",
        content: content,
        offset: new AMap.Pixel(0, -5),
      });

      let pointName = document.getElementsByClassName("pointName")[0];
      if (pointName) {
        if (item.nameDisplay == 1) {
          pointName.style.display = "block";
        } else {
          pointName.style.display = "none";
        }
      }

      let div = document.getElementsByClassName("amap-marker-label");
      if (div) {
        // 鼠标移入移出事件
        if (item.nameDisplay == 0) {
          $(div).bind("mouseenter", () => {
            // 鼠标移入显示标签
            pointName.style.display = "block";
            div.style.zIndex = "999";
          });
          $(div).bind("mouseleave", () => {
            // 鼠标移出隐藏标签
            pointName.style.display = "none";
            div.style.zIndex = "unset";
          });
        }
      }

      // 给点添加点击事件
      marker.on("click", (event) => {
        // classify  1溯源站,2排水户
        if (item.classify == "2") {
          this.clickMark(event, item);
        } else {
          this.currentNode = item;
          this.changeTree();
        }
      });
      this.mapPointMrksClu.push(marker);
    },
    // 打开信息窗体
    clickMark(e, item) {
      let title = item?.name;
      //实例化信息窗体
      let content = [];

      let trs = "";
      if (item.indexVOS && item.indexVOS.length) {
        item.indexVOS.forEach((e) => {
          trs += `<ul class="meddil_val">
             <li>
            <div class="title_info">${e.indexName}:</div>
            <div class="title_value" id="${item.id}-${e.pointId}"> ${
            e.val || ""
          }<span class="unit">${e.unit}</span>
            </div>
          </li>
          </ul>`;
        });
      } else {
        trs += "<ul class='meddil_val'><li>暂无数据</li></ul>";
      }
      content.push(trs);

      this.infoWindow = new AMap.InfoWindow({
        isCustom: true, //使用自定义窗体
        content: this.createInfoWindow(title, content.join("<br/>"), item),
        offset: new AMap.Pixel(16, -45),
      });
      this.infoWindow.open(this.map, e.target.getPosition());
    },
    //构建自定义信息窗体
    createInfoWindow(title, content, item) {
      let info = document.createElement("div");
      info.className = "custom-info input-card content-window-card";

      //可以通过下面的方式修改自定义窗体的宽高
      info.style.width = "400px";
      // 定义顶部标题
      let top = document.createElement("div");
      let titleD = document.createElement("div");
      let closeX = document.createElement("div");
      top.className = "info-top";
      top.style.backgroundColor = item.typeColour;

      titleD.innerHTML = title;
      closeX.innerHTML = "<i class='el-icon-close'></i>";
      closeX.onclick = this.closeInfoWindow;

      top.appendChild(titleD);
      top.appendChild(closeX);
      info.appendChild(top);

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

      return info;
    },

    //关闭信息窗体
    closeInfoWindow() {
      this.map.clearInfoWindow();
    },

自定义样式如下

/deep/.amap-marker {
  position: relative;
  height: 50px;
  .map-item__base {
    position: absolute;
    z-index: 9852432;
    color: rgb(255, 255, 255);
    white-space: nowrap;
    font-size: 12px;
    cursor: pointer;
    user-select: none;
    top: -27px;
    left: -14px;
  }
  .arrowBox {
    position: absolute;
    height: 50px;
    top: -1px;
    left: 1px;
    img {
      position: absolute;
      height: 50px;
    }
  }
  .fontfamily {
    position: absolute;
    left: 5px;
    top: 4px;
    font-size: 34px;
  }
  .pointBox {
    position: absolute;
    left: 10px;
    top: 8px;
    width: 24px;
    height: 22px;

    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    img {
      display: inline-block;
      width: 16px;
    }
  }
}
/deep/.el-icon-close {
  color: #fff;
  font-size: 17px !important;
  cursor: pointer;
  font-weight: 600 !important;
  margin: 0 0 0 10px !important;
}
/deep/ .content {
  max-height: 500px;
  overflow: auto;
}
.rala_select {
  width: 120px;
  margin-left: 8px;
}
/deep/.amap-icon {
  font-size: 34px;
  background: rgb(71, 63, 63);
}
/deep/ .amap-marker-label {
  top: 20px !important;
  border: 0;
  background: transparent;
  margin-left: -16px;

  .pointTxt {
    color: rgb(0, 0, 0);
    font-size: 14px;
    font-weight: 600;
  }
}
/deep/.amap-marker {
  position: relative;
  height: 50px;
  .map-item__base {
    position: absolute;
    z-index: 9852432;
    color: rgb(255, 255, 255);
    white-space: nowrap;
    font-size: 12px;
    cursor: pointer;
    user-select: none;
    top: -27px;
    left: -14px;
  }
  .arrowBox {
    position: absolute;
    height: 50px;
    top: -1px;
    left: 1px;
    img {
      position: absolute;
      height: 50px;
    }
  }
  .fontfamily {
    position: absolute;
    left: 5px;
    top: 4px;
    font-size: 34px;
  }
  .pointBox {
    position: absolute;
    left: 10px;
    top: 8px;
    width: 24px;
    height: 22px;

    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    img {
      display: inline-block;
      width: 16px;
    }
  }
}
/deep/.info-top {
  display: flex;
  justify-content: space-between;
  padding: 2px 8px;
  color: #fff;
  line-height: 30px;
}
/deep/.info-middle {
  max-height: 300px;
  overflow: auto;
}
/deep/.info-middle .meddil_val {
  padding: 4px 0;
  li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 20px 4px 8px;
    font-size: 14px;
    .unit {
      display: inline-block;
      width: 40px;
      margin-left: 12px;
      color: #adaab2;
    }
  }
}
在使用高德地图API和Vue3实现自定义弹窗样式时,可以参考以下步骤: 1. 首先,初始化地图弹窗实例,并设置isCustom为true,表示使用自定义窗体。可以使用AMap.InfoWindow类来创建地图弹窗实例,并设置偏移量等属性。\[1\] 2. 在处理地图标记物点击事件时,可以通过获取点击的标记物对象,设置动画效果,并获取相应的内容。可以使用AMap.InfoWindow的setContent方法设置弹窗内容,然后使用open方法打开弹窗。\[1\] 3. 可以参考高德地图官方提供的demo样式,该demo展示了自定义弹窗样式。可以在官方demo中查看代码和样式,以便参考和使用。\[2\] 4. 高德地图官网还提供了自定义弹窗内容的例子,使用了Dom操作的方式来实现。可以使用Vue的全局API中的Vue.extend方法来创建一个Vue子类构造器,然后通过实例化该构造器来创建一个Vue对象,类似于Dom中的DocumentFragment接口。这样可以使用Vue的方式来实现自定义弹窗的Dom操作。\[3\] 综上所述,你可以使用高德地图API和Vue3来实现自定义弹窗样式。可以参考高德地图官方提供的demo和文档,根据自己的需求进行相应的代码编写和样式设计。 #### 引用[.reference_title] - *1* *3* [高德地图自定义弹窗内容](https://blog.csdn.net/fredricen/article/details/106172766)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [VUE 高德自定义弹窗样式不生效?(AMap.InfoWindow弹窗样式问题)](https://blog.csdn.net/weixin_39921970/article/details/120744647)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值