Leaflet中自定义marker的icon图标

生活中,最使人疲惫的往往不是道路的遥远,而是心中的郁闷;最使人痛苦的往往不是生活的不幸,而是希望的破灭;最使人颓废的往往不是前途的坎坷,而是自信的丧失;最使人绝望的往往不是挫折的打击,而是心灵的死亡。所以我们要有自己的梦想,让梦想的星光指引着我们走出落漠,走出惆怅,带着我们走进自己的理想。

导读:本篇文章讲解 Leaflet中自定义marker的icon图标,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

场景

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

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

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

官网有详细的教程

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

Leaflet中自定义marker的icon图标

注:

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

实现

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

Leaflet中自定义marker的icon图标Leaflet中自定义marker的icon图标Leaflet中自定义marker的icon图标Leaflet中自定义marker的icon图标Leaflet中自定义marker的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、效果

Leaflet中自定义marker的icon图标

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/136120.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!