百度地图多边形范围绘制

版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!


恰饭广告




  注意:

1.百度地图ak换成自己在百度地图官网申请

2.建议使用firefox浏览器,google被墙无法调用gps

3.发布到服务器必须使用https,对于localhost和本地测试不用

4.电脑开启位置服务,手机打开gps定位

5.百度地图坐标和gps坐标不一致,要在百度地图正确显示位置必须把gps坐标转为百度坐标 

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body,
        html {
            width: 100%;
            height: 100%;
            margin: 0;
            font-family: "微软雅黑";
        }

        #result {
            width: 100%;
            font-size: 12px;
        }

        dl,
        dt,
        dd,
        ul,
        li {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        p {
            font-size: 12px;
        }

        dt {
            font-size: 14px;
            font-family: "微软雅黑";
            font-weight: bold;
            border-bottom: 1px dotted #000;
            padding: 5px 0 5px 5px;
            margin: 5px 0;
        }

        dd {
            padding: 5px 0 0 5px;
        }

        li {
            line-height: 28px;
        }
    </style>
    <script type="text/javascript"
        src="https://api.map.baidu.com/api?v=2.0&ak=自己申请"></script>
    <!--加载鼠标绘制工具-->
    <script type="text/javascript"
        src="https://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>
    <link rel="stylesheet" href="https://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css" />

</head>

<body>
    <div>多边形绘制</div>
    <div>双击鼠标完成绘制</div>
    <button onclick="getPath()">获取范围坐标</button>
    <div id="map" style="height:100%;-webkit-transition: all 0.5s ease-in-out;transition: all 0.5s ease-in-out;"></div>
    <script type="text/javascript">

        function getPath(){
            alert(mypath);
        }

        // 百度地图API功能
        var map = new BMap.Map('map');
        var poi = new BMap.Point(103.01145366580414, 25.244087791047527); //位置
        map.centerAndZoom(poi, 10);//设置中心点坐标和地图级别
        map.enableScrollWheelZoom(); //启用鼠标滚动对地图放大缩小

        //鼠标绘制完成回调方法   获取各个点的经纬度
        var overlays = [];
        var mypath="";
        var overlaycomplete = function (e) {
            overlays.push(e.overlay);
            var path = e.overlay.getPath();//Array<Point> 返回多边型的点数组
            mypath=JSON.stringify(path);
            console.log(mypath);
        };
        var styleOptions = {
            strokeColor: "red",    //边线颜色。
            fillColor: "red",      //填充颜色。当参数为空时,圆形将没有填充效果。
            strokeWeight: 3,       //边线的宽度,以像素为单位。
            strokeOpacity: 0.8,       //边线透明度,取值范围0 - 1。
            fillOpacity: 0.6,      //填充的透明度,取值范围0 - 1。
            strokeStyle: 'solid' //边线的样式,solid或dashed。
        }
        //实例化鼠标绘制工具
        var drawingManager = new BMapLib.DrawingManager(map, {
            isOpen: false, //是否开启绘制模式
            enableDrawingTool: true, //是否显示工具栏
            drawingMode: BMAP_DRAWING_POLYGON,//绘制模式  多边形
            drawingToolOptions: {
                anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
                offset: new BMap.Size(5, 5), //偏离值
                drawingModes: [
                    BMAP_DRAWING_POLYGON
                ]
            },
            polygonOptions: styleOptions //多边形的样式
        });

        //添加鼠标绘制工具监听事件,用于获取绘制结果
        drawingManager.addEventListener('overlaycomplete', overlaycomplete);
        function clearAll() {
            for (var i = 0; i < overlays.length; i++) {
                map.removeOverlay(overlays[i]);
            }
            overlays.length = 0
        }
    </script>
</body>

</html>

原文链接:https://www.idaobin.com/archives/2270.html

让我恰个饭吧.ヘ( ̄ω ̄ヘ)

支付宝 ——————- 微信
图片加载中图片加载中



恰饭广告

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

+ 30 = 36