场景
Leaflet中原生方式实现测距:
Leaflet中原生方式实现测距_BADAO_LIUMANG_QIZHI的博客-CSDN博客
Leaflet中原生方式实现测量面积:
Leaflet中原生方式实现测量面积_BADAO_LIUMANG_QIZHI的博客-CSDN博客
在上面原生方式实现测距测面的效果下,怎样通过插件的方式实现效果。
leaflet-measure插件地址:
GitHub - ljagis/leaflet-measure: Coordinate, linear, and area measure control for Leaflet maps
官方示例地址:
Leaflet Measure Path - example
本地运行效果
注:
博客:
BADAO_LIUMANG_QIZHI的博客_霸道流氓气质_CSDN博客-C#,SpringBoot,架构之路领域博主
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
1、按照官方示例引入所需的js、css文件等
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>