LeaFlet学习之地图文字标注

在地图上进行一些文字的标注有些场景我们会用到,在这里我们文字标注用到了DivICon图标,通过与mark相结合,将mark的图标设置为DivICon图标,进行文字标注,放张图看看效果:

一、全部代码

<!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>

二、总结

html是标注的内容,iconSize是图标大小是个正方形概念,className这是div设置类名,进行css样式设置所用,leaflet的图标本质上就是div容器,所以会有这个属性,关于样式的设置,几乎所有的文字css样式都能设置看你需要。地图是加载的OSM

转载自:https://blog.csdn.net/weixin_40184249/article/details/81265720

You may also like...