Leaflet中自定义marker的icon图标

场景

Leaflet中添加标记、折线、圆圈、多边形、弹窗显示点击处坐标:

Leaflet中添加标记、折线、圆圈、多边形、弹窗显示点击处坐标_BADAO_LIUMANG_QIZHI的博客-CSDN博客

在上面加载标记使用的是默认图标,如果要自定义图标怎么用。

官网有详细的教程

Markers With Custom Icons - Leaflet - a JavaScript library for interactive maps

注:

博客:
BADAO_LIUMANG_QIZHI的博客_霸道流氓气质_CSDN博客-C#,SpringBoot,架构之路领域博主
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

1、与官方教程一样,准备一张阴影照片,三张不同颜色的icon

2、创建ICON构造函数,相当于继承ICON,可以重新设置属性

        //创建ICON构造函数,相当于继承于ICON,可以重新设置属性
        var LeafIcon = L.Icon.extend({
            options: {
                shadowUrl: './icon/leaf-shadow.png', //阴影地址
                iconSize: [38, 95],  //图标宽高
                shadowSize: [50, 64], //阴影宽高
                iconAnchor: [22, 94], //图标锚点
                shadowAnchor: [4, 62], //阴影锚点
                popupAnchor: [-3, -76] //弹出框弹出位置,相对于图标锚点
            }
        });

3、创建多个icon

        //创建多个icon
        var greenIcon = new LeafIcon({
                iconUrl: './icon/leaf-green.png'
            }),
            redIcon = new LeafIcon({
                iconUrl: './icon/leaf-red.png'
            }),
            orangeIcon = new LeafIcon({
                iconUrl: './icon/leaf-orange.png'
            });

4、创建marker添加到地图上

        //创建marker添加到地图上
        L.marker([36.09, 120.35], {
            icon: greenIcon
        }).bindPopup("I am a green leaf.").addTo(map);
        L.marker([36.13, 120.25], {
            icon: redIcon
        }).bindPopup("I am a red leaf.").addTo(map);
        L.marker([36.16, 120.15], {
            icon: orangeIcon
        }).bindPopup("I am an orange leaf.").addTo(map);

5、完整代码

​
<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>leaflet自定义标记图标</title>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
    <style>
        html,
        body,
        #map {
            padding: 0;
            margin: 0;
            width: 100%;
            height: 100%;
            overflow: hidden;
        }
    </style>
</head>

<body>
    <div id="map"></div>
    <script type="text/javascript" src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
    <script type="text/javascript">
        var map = L.map('map').setView([36.09, 120.35], 13);
        L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attribution: '&copy; <a href="OpenStreetMaphttps://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
        }).addTo(map);

        //创建原始图标ICON
        //创建ICON构造函数,相当于继承于ICON,可以重新设置属性
        var LeafIcon = L.Icon.extend({
            options: {
                shadowUrl: './icon/leaf-shadow.png', //阴影地址
                iconSize: [38, 95],  //图标宽高
                shadowSize: [50, 64], //阴影宽高
                iconAnchor: [22, 94], //图标锚点
                shadowAnchor: [4, 62], //阴影锚点
                popupAnchor: [-3, -76] //弹出框弹出位置,相对于图标锚点
            }
        });

        //创建多个icon
        var greenIcon = new LeafIcon({
                iconUrl: './icon/leaf-green.png'
            }),
            redIcon = new LeafIcon({
                iconUrl: './icon/leaf-red.png'
            }),
            orangeIcon = new LeafIcon({
                iconUrl: './icon/leaf-orange.png'
            });

        //创建marker添加到地图上
        L.marker([36.09, 120.35], {
            icon: greenIcon
        }).bindPopup("I am a green leaf.").addTo(map);
        L.marker([36.13, 120.25], {
            icon: redIcon
        }).bindPopup("I am a red leaf.").addTo(map);
        L.marker([36.16, 120.15], {
            icon: orangeIcon
        }).bindPopup("I am an orange leaf.").addTo(map);
    </script>
</body>

</html>

​

6、效果

 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值