关键词搜索

源码搜索 ×
×

Leaflet文档阅读笔记-Markers With Custom Icons笔记

发布2019-09-09浏览4618次

详情内容

目录

 

官方解析

博主例子


 

官方解析

这个内容主要是把自定义图片,放到地图上,并且还可以增加阴影图片,以及点击图片后的弹出事件。

官方是准备了下面这4张图片:

使用下面这种方式可以单独设置,自定义图标

  1. var greenIcon = L.icon({
  2. iconUrl: 'leaf-green.png',
  3. shadowUrl: 'leaf-shadow.png',
  4. iconSize: [38, 95], // size of the icon
  5. shadowSize: [50, 64], // size of the shadow
  6. iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
  7. shadowAnchor: [4, 62], // the same for the shadow
  8. popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor
  9. });

随后增加到地图上:

L.marker([51.5, -0.09], {icon: greenIcon}).addTo(map);

运行截图如下:

在后面本人的例子中!

这里目前暂时把iconAnchor设置为[图片的宽度https://cdn.jxasp.com:9143/image/2,图片的高度]

下面是定义一个类,然后简单调用的例子:

  1. var LeafIcon = L.Icon.extend({
  2. options: {
  3. shadowUrl: 'leaf-shadow.png',
  4. iconSize: [38, 95],
  5. shadowSize: [50, 64],
  6. iconAnchor: [22, 94],
  7. shadowAnchor: [4, 62],
  8. popupAnchor: [-3, -76]
  9. }
  10. });

简单进行调用

  1. var greenIcon = new LeafIcon({iconUrl: 'leaf-green.png'}),
  2. redIcon = new LeafIcon({iconUrl: 'leaf-red.png'}),
  3. orangeIcon = new LeafIcon({iconUrl: 'leaf-orange.png'});

添加到地图上并且弹窗

  1. L.marker([51.5, -0.09], {icon: greenIcon}).addTo(map).bindPopup("I am a green leaf.");
  2. L.marker([51.495, -0.083], {icon: redIcon}).addTo(map).bindPopup("I am a red leaf.");
  3. L.marker([51.49, -0.1], {icon: orangeIcon}).addTo(map).bindPopup("I am an orange leaf.");

最后帖下官方全部代码!

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Custom Icons Tutorial - Leaflet</title>
  5. <meta charset="utf-8" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
  8. <link rel="stylesheet" href="https://unpkg.com/leaflet@1.5.1/dist/leaflet.css" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin=""/>
  9. <script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js" integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og==" crossorigin=""></script>
  10. <style>
  11. html, body {
  12. height: 100%;
  13. margin: 0;
  14. }
  15. #map {
  16. width: 600px;
  17. height: 400px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id='map'></div>
  23. <script>
  24. var map = L.map('map').setView([51.5, -0.09], 13);
  25. L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  26. attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
  27. }).addTo(map);
  28. var LeafIcon = L.Icon.extend({
  29. options: {
  30. shadowUrl: 'leaf-shadow.png',
  31. iconSize: [38, 95],
  32. shadowSize: [50, 64],
  33. iconAnchor: [22, 94],
  34. shadowAnchor: [4, 62],
  35. popupAnchor: [-3, -76]
  36. }
  37. });
  38. var greenIcon = new LeafIcon({iconUrl: 'leaf-green.png'}),
  39. redIcon = new LeafIcon({iconUrl: 'leaf-red.png'}),
  40. orangeIcon = new LeafIcon({iconUrl: 'leaf-orange.png'});
  41. L.marker([51.5, -0.09], {icon: greenIcon}).bindPopup("I am a green leaf.").addTo(map);
  42. L.marker([51.495, -0.083], {icon: redIcon}).bindPopup("I am a red leaf.").addTo(map);
  43. L.marker([51.49, -0.1], {icon: orangeIcon}).bindPopup("I am an orange leaf.").addTo(map);
  44. </script>
  45. </body>
  46. </html>

 

博主例子

这里采用本地的GIS服务,搭建方式看此博文,在此不再赘述

https://blog.csdn.net/qq78442761/article/details/100581622

程序运行截图如下:

并且还增加了弹窗响应

函数和理论都是上面官方的,在此不多说,直接上代码!

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Hello Test4</title>
  5. <meta charset="utf-8" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
  8. <link rel="stylesheet" href="leaflet.css" />
  9. <script src="leaflet.js"></script>
  10. <script src="leaflet-tilelayer-wmts-src.js"></script>
  11. <script src="echarts.js"></script>
  12. <style>
  13. html, body {
  14. height: 100%;
  15. margin: 0;
  16. }
  17. #map {
  18. width: 100%;
  19. height: 100%;
  20. }
  21. .chart{
  22. width: 600px;
  23. height: 300px;
  24. background-color: #fff;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id='map'></div>
  30. <script type="text/javascript">
  31. var ign = new L.TileLayer.WMTS( "http://XXX,XXX,XXX,XXX:8080/geoserver/gwc/service/wmts" ,
  32. {
  33. layer: 'GG_9:gg_9',
  34. tilematrixset: "EPSG:900913",
  35. Format : 'image/png',
  36. TileMatrix: 'EPSG:900913:8'
  37. }
  38. );
  39. var map = L.map('map', {
  40. minZoom: 6,
  41. maxZoom: 7
  42. }).setView([32, 118], 7);
  43. L.control.scale({'position':'bottomleft','metric':true,'imperial':false}).addTo(map);
  44. map.addLayer(ign);
  45. map.invalidateSize(true);
  46. var popup = L.popup();
  47. function onMapClick(e) {
  48. popup
  49. .setLatLng(e.latlng)
  50. .setContent("You clicked the map at " + e.latlng.toString())
  51. .openOn(map);
  52. }
  53. map.on('click', onMapClick);
  54. //新加的代码
  55. var marker1 = L.icon({
  56. iconUrl: "img/marker1.png",
  57. iconSize: [128, 128],
  58. iconAnchor: [64, 128]
  59. });
  60. L.marker([32.3, 118.8], {icon: marker1}).addTo(map).bindPopup("南京的BOSS");
  61. var marker2 = L.icon({
  62. iconUrl: "img/marker2.png",
  63. iconSize: [128, 128],
  64. iconAnchor: [64, 128]
  65. });
  66. L.marker([31.4, 121.4], {icon: marker2}).addTo(map).bindPopup("上海的BOSS");
  67. var marker3 = L.icon({
  68. iconUrl: "img/marker3.png",
  69. iconSize: [128, 128],
  70. iconAnchor: [64, 128]
  71. });
  72. L.marker([34.7, 119.2], {icon: marker3}).addTo(map).bindPopup("连云港BOSS");
  73. //新添加法
  74. var OtherIcon = L.Icon.extend({
  75. options:{
  76. iconSize: [128, 128],
  77. iconAnchor: [64, 128]
  78. }
  79. });
  80. var marker4 = new OtherIcon({iconUrl: 'img/marker4.png'});
  81. var marker5 = new OtherIcon({iconUrl: 'img/marker5.png'});
  82. L.marker([34.9, 113.6], {icon: marker4}).addTo(map).bindPopup("郑州BOSS");
  83. L.marker([30.8, 114.3], {icon: marker5}).addTo(map).bindPopup("武汉BOSS");
  84. //新加的代码
  85. </script>
  86. </body>
  87. </html>

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载