<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath() + '/'} ">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <style type="text/css">
        .anchorBL{display:none} /** 去除百度地图Logo */
        body, html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";}
        #allmap {width: 100%; height:600px; overflow: hidden;}
        #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;
        }
        .selectColor{
            width:95%;
            float:left;
            cursor:pointer;
        }
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3&key=G0k574UiHH2TTg6iiAyHZk7FEXcnpIhS"></script>
    <!--加载鼠标绘制工具-->
    <script type="text/javascript" src="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>
    <link rel="stylesheet" href="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css" />
    <!--加载检索信息窗口-->
    <script type="text/javascript" src="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.js"></script>
    <link rel="stylesheet" href="http://api.map.baidu.com/library/SearchInfoWindow/1.4/src/SearchInfoWindow_min.css" />
    <link rel="stylesheet" href="assets/fonts/font-awesome/css/font-awesome.css"/>
    <link rel="stylesheet" href="assets/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="assets/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" type="text/css" href="assets/js/vendor/viewer/viewer.min.css">
    <link href="assets/js/vendor/json-viewer/jquery.json-viewer.css" type="text/css" rel="stylesheet" />
    <title>百度地图API功能演示</title>
</head>
<body>
<div id="allmap" style="overflow:hidden;zoom:1;position:relative;">
    <div id="map" style="height:100%;-webkit-transition: all 0.5s ease-in-out;transition: all 0.5s ease-in-out;">
    </div>
</div>
<div id="result">
<!--    <input type="button" value="获取绘制的覆盖物个数" onclick="alert(overlays.length)"/>-->
    <button type="button" class="layui-btn" id="open" >开始画区域</button>
    <button type="button" class="layui-btn" id="close" >停止画区域</button>
    <button type="button" class="layui-btn" id="clearAll" >清除所有区域</button>
    <button type="button" class="layui-btn" id="clearBack" >清除上一个区域</button>
<!--    <input type="button"  value="获取多边形的顶点" onclick="getPoint()"/><br/>-->
<!--    <input type="button"  value="开启线、面编辑功能" onclick="Editing('enable')"/>-->
<!--    <input type="button"  value="关闭线、面编辑功能" onclick="Editing('disable')"/>-->
<!--    <input type="button"  value="显示原有多边形" onclick="showPolygon(this)" /><br/>-->

<!--    <input type="button"  value="画多边形" id="drawDuobian" onclick="draw(BMAP_DRAWING_POLYGON)" />-->
<!--    <input type="button"  value="画矩形" onclick="draw(BMAP_DRAWING_RECTANGLE)" />-->
<!--    <input type="button"  value="画线" onclick="draw(BMAP_DRAWING_POLYLINE)" />-->
    <button type="button" class="layui-btn" id="downJson" >导出Json数据</button>

    <!-- <input type="button"  value="画点" onclick="draw(BMAP_DRAWING_MARKER)" />-->
    <span style="font-size: 20px;">右键获取任意点的经纬度 , 双击左键停止多边形绘制</span>

    <div  id="subForm" class="layui-card-body" style="padding: 15px; display: none">
        <form class="layui-form layui-form-pane" style="padding: 15px; ">
            <div class="layui-form-item">
                <label class="layui-form-label">id</label>
                <div class="layui-input-block">
                    <input type="text" name="id" id="id" placeholder="请输入区域ID"  class="layui-input" >
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">title</label>
                <div class="layui-input-block">
                    <input type="text" name="title" id="title" placeholder="请输入区域标题" class="layui-input" >
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">color</label>
                <div class="layui-input-block">
                    <input type="text" id="color" name="color" style="width:90%; float:left;cursor:pointer;" readonly class="layui-input dataInputRowFilter selectColor" value="#000" placeholder="请选择颜色" >
                    <div style="height:38px;width:10%;float:left;"></div>
                </div>
            </div>
            <div id="test-all" style="padding-left:200px;padding-top: 5px;display: none;"></div>
        </form>
    </div>
    <pre id="json-renderer" style=""></pre>
