BIGEMPA Js API示例中心

限制地图的拖动源代码展示

代码编辑区 运行 下载 还原
<!DOCTYPE html>

<html>
<head>
    <meta charset='UTF-8'/>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no'/>
    <link href="http://www.bigemap.com/Public/css/button.min.css" rel="stylesheet">
    <!--
        以下CSS地址请在安装软件了替换成本地的地址
        CSS地址请使用:
        http://localhost:9000/bigemap.js/v2.1.0/bigemap.css
        软件下载地址 http://www.bigemap.com/reader/download/detail201802017.html
    -->
    <link href='http://www.bigemap.com:9000/bigemap.js/v2.1.0/bigemap.css' rel='stylesheet'/>
    <!--
        JS地址请使用:
        http://localhost:9000/bigemap.js/v2.1.0/bigemap.js
    -->
    <script src='http://www.bigemap.com:9000/bigemap.js/v2.1.0/bigemap.js'></script>
    <style>
        body {
            margin: 0;
            padding: 0;
        }

        #map {
            position: absolute;
            top: 0;
            bottom: 0;
            width: 100%;
        }

        #control {

            padding: 15px;
            position: absolute;
            top: 10px;
            right: 20px;
            z-index: 9;
            border-radius:5px;
        }
    </style>
    <title>限制地图</title>
</head>
<body>
<div id="control">
    <div>
        <a class="button button-tiny button-rounded button-primary" href="javascript:void (0);">
            限制区域
        </a>
    </div>
    <br>
    <div>
        <a id="panTo" class="button button-tiny button-rounded button-primary" href="javascript:void (0)">
            取消限制
        </a>
    </div>
</div>
<div id='map'></div>
<script>
    // 软件配置信息地址,软件安装完成之后使用本地地址,如:http://localhost:9000
    BM.Config.HTTP_URL = 'http://www.bigemap.com:9000';
    // 在ID为map的元素中实例化一个地图,并设置地图的ID号,ID号程序自动生成,无需手动配置,中心点,默认的级别和显示级别控件
    var map = BM.map('map', 'bigemap.zhongkexingtu', {center: [30, 102], zoom: 9, zoomControl: true,attributionControl:false});
    var el=document.querySelectorAll('.button');

    //限制地图浏览范围(左下家 右上角)
    map.setMaxBounds([[30,100],[34,105]]);

    //绘制限制的范围
    var gon = BM.polygon([[30,100],[34,100],[34,105],[30,105]]).addTo(map)
    map.fitBounds(gon.getBounds(),{fillColor:"red"})

    //限制地图的移动的区域。具体API详情请参见 :http://www.bigemap.com/offlinemaps/api/#map-setmaxbounds
    el[0].onclick=function () {
        gon.addTo(map)
        //限制地图浏览范围
        map.setMaxBounds([[30,100],[34,105]]);
        map.panInsideBounds([[30,100],[34,105]]);
    };

    el[1].onclick=function () {
        map.setMaxBounds();
        gon.remove()
    };
</script>
</body>
</html>