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

场景

Leaflet中原生方式实现测距:

Leaflet中原生方式实现测距_BADAO_LIUMANG_QIZHI的博客-CSDN博客

在上面实现测量距离的基础上,实现测量面积效果如下

注:

博客:
BADAO_LIUMANG_QIZHI的博客_霸道流氓气质_CSDN博客-C#,SpringBoot,架构之路领域博主
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

完整代码:

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

<head>
    <meta charset="UTF-8">
    <title>leaflet实现测量面积</title>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
    <style>
        #mapButton {
            position: absolute;
            z-index: 10000;
        }

        html,
        body,
        #map {
            padding: 0;
            margin: 0;
            width: 100%;
            height: 100%;
            overflow: hidden;
            position: absolute;
        }
    </style>
</head>

<body>
    <div id="mapButton">
        <br><br><br><br>
        <button type="button" id="areaMeasure">测面积</button>
        <button type="button" id="clearMeasure">清除</button>
    </div>
    <div id="map">
    </div>
    <script src="http://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
    <script type="text/javascript" src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
    <script type="text/javascript">
        var map = L.map('map').setView([36.09, 120.35], 13);
        L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attribution: ''
        }).addTo(map);

        var DRAWING = false; //是否正在绘制
        var BarDRAWLAYERS = [];
        var MEASUREAREATOOLTIP; //量面提示
        var DRAWPOLYGON; //绘制的面
        var DRAWMOVEPOLYGON; //绘制过程中的面
        var DRAWPOLYGONPOINTS = []; //绘制的面的节点集

        var MEASURERESULT = 0; //测量结果

        //测量面积按钮点击事件
        $('#areaMeasure').click(function () {
            //开始绘制多边形
            startDrawPolygon();
        });

        /*多边形*/
        function startDrawPolygon(func) {
            MEASURERESULT = 0;
            map.getContainer().style.cursor = 'crosshair';
            //地图添加鼠标按下事件
            map.on('mousedown', function (e) {
                DRAWING = true;
                DRAWPOLYGONPOINTS.push(e.latlng);
                DRAWPOLYGON.addLatLng(e.latlng);
            });
            //地图添加鼠标移动事件
            map.on('mousemove', function (e) {
                if (DRAWING) {
                    //清除上次数据
                    if (DRAWMOVEPOLYGON != undefined && DRAWMOVEPOLYGON != null) {
                        map.removeLayer(DRAWMOVEPOLYGON);
                    }
                    var prevPoint = DRAWPOLYGONPOINTS[DRAWPOLYGONPOINTS.length - 1];
                    var firstPoint = DRAWPOLYGONPOINTS[0];
                    DRAWMOVEPOLYGON = new L.Polygon([firstPoint, prevPoint, e.latlng], shapeOptions);
                    map.addLayer(DRAWMOVEPOLYGON);
                }
            });
            
            //地图添加鼠标双击事件
            map.on('dblclick', function (e) {
                map.getContainer().style.cursor = '';
                var tempPoints = [];
                for (var i = 0; i < DRAWPOLYGONPOINTS.length; i++) {
                    tempPoints.push(DRAWPOLYGONPOINTS[i]);
                }
                tempPoints.push(e.latlng);
                //计算面积
                var distance = CalArea(tempPoints);
                //声明标记
                marker = new L.Marker(e.latlng, {
                    draggable: false
                });
                //地图上添加标记
                map.addLayer(marker);
                //标记弹窗显示面积
                marker.bindPopup("总面积:" + (distance / 1000000).toFixed(3) + '平方公里').openPopup();
                if (DRAWING) {
                    if (DRAWMOVEPOLYGON != undefined && DRAWMOVEPOLYGON != null) {
                        map.removeLayer(DRAWMOVEPOLYGON);
                        DRAWMOVEPOLYGON = null;
                    }
                    BarDRAWLAYERS.push(DRAWPOLYGON);
                    if (func) {
                        func(DRAWPOLYGONPOINTS);
                    }
                    DRAWPOLYGONPOINTS = [];
                    DRAWING = false;
                    //地图移除事件
                    map.off('mousedown');
                    map.off('mousemove');
                    map.off('dblclick');
                }
            });

            var shapeOptions = {
                    color: '#F54124',
                    weight: 3,
                    opacity: 0.8,
                    fill: true,
                    fillColor: null,
                    fillOpacity: 0.2,
                    clickable: true
                },

            DRAWPOLYGON = new L.Polygon([], shapeOptions);
            map.addLayer(DRAWPOLYGON);

            //计算面积
            function CalArea(latLngs) {
                var pointsCount = latLngs.length,
                    area = 0.0,
                    d2r = Math.PI / 180,
                    p1, p2;
                if (pointsCount > 2) {
                    for (var i = 0; i < pointsCount; i++) {
                        p1 = latLngs[i];
                        p2 = latLngs[(i + 1) % pointsCount];
                        area += ((p2.lng - p1.lng) * d2r) *
                            (2 + Math.sin(p1.lat * d2r) + Math.sin(p2.lat * d2r));
                    }
                    area = area * 6378137.0 * 6378137.0 / 2.0;
                }
                return Math.abs(area);
            }
        }

        //清除按钮点击事件
        $('#clearMeasure').click(function () {
            qingchu()
        })

        //执行清除方法
        function qingchu(func) {
            for (var i = 0; i < BarDRAWLAYERS.length; i++) {
                //移除图层
                map.removeLayer(BarDRAWLAYERS[i]);
            }
            //清空数组
            BarDRAWLAYERS = [];
            if (marker) {
                map.removeLayer(marker)
            }
        }
    </script>
</body>

</html>

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值