</div>
<div id="resultShape"></div>
<script src="assets/js/vendor/wangEditor/wangEditor.min.js"></script>
<script src="assets/js/vendor/ckplayer/ckplayer/ckplayer.js"></script>
<script src="assets/js/vendor/viewer/viewer.min.js"></script>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script src="assets/js/vendor/json-viewer/jquery-1.11.0.min.js"></script>
<script src="assets/js/vendor/json-viewer/jquery.json-viewer.js"></script>
<script>
    layui.config({
        base: 'assets/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate', 'laytpl', 'colorpicker'], function(){
        var $ = layui.$;
        var form = layui.form;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var colorpicker = layui.colorpicker;
        var wangEditor = window.wangEditor;
        var wangEditorObj = {};
        var viewerObj = {};
        var $win = $(window);

        $("#allmap").height($win.height() - 90);

        $(document).on('click', '.selectColor', function() {
            selectColor(this, $(this).val())
        });

        function selectColor(ele, color) {
            //开启全功能
            colorpicker.render({
                elem: '#test-all'
                ,color: color
                ,format: 'rgb'
                ,predefine: true
                ,alpha: true
                ,shadeClose: false
                ,done: function(color){
                    $(ele).val(color);
                    $(ele).next().css("background",color);
                }
                ,change: function(color){
                    $(ele).val(color);
                    $(ele).next().css("background",color);
                }
            });
            $('.layui-colorpicker').click();
        }

        $('#open').on('click', function() {
            drawingManager.open();
            drawingManager.setDrawingMode(BMAP_DRAWING_POLYGON);
        });
        $('#close').on('click', function() {
            drawingManager.close();
        });

        $('.close').on('click', function() {
            closeBox();
        });

        function $(id){
            return document.getElementById(id);
        }

        // 百度地图API功能
        var map = new BMap.Map('map');
        var poi = new BMap.Point(111.773119,40.854725);
        map.centerAndZoom(poi, 16);
        map.enableScrollWheelZoom();
        var overlays = [];
        var overlaycomplete = function(e){
            /**
             * 表单输入
             */
            layer.open({
                type: 1,
                title: '请填写参数',
                shadeClose: false,
                shade: 0.8,
                btn:['确定','撤销'],
                area: ['50%', '90%'],
                yes:function(index,layero) {
                    $(e.overlay.domElement).attr("fill", $("#color").val());   //填充颜色
                    $(e.overlay.domElement).attr("stroke", $("#color").val()); //边线颜色
                    overlays.push({overlay:e.overlay, title:$("#title").val(),id:$("#id").val(),color:$("#color").val()});
                    $("#title").val("");
                    $("#id").val("");
                    $("#color").val("");
                    layer.close(index);
                },
                btn2:function() {
                    map.removeOverlay(e.overlay);
                },
                shadeClose: false,
                content: $("#subForm"),
                end:function(index){
                }
            });
        };

        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, //是否显示工具栏
            drawingToolOptions: {
                anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
                offset: new BMap.Size(5, 5), //偏离值
            },
            circleOptions: styleOptions, //圆的样式
            polylineOptions: styleOptions, //线的样式
            polygonOptions: styleOptions, //多边形的样式
            rectangleOptions: styleOptions //矩形的样式
        });

        //添加鼠标绘制工具监听事件，用于获取绘制结果
        drawingManager.addEventListener('overlaycomplete', overlaycomplete);

        map.addEventListener("rightclick",function(e){
            if(confirm(e.point.lng + "," + e.point.lat)){
                $("shape").innerHTML=$("shape").innerHTML+" <br/>("+e.point.lng+","+e.point.lat+")";
            }
        });

        $(document).on("click",'#clearAll',function() {
            for(var i = 0; i < overlays.length; i++){
                map.removeOverlay(overlays[i].overlay);
            }
            overlays.length = 0
            $('#json-renderer').text("");
        });

        $(document).on("click",'#clearBack',function() {
            map.removeOverlay(overlays[overlays.length-1].overlay);
            overlays.length = overlays.length - 1;
            var jsonData = new Array();
            $("resultShape").innerHTML='';
            for(var i = 0; i < overlays.length; i++){
                var overlay=overlays[i].overlay.getPath();
                var pointArry = new Array();
                for(var j = 0; j < overlay.length; j++){
                    var grid =overlay[j];
                    pointArry.push({
                        lng: grid.lng,
                        lat: grid.lat
                    });
                }
                jsonData.push({
                    title:overlays[i].title,
                    id:overlays[i].id,
                    areas:pointArry,
                    color:overlays[i].color
                })
            }
            try {
                var options = {
                    collapsed: false,
                    withQuotes: true
                };
                $('#json-renderer').jsonViewer(jsonData, options);
            } catch (error) {
            }
        });


        function getPoint(){
            $("resultShape").innerHTML='';
            for(var i = 0; i < overlays.length; i++){
                var overlay=overlays[i].overlay.getPath();
                $("resultShape").innerHTML=$("resultShape").innerHTML+overlay.length+'边形:<br/>';
                for(var j = 0; j < overlay.length; j++){
                    var grid =overlay[j];
                    $("resultShape").innerHTML=$("resultShape").innerHTML+(j+1)+"个点:("+grid.lng+","+grid.lat+");<br/>";
                }
            }
        }

        function getJson(){
            var jsonData = new Array();
            $("resultShape").innerHTML='';
            for(var i = 0; i < overlays.length; i++){
                var overlay=overlays[i].overlay.getPath();
                var pointArry = new Array();
                for(var j = 0; j < overlay.length; j++){
                    var grid =overlay[j];
                    pointArry.push({
                        lng: grid.lng,
                        lat: grid.lat
                    });
                }
                jsonData.push({
                    title:overlays[i].title,
                    id:overlays[i].id,
                    areas:pointArry,
                    color:overlays[i].color
                })
            }
            try {
                var options = {
                    collapsed: false,
                    withQuotes: true
                };
                $('#json-renderer').jsonViewer(jsonData, options);
                console.log(jsonData);
                return $('#json-renderer').text();
            } catch (error) {
            }
            return ""
        }

        $(document).on("click",'#downJson',function() {
            // 下载文件方法
            var eleLink = document.createElement('a');
            eleLink.download = "areaMap.json";
            eleLink.style.display = 'none';
            // 字符内容转变成blob地址
            var blob = new Blob([getJson() + "\r\n" + 123 ]);
            eleLink.href = URL.createObjectURL(blob);
            // 触发点击
            document.body.appendChild(eleLink);
            eleLink.click();
            // 然后移除
            document.body.removeChild(eleLink);
        });

        function Editing(state){
            for(var i = 0; i < overlays.length; i++){
                state=='enable'?overlays[i].overlay.enableEditing():overlays[i].overlay.disableEditing();
            }
        }

        function  showPolygon(btn){
            var polygon = new BMap.Polygon([
                new BMap.Point(113.941853,22.530777),
                new BMap.Point(113.940487,22.527789),
                new BMap.Point(113.94788,22.527597),
                new BMap.Point(113.947925,22.530618)
            ], styleOptions);  //创建多边形
            map.addOverlay(polygon);   //增加多边形
            // overlays.push(polygon); //是否把该图像加入到编辑和删除行列
            btn.setAttribute('disabled','false');
            showText();
        }

        function showText(){
            var opts = {
                position : new BMap.Point(113.941853,22.530777),    // 指定文本标注所在的地理位置
                offset   : new BMap.Size(30, 30)    //设置文本偏移量
            }
            var label = new BMap.Label("不可编辑", opts);  // 创建文本标注对象
            label.setStyle({
                color : "black",
                fontSize : "16px",
                fillColor:"red",      //填充颜色。当参数为空时，圆形将没有填充效果。
            });
            map.addOverlay(label);
        }
    });
</script>
</body>
</html>

