在地图上进行一些文字的标注有些场景咱们会用到,在这里咱们文字标注用到了DivICon图标,经过与mark相结合,将mark的图标设置为DivICon图标,进行文字标注,放张图看看效果:javascript
1、所有代码css
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>文字标注</title> <link href="Script/leaflet/leaflet.css" rel="stylesheet" /> <style> #map { width:1000px; height:1000px; } .my-div-icon{ font-size:15px; /*background:red;*/ /*width:5px;*/ color:red; } </style> <script src="Script/leaflet/leaflet.js"></script> </head> <body> <div id="map"></div> <script type="text/javascript"> var map = new L.Map('map'); var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'; var osm = new L.TileLayer(osmUrl, { minZoom: 1, maxZoom: 18 }); map.addLayer(osm); map.setView(new L.LatLng(31.864942016, 117.2882028929), 11); var myIcon = L.divIcon({ html: "狗子", className: 'my-div-icon', iconSize:30 }); L.marker([31.864942016,117.2882028929], { icon: myIcon }).addTo(map); </script> </body> </html>
2、总结html
html是标注的内容,iconSize是图标大小是个正方形概念,className这是div设置类名,进行css样式设置所用,leaflet的图标本质上就是div容器,因此会有这个属性,关于样式的设置,几乎全部的文字css样式都能设置看你须要。地图是加载的OSMjava