leaflet 自定义图标 图标继承 (每天进步一点点)

(例子参考leaflet官方教程的)

当我们需要用到的图标,大多数样式都是类似的,例如只有颜色不同,这个时候我们可以利用L.Icon的继承功能!

定义一个继承类:

 var LeafIcon = L.Icon.extend({
      options: {
        shadowUrl: '././assets/img/leaf-shadow.png',
        iconSize: [38, 95],
        shadowSize: [50, 64],
        iconAnchor: [22, 94],
        shadowAnchor: [4, 62],
        popupAnchor: [-3, -76]
      }
    });

创建图标,给不同的属性定义值

 var greenIcon = new LeafIcon({ iconUrl: '././assets/img/leaf-green.png' });
    var redIcon = new LeafIcon({ iconUrl: '././assets/img/leaf-red.png' });
    var orangeIcon = new LeafIcon({ iconUrl: '././assets/img/leaf-orange.png' });

添加到地图中

L.marker([32, -130], { icon: greenIcon }).addTo(this.mymap).bindPopup("I am a green leaf.");
    L.marker([22, -120], { icon: redIcon }).addTo(this.mymap).bindPopup("I am a red leaf.");
    L.marker([13, -100], { icon: orangeIcon }).addTo(this.mymap).bindPopup("I am an orange leaf.");

效果如图:

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值