Leaflet中通过leaflet-measure插件实现测距测面效果

场景

Leaflet中原生方式实现测距:

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

Leaflet中原生方式实现测量面积:

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

在上面原生方式实现测距测面的效果下,怎样通过插件的方式实现效果。

leaflet-measure插件地址:

https://github.com/ljagis/leaflet-measure

官方示例地址:

http://xiaobaibai521.gitee.io/leaflet-measure-path/example/

本地运行效果

Leaflet中通过leaflet-measure插件实现测距测面效果

 

注:

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

实现

1、按照官方示例引入所需的js、css文件等

Leaflet中通过leaflet-measure插件实现测距测面效果

 

2、新建html,参考其官方代码修改如下

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

<head>
    <meta charset="UTF-8">
    <title>leaflet-measure插件测距测面</title>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css" />
    <link rel="stylesheet" href="./css/leaflet-measure-path.css">
    <style>
        body {
            padding: 0;
            margin: 0;
            font-family: sans-serif;
            font-size: 10px;
        }
        #map {
            position: absolute;
            width: 100%;
            height: 100%;
        }
    </style>
</head>

<body>
    <div id="map"></div>
    <script src="https://unpkg.com/leaflet@1.0.1/dist/leaflet-src.js"></script>
    <script src="./js/leaflet-measure-path.js"></script>
    <script src="./js/Leaflet.Editable.js"></script>
    <script type="text/javascript">
        //实例化地图并添加osm地图
        //注意这里设置editable为true
        var map = L.map('map', {editable: true}).setView([36.09, 120.35], 13);
        L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attribution: ''
        }).addTo(map);
        //新建多边形并添加到地图
        var polygon = L.polygon([
                [36.208, 120.181],
                [36.194, 120.359],
                [36.105, 120.317],
                [36.014, 120.250],
                [36.107, 120.110]
            ], { showMeasurements: true })
            .addTo(map);
        //激活多边形的编辑功能
        polygon.enableEdit();
        map.on('editable:vertex:drag editable:vertex:deleted', polygon.updateMeasurements, polygon);


    </script>
</body>

</html>

 

上一篇:leaflet地图初始化显示不全问题


下一篇:Leaflet中使用Leaflet.contextmenu插件实现地图上添加鼠标右键菜单