当前位置:   article > 正文

Leaflet中自定义marker的icon图标_leaflet自定义marker图标

leaflet自定义marker图标

场景

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,可以重新设置属性

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

3、创建多个icon

  1.         //创建多个icon
  2.         var greenIcon = new LeafIcon({
  3.                 iconUrl: './icon/leaf-green.png'
  4.             }),
  5.             redIcon = new LeafIcon({
  6.                 iconUrl: './icon/leaf-red.png'
  7.             }),
  8.             orangeIcon = new LeafIcon({
  9.                 iconUrl: './icon/leaf-orange.png'
  10.             });

4、创建marker添加到地图上

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

5、完整代码

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>leaflet自定义标记图标</title>
  6.     <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
  7.     <style>
  8.         html,
  9.         body,
  10.         #map {
  11.             padding: 0;
  12.             margin: 0;
  13.             width: 100%;
  14.             height: 100%;
  15.             overflow: hidden;
  16.         }
  17.     </style>
  18. </head>
  19. <body>
  20.     <div id="map"></div>
  21.     <script type="text/javascript" src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
  22.     <script type="text/javascript">
  23.         var map = L.map('map').setView([36.09, 120.35], 13);
  24.         L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  25.             attribution: '&copy; <a href="OpenStreetMaphttps://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
  26.         }).addTo(map);
  27.         //创建原始图标ICON
  28.         //创建ICON构造函数,相当于继承于ICON,可以重新设置属性
  29.         var LeafIcon = L.Icon.extend({
  30.             options: {
  31.                 shadowUrl: './icon/leaf-shadow.png', //阴影地址
  32.                 iconSize: [38, 95],  //图标宽高
  33.                 shadowSize: [50, 64], //阴影宽高
  34.                 iconAnchor: [22, 94], //图标锚点
  35.                 shadowAnchor: [4, 62], //阴影锚点
  36.                 popupAnchor: [-3, -76] //弹出框弹出位置,相对于图标锚点
  37.             }
  38.         });
  39.         //创建多个icon
  40.         var greenIcon = new LeafIcon({
  41.                 iconUrl: './icon/leaf-green.png'
  42.             }),
  43.             redIcon = new LeafIcon({
  44.                 iconUrl: './icon/leaf-red.png'
  45.             }),
  46.             orangeIcon = new LeafIcon({
  47.                 iconUrl: './icon/leaf-orange.png'
  48.             });
  49.         //创建marker添加到地图上
  50.         L.marker([36.09, 120.35], {
  51.             icon: greenIcon
  52.         }).bindPopup("I am a green leaf.").addTo(map);
  53.         L.marker([36.13, 120.25], {
  54.             icon: redIcon
  55.         }).bindPopup("I am a red leaf.").addTo(map);
  56.         L.marker([36.16, 120.15], {
  57.             icon: orangeIcon
  58.         }).bindPopup("I am an orange leaf.").addTo(map);
  59.     </script>
  60. </body>
  61. </html>

6、效果

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/234853
推荐阅读
相关标签
  

闽ICP备14008679号