<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath() + '/'}">
    <meta charset="utf-8">
    <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">
    <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">
    <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=2qzNKd33lQERUDakqDDyvLOyqM1x2i8y"></script>
</head>
<body>


<div class="layui-anim layui-anim-fadein">
    <div class="layui-card">
        <div class="layui-inline">
            <input type="text" class="layui-input search-item" name="searchMapId" id="searchMapId" value="">
        </div>
        <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id = "searchMap">
            <i class="fa fa-lg fa-search"></i>
        </button>
        <input type="hidden" id="mapX" value=""><!--经度-->
        <input type="hidden" id="mapY" value=""><!--纬度-->
        <input type="hidden" id="mapName" value=""><!--名称-->
        <div id="maplocation" style="width:100%;height:500px;"></div>
        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-footer" style="left: 0;">
                    <button type="button" class="layui-btn" id = "save">保存</button>
                </div>
            </div>
        </div>
    </div>

</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/jquery-2.1.1.min.js"></script>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script src="assets/js/vendor/viewer/viewer.min.js"></script>

<script>
    layui.config({
        base: 'assets/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index', 'table', 'laydate', 'common'], function() {
        var $ = layui.$;
        var $win = $(window);
        var table = layui.table;
        var admin = layui.admin;
        var common = layui.common;
        var mapObj = {}

        initMap(88.886487,29.276981);

        var zoomNum = 17;

        $("#save").on('click',function(){
            var mapX = $("#mapX").val();
            var mapY = $("#mapY").val();
            if ("经度：空" == mapX || "纬度：空" == mapY) {
                layer.msg("尚未选择位置");
            } else {
                top.dialog.dialogData.activityAddressX = mapX;
                top.dialog.dialogData.activityAddressY = mapY;
                top.dialog.dialogData.activityAddress = $("#mapName").val();
                closeWindow();
            }
        });

        function ComplexCustomOverlay(point, text) { // 构造函数
            this._point = point;
            this._text = text;
        }

        ComplexCustomOverlay.prototype = new BMap.Overlay();
        // 监听事件 通过构造函数的prototype属性 添加任自定义的方法
        ComplexCustomOverlay.prototype.addEventListener = function (event, fun) {
            this._div['on' + event] = fun;
        }
        ComplexCustomOverlay.prototype.initialize = function (map) {
            this._map = map;
            var A = this._div = document.createElement("a");
            A.style.display = "block";
            A.style.position = "absolute";
            A.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
            A.style.border = "1px solid #ff6000";
            A.style.color = "#FF0000";
            A.style.display = "block";
            A.style.fontWeight = "bold";
            A.style.height = "20px";
            A.style.padding = "0px 4px 0px 0px";
            A.style.lineHeight = "15px";
            A.style.whiteSpace = "nowrap";
            A.style.MozUserSelect = "none";
            A.style.fontSize = "13px"
            var span = this._span = document.createElement("span");
            var img = document.createElement("img");
            img.src = "<%=basePath%>static/images/department.png";
            img.style.height = "18px";
            img.style.width = "20px";
            A.appendChild(img);
            A.appendChild(span);
            span.appendChild(document.createTextNode(this._text));
            var that = this;

            var arrow = this._arrow = document.createElement("div");
            arrow.style.background = "url(<%=basePath%>static/images/member.png) -77px -119px no-repeat";
            arrow.style.position = "absolute";
            arrow.style.width = "21px";
            arrow.style.height = "32px";
            arrow.style.top = "-1px";
            arrow.style.left = "-11px";//-11
            arrow.style.overflow = "hidden";
            A.appendChild(arrow);

            A.onmouseover = function () {
                this.style.borderColor = "#32CD32";
                this.style.color = "#32CD32";
                arrow.style.backgroundPosition = "-77px -161px";
                this.style.cursor = "pointer";
                this.style.zIndex = 100;
            }

            A.onmouseout = function () {
                this.style.borderColor = "#ff6000";
                this.style.color = "#ff6000";
                arrow.style.backgroundPosition = "-77px -119px";
                this.style.zIndex = 1;
            }
            map.getPanes().labelPane.appendChild(A);
            return A;
        }
        // 实现绘制方法
        ComplexCustomOverlay.prototype.draw = function () {
            var map = this._map;
            var pixel = map.pointToOverlayPixel(this._point);
            this._div.style.left = pixel.x + "px";
            this._div.style.top = pixel.y + "px";
        }

        function closeWindow() {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index);
        }

        // 查询地名对象
        var geoc = new BMap.Geocoder();

        //创建和初始化地图函数：
        function initMap(x, y) {
            createMap(x, y);//创建地图
            setMapEvent();//设置地图事件
            addMapControl();//向地图添加控件
        }

        //创建地图函数：
        function createMap(x, y) {
            var map = new BMap.Map("maplocation");//在百度地图容器中创建一个地图
            var point = new BMap.Point(x, y);//定义一个中心点坐标
            map.addOverlay(new BMap.Marker(point));//定点坐标红点覆盖
            /*map.centerAndZoom(point, zoomNum);//设定地图的中心点和坐标并将地图显示在地图容器中*/
            map.centerAndZoom(point, 12)
            window.map = map;//将map变量存储在全局
        }


        //地图事件设置函数：
        function setMapEvent() {
            map.enableDragging();//启用地图拖拽事件，默认启用(可不写)
            map.enableScrollWheelZoom();//启用地图滚轮放大缩小
            map.enableContinuousZoom();//开启缩放平滑
            map.enableDoubleClickZoom();//启用鼠标双击放大，默认启用(可不写)
            map.enableKeyboard();//启用键盘上下左右键移动地图
            map.addEventListener("click", function (e) {
                var LONGITUDE = e.point.lng;
                var LATITUDE = e.point.lat;
                /*$("#LONGITUDE").text("经度："+LONGITUDE);
                $("#LATITUDE").text("纬度："+LATITUDE);*/
                geoc.getLocation(e.point, function (rs) {
                    var addComp = rs.addressComponents;
                    var site = addComp.province + "." + addComp.city + "." + addComp.district;
                    if (addComp.street) {
                        site = site + "." + addComp.street;
                    }
                    if (addComp.streetNumber) {
                        site = site + "." + addComp.streetNumber;
                    }

                    /*$("#MAP_NAME").text(site);*/
                    $('#mapX').val(LONGITUDE);
                    $('#mapY').val(LATITUDE);
                    $('#mapName').val(site);
                    /* console.log("经度"+LONGITUDE)
                    console.log("纬度"+LATITUDE)
                    console.log("名称"+site)*/
                });

                // 如果不循环删除遮罩会一直累加
                var allOverlay = map.getOverlays();
                for (var i = 0; i < allOverlay.length; i++) {
                    map.removeOverlay(allOverlay[i]);
                }
                // 第一种"显示太大
                // var point = new BMap.Point(LONGITUDE, LATITUDE);
                // var myCompOverlay = new ComplexCustomOverlay(point, "经度："+LONGITUDE + "|纬度："+LATITUDE);
                // map.addOverlay(myCompOverlay);
                // 第二中"系统默认
                var point = new BMap.Point(LONGITUDE, LATITUDE);//定义一个中心点坐标
                map.addOverlay(new BMap.Marker(point));//定点坐标红点覆盖
                // 重新初始化
                // initMap(LONGITUDE, LATITUDE);
            });
            map.addEventListener("zoomend", function (e) {
                zoomNum = map.getZoom();
            });
        }

        // 地图控件添加函数：
        function addMapControl() {
            // 向地图中添加缩放控件
            var ctrl_nav = new BMap.NavigationControl({
                anchor: BMAP_ANCHOR_TOP_LEFT,
                type: BMAP_NAVIGATION_CONTROL_LARGE
            });
            map.addControl(ctrl_nav);
            // 向地图中添加缩略图控件
            var ctrl_ove = new BMap.OverviewMapControl({
                anchor: BMAP_ANCHOR_BOTTOM_RIGHT,
                isOpen: 2
            });
            map.addControl(ctrl_ove);
            // 向地图中添加比例尺控件
            var ctrl_sca = new BMap.ScaleControl({
                anchor: BMAP_ANCHOR_BOTTOM_LEFT
            });
            map.addControl(ctrl_sca);
        }


        $("#searchMap").on('click',function(){
            var searchInput = $('#searchMapId').val();
            if (!searchInput) {
                layer.msg("查询内容不能为空");
            }
            var localSearch = new BMap.LocalSearch(map);
            localSearch.setSearchCompleteCallback(function (msg) {
                if (localSearch.getStatus() == BMAP_STATUS_SUCCESS) {
                    var poi = msg.getPoi(0);
                    if (poi) {
                        initMap(poi.point.lng, poi.point.lat);
                        var LONGITUDE = poi.point.lng;
                        var LATITUDE = poi.point.lat;
                        $("#mapX").val(LONGITUDE);
                        $("#mapY").val(LATITUDE);
                        $("#mapName").val((poi.province ? poi.province + "." : "") + (poi.address ? poi.address + "." : poi.city + ".") + poi.title);
                        console.log(poi);
                        $("#MAP_NAME").text((poi.province ? poi.province + "." : "") + (poi.address ? poi.address + "." : poi.city + ".") + poi.title);
                    }
                } else {
                    layer.msg("没有查询结果");
                }
            });
            localSearch.search(searchInput);
        });
    });

</script>
</body>
</html>