Leaflet中使用Leaflet.draw插件实现图形交互绘制和编辑(修改图形坐标点)

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

导读:本篇文章讲解 Leaflet中使用Leaflet.draw插件实现图形交互绘制和编辑(修改图形坐标点),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

场景

Leaflet中使用Leaflet.Pin插件实现图层要素编辑效果:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/122466785

上面也使用了draw插件实现要素编辑,这里进行补充。

插件地址:

https://github.com/Leaflet/Leaflet.draw

下载插件,引入draw插件所需的js、css和image

​
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
    <link rel="stylesheet" href="./css/leaflet.draw.css" />

    <script type="text/javascript" src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
    <script type="text/javascript" src="./js/leaflet.draw.js"></script>

​

Leaflet中使用Leaflet.draw插件实现图形交互绘制和编辑(修改图形坐标点)

 

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

1、修改css中图片的路径

Leaflet中使用Leaflet.draw插件实现图形交互绘制和编辑(修改图形坐标点)

2、添加绘制图层与绘制控件

        //添加绘制图层
        var drawnItems = new L.FeatureGroup();
        map.addLayer(drawnItems);
        //添加绘制控件
        var drawControl = new L.Control.Draw({
            draw:{
                //绘制线
                polyline:true,
                //绘制多边形
                polygon:true,
                //绘制矩形
                rectangle:true,
                //绘制圆
                circle:true,
                //绘制标注
                marker:true,
                //绘制圆形标注
                circlemarker:true
            },
            edit:{
                //绘制图层
                featureGroup:drawnItems,
                //图形编辑控件
                edit:true,
                //图形删除控件
                remove:true,
            }
        });

3、添加绘制结束事件

        //绘制事件
        map.on(L.Draw.Event.CREATED, function(e){
           
            var type = e.layerType,
            //获取绘制图层
            drawlayer = e.layer;
            if(type === 'marker'){
                drawlayer.bindPopup('A popup!');
            }
            drawnItems.addLayer(drawlayer);
        });

4、完整代码

​
<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>leaflet-draw修改图形坐标点</title>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
    <link rel="stylesheet" href="./css/leaflet.draw.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" src="./js/leaflet.draw.js"></script>
    <script type="text/javascript">

        var map = L.map('map').setView([36.09, 120.35], 13);
        var tiles =
            L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                attribution: ''
            });
        tiles.addTo(map);

        //添加绘制图层
        var drawnItems = new L.FeatureGroup();
        map.addLayer(drawnItems);
        //添加绘制控件
        var drawControl = new L.Control.Draw({
            draw:{
                //绘制线
                polyline:true,
                //绘制多边形
                polygon:true,
                //绘制矩形
                rectangle:true,
                //绘制圆
                circle:true,
                //绘制标注
                marker:true,
                //绘制圆形标注
                circlemarker:true
            },
            edit:{
                //绘制图层
                featureGroup:drawnItems,
                //图形编辑控件
                edit:true,
                //图形删除控件
                remove:true,
            }
        });
       
        //添加绘制控件
        map.addControl(drawControl);
        //绘制事件
        map.on(L.Draw.Event.CREATED, function(e){
           
            var type = e.layerType,
            //获取绘制图层
            drawlayer = e.layer;
            if(type === 'marker'){
                drawlayer.bindPopup('A popup!');
            }
            drawnItems.addLayer(drawlayer);
        });
    </script>
</body>

</html>

​

5、效果

Leaflet中使用Leaflet.draw插件实现图形交互绘制和编辑(修改图形坐标点)

 

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

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

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

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