高德地图点聚合,添加点击事件,并显示弹窗

1.地图实例化,拿到map,这一步就不写了

2.拿到要聚合的点数据 ,不管你的矢量数据是什么样子,都处理为以下格式,变量名必须为lnglat

const amapPoints = filteredData.map((item) => {

   const lnglat = [item.longitude, item.latitude]; // 坐标格式为 [longitude,latitude]

   return { lnglat};

});

3.通过map添加点聚合插件,amapPoints就是你要聚合的点的集合,到这一步点就加载地图上了,并且缩放地图可以聚合。

 this.map.plugin(["AMap.MarkerClusterer"], () => {
     let markers = new AMap.MarkerClusterer(this.map, amapPoints, {
         gridSize: 80,
         renderClusterMarker: _renderClusterMarker, //这里是设置聚合点的样式,不写为默认样式
         renderMarker:(context)=> {                 // 这里是未聚合点的样式,不写为默认样式
           // 设置未聚合点样式
         };                                                  
     });
         
 });
 var _renderClusterMarker = function (context) {  
   // 设置聚合点样式
 };

 4.设置未聚合点样式

  renderMarker: (context) => {
      const Info = context.data[0].data;
      const name = Info.unitName;
      var content = `<div style="background-color: hsla(180, 100%, 50%, 0.4);height: 28px;line-height: 28px;padding: 0 10px;border: 1px solid hsl(180, 100%, 40%);box-shadow: hsl(180, 100%, 50%) 0px 0px 3px;white-space: nowrap;">${name}</div>`;
      context.marker.setContent(content);
 },

5.给每个点绑定点击事件

context.marker.on("click", clickEvent);

6.点击地图上的点显示弹窗,并自定义弹窗 

const clickEvent = (e) => {
// 自定义弹窗内容
  const infoWindowContent = `
          <div>
              <div style="font-size: 14px;text-align: center;background-color: #00aaa3;color: #fff;padding: 10px 0">
                  <span>${
                    Info.unitName == null ? "" : Info.unitName
                  }</span>
              </div>
              <div style="padding: 10px;font-size: 12px;background-color: #fff">
                  <div style="display: flex">
                      <div style="flex: 1;margin-bottom: 15px;">
                          门牌名称:${
                            Info.doorCard == null
                              ? ""
                              : Info.doorCard
                          }
                      </div>
                      <div style="flex: 1;margin-bottom: 15px;">
                          单位名称:${
                            Info.unitName == null
                              ? ""
                              : Info.unitName
                          }
                      </div>
                  </div>
              </div>
          </div>
          `;
  const infoWindow = new AMap.InfoWindow({
    content: infoWindowContent,
    // isCustom: true, //自定义窗体,设置为false为默认弹窗
    offset: new AMap.Pixel(115, -35), // 设置弹窗的位置
  });
  infoWindow.open(this.map, e.target.getPosition());
};

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值