百度地图自定义弹窗infoBox

本文介绍了如何在HTML中引入InfoBox库,并在BMapGL地图上实现信息弹窗功能,包括初始化地图、添加标记和定制Infobox的显示内容。
摘要由CSDN通过智能技术生成

infoBox文档地址: https://api.map.baidu.com/library/InfoBox/1.2/docs/symbols/BMapLib.InfoBox.html

infobox依赖文件可以到这里下载:https://gitee.com/shiguangliangyi/dependent-file/blob/00d37b1ba2ffbbfd309305449711b4e7378878b5/infobox.js

1.引入
index.html

<script type="text/javascript" src="../js/InfoBox.min.js"></script

2.使用

   //初始化地图
    initMap() {
      let self = this;
      var map = new BMapGL.Map("map"); // 创建Map实例
      map.centerAndZoom(
        new BMapGL.Point(self.center.lng, self.center.lat),
        self.zoom
      ); // 初始化地图,设置中心点坐标和地图级别
      map.enableScrollWheelZoom(true); // 开启鼠标滚轮缩放
      map.setMapType(BMAP_NORMAL_MAP);
      self.map = map;
      window.map = map;
      self.map.addEventListener("zoomend", zoomEvent);//监听地图缩放事件
      self.map.setMapStyleV2({
        styleJson: [
          {
            featureType: "poilabel",
            elementType: "all",
            stylers: {
              visibility: "off",
            },
          },
          {
            featureType: "manmade",
            elementType: "all",
            stylers: {
              visibility: "off",
            },
          },
          {
            featureType: "building",
            elementType: "all",
            stylers: {
              visibility: "off",
            },
          },
        ],
      });
 
 
    },

	//添加图片覆盖物
    markerClick(node) {
        let point = new BMapGL.Point(node.lng, node.lat);
        var myIcon = new BMapGL.Icon(
          "/img/point.png",
          new BMapGL.Size(25, 25),
          {
            anchor: new BMapGL.Size(25, 25),
          }
        );
        // 创建标注对象并添加到地图
        var marker = new BMapGL.Marker(point, { icon: myIcon });
        this.map.addOverlay(marker);
        let obj = {
          item: item,
          lng: lng,
          lat: lat,
        };
        //覆盖物绑定点击事件,打开弹窗
        marker.addEventListener("click", (e) => {
          this.pointClick(obj);//点击事件传参,打开弹窗
        });
      
    },
    //重点 infobox的使用
   async pointClick(node) {
      //  infox弹窗
      let strHtml = `<div class="content bor1"  style="width:200px">
      <div class='infoBox_content_title'>{{title}}</div>
      <div class='infoBox_content_row'>今日上班人数:{{attNum}}</div>
      <div class='infoBox_content_row'>备注信息:{{note}}</div>
      </div>`;
      let res = await getOperPointInfo(node.item.id);//查詢事件
      strHtml = strHtml.replace("{{title}}", node.item.name);
      strHtml = strHtml.replace("{{attNum}}", res.data.attNum);
      strHtml = strHtml.replace("{{note}}", res.data.note);
      var infoBox = new BMapLib.InfoBox(this.map, strHtml, {
        closeIconMargin: "0px 10px 0 0",
        closeIconUrl: "/img/close.png",//关闭图标的图片路径
        closeIconWidth: "16px",
        offset: new BMapGL.Size(0, 30),//infobox偏移量
        enableAutoPan: true,//自动偏移
      });
      this.oldInfoBox && this.oldInfoBox.close();//如果有别的弹窗先关闭
      let point = new BMapGL.Point(node.lng, node.lat);//打开的坐标点
      infoBox.open(point);
      this.oldInfoBox=infoBox 
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
infoBox.js是一个JavaScript库,用于创建和管理信息框(InfoBox)的显示。通过引用该库,可以在网页中使用InfoBox对象来显示自定义的信息框。 在infoBox.js库中,存在一个名为getContent的方法,它返回InfoBox的内容,即信息框中显示的文本内容。这个方法的定义如下:InfoBox.prototype.getContent = function () {}。 另外,在infoBox.js库中还有一个名为getZIndex的方法,它返回InfoBox的层级索引(z-index),用于控制信息框在网页中的显示次序。这个方法的定义如下:InfoBox.prototype.getZIndex = function () {}。 通过使用这些方法和其他的功能,开发人员可以根据自己的需求定制和操作InfoBox对象,以实现信息框的具体功能和样式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [百度地图 infobox的使用](https://blog.csdn.net/zcq125521/article/details/88285421)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [google地图 自定义infowindow infobox.js 插件](https://blog.csdn.net/lsh451334122/article/details/53939780)[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^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值