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

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

导读:本篇文章讲解 Leaflet中原生方式实现测量面积,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

场景

Leaflet中原生方式实现测距:

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

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

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

注:

博客:
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>

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

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

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

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