Leaflet 学习心路历程之 —— 自定义 Popup 基础教学(自定义Marker标记气泡)

Leaflet 自定义 Popup  (自定义Marker标记气泡)

之前在写项目的过程中遇到一个这样的需求,找了很多文章都没有能直接解决这个问题

我们开始解决问题吧:

首先我们在地图上加载marker 这里就不过多描述了 直接进入正题  先把popup这一块的文档放在这里

Option参数参数类型默认值说明

我们要用到的就是 className 参数

  • 添加/新增 Marker
let marker = L.marker([50.5, 30.5]).addTo(map)
  • 给 Marker 添加 Popup 气泡
marker.bindPopup("我是popup").openPopup(); // openPopup 是自动打开气泡
  • 我们给气泡绑定 自定义class 并将内容填写进去
marker.bindPopup(`<span class="cd-span">发布消息</span>
                            <span class="cd-span">当前信息</span>
                            <span class="cd-span">实时状态</span>`, { className: 'mypopup' }).addTo(map)
  • 接下来设置 mypopup 改变popup位置  有一些关闭按钮,包括气泡下方的小三角指示表都可以通过css属性display:none 搞定
  /* popup弹窗位置 */

  .mypopup {
    bottom: -120px !important;
    left: -81px !important;
  }
  • 这里是我气泡摸样的css   我个人的思路是这样的 :
  1. 首先将气泡变成圆形,然后将背景色设置为透明
  2. 给圆形增加边框
  3. 文字 、分割线 都是用的定位  这里分割线我用伪类写的利用偏移将它勉强看着舒服一点(不要在意细节没有精准三等分)
 .mypopup .leaflet-popup-content-wrapper {
    width: 160px;
    height: 160px;
    border-radius: 50%;
    border: 55px solid rgb(83, 115, 145);
    background-color: rgba(0, 0, 0, 0.24);
  }

  .cd-span {
  position: absolute;
  width: 30px;
  font-size: 14px;
  text-align: center;
  line-height: 15px;
}

.cd-span:nth-child(1) {
  color: #fff;
  top: 13px;
  left: 65px;
}

.cd-span:nth-child(1):active {
  color: rgb(152, 199, 218);
}

.cd-span:nth-child(1):before {
  content: "";
  position: absolute;
  width: 40px;
  height: 1px;
  border-bottom: 1px dashed rgb(180, 193, 204);
  transform: translate3d(40px, 41px, 0px) rotate(-33deg);
}

.cd-span:nth-child(2) {
    color: #fff;
    top: 92px;
    left: 20px;
}

.cd-span:nth-child(2):active {
  color: rgb(152, 199, 218);
}

.cd-span:nth-child(2):before {
  content: "";
  position: absolute;
  width: 40px;
  height: 1px;
  border-bottom: 1px dashed rgb(180, 193, 204);
  transform: translate3d(-6px, -39px, 0px) rotate(30deg);
}

.cd-span:nth-child(3) {
    color: #fff;
    top: 92px;
    left: 110px;
}

.cd-span:nth-child(3):active {
  color: rgb(152, 199, 218);
}

.cd-span:nth-child(3):before {
  content: "";
  position: absolute;
  width: 40px;
  height: 1px;
  border-bottom: 1px dashed rgb(180, 193, 204);
  transform: translate3d(-50px, 40px, 0px) rotate(-90deg);
}

最后希望大家遇到跟我同样的问题可以少走一些弯路

  • 8
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值