高德地图自定义infowindow内容

这篇博客介绍了如何利用高德地图(AMap)API创建地图上的Marker,并设置其图标、标题和位置。同时,展示了如何设置Marker的点击事件,以及创建自定义的InfoWindow展示详细信息。InfoWindow内容包括顶部标题、关闭按钮以及中部和底部的自定义布局。此外,还提供了关闭InfoWindow的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


// 创建一个marker 在地图上显示
              var markerMC = new AMap.Marker({
                icon: '',//图标
                title:'',//标题
                position: ['经度','纬度'],
                map: map
              });
              markerMC.content = `自定义marker内容`;
              markerMC.on('click', markerClickMC);//点击marker点调用方法
              // markerMC.emit('click', {   : markerMC });默认点击

// 设置label
              markerMC.setLabel({
                  offset: new AMap.Pixel(20, -10),
                  content: 'label内容'
                })



// 可配合marker点击方法传入相关参数
    function markerClickMC(e) {
      var infoWindowMenci = new AMap.InfoWindow({
        isCustom: true,//启用自定义窗体
        content: InfoWindowMC(e.target.content),//调用窗体方法 传入html字符串
        offset: new AMap.Pixel(0, -30)//信息窗偏移
      });
      // 设置了infowindows content就不能显示自定义信息窗体?
      // infoWindowMenci.setContent(e.target.content);
      infoWindowMenci.open(map, e.target.getPosition());//打开信息窗
    }



//构建自定义信息窗体
function InfoWindowMC(cont) {
  // cont自定义内容 html字符串
  var info = document.createElement("div");
    info.className = "custom-info input-card content-window-card";

    //可以通过下面的方式修改自定义窗体的宽高
    //info.style.width = "400px";
    // 定义顶部标题
    var top = document.createElement("div");
    var titleD = document.createElement("div");
    var closeX = document.createElement("img");
    top.className = "info-top";
    // titleD.innerHTML = title;
    closeX.src = "https://webapi.amap.com/images/close2.gif";
    closeX.style.float='right'
    closeX.onclick = closeInfoWindow;

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

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

    // 定义底部内容
    var bottom = document.createElement("div");
    bottom.className = "info-bottom";
    bottom.style.position = 'relative';
    bottom.style.top = '0px';
    bottom.style.margin = '0 auto';
    var sharp = document.createElement("img");
    sharp.src = "";
    bottom.appendChild(sharp);
    info.appendChild(bottom);
    return info;
}

// 点击关闭信息窗体
function closeInfoWindow() {
    map.clearInfoWindow();
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值