自定义控件

<!doctype html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/css/ol.css" type="text/css">
    <style>
        .map {
            height: 400px;
            width: 100%;
        }
        .share {
            position: absolute;
            top: 10px;
            right: 10px;
            border: 1px;
            border-color: #333;
            background-color: #339999;
            color: #fff;
            box-shadow: 0px 0px 2px #666;
            cursor: pointer;
            padding: 0 4px 0 4px;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/build/ol.js"></script>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <title>OpenLayers example</title>
</head>
<body>
<h2>My Map</h2>
<div id="map" class="map"></div>
<script type="text/javascript">
    var map = new ol.Map({
        layers: [
            new ol.layer.Tile({
                source: new ol.source.OSM()
            })
        ],
        target: 'map',
        view: new ol.View({
            center: ol.proj.transform(
                [106.70, 26.62], 'EPSG:4326', 'EPSG:3857'),
            zoom: 10
        })
    });

    // 在viewport节点下添加一个分享按钮
    var viewport = map.getViewport();
    $(viewport).append('<div id="share" class="share">分享地图</div>');

    // 监听按钮点击事件,执行相关操作
    document.getElementById('share').onclick = function() {
        alert('分享当前地图给朋友');
    }
</script>
</body>
</html>

##自适配区
<!doctype html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/css/ol.css" type="text/css">
    <style>
        .map {
            height: 800px;
            width: 100%;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/build/ol.js"></script>
    <title>OpenLayers example</title>
</head>
<body>
<h2>My Map</h2>
<div id="map" class="map"></div>
<input type="button" value="显示重庆" onclick="showChongQing()">
<script type="text/javascript">

    var map = new ol.Map({
        layers: [
            new ol.layer.Tile({source: new ol.source.OSM()})
        ],
        view: new ol.View({
            // 设置为地图中心
            center: [106.71, 26.63],
            projection: 'EPSG:4326',
            zoom: 10
        }),
        target: 'map'
    });

    function showChongQing() {
        // 让地图最大化完全地显示区域[106,26.5,106.14,30.74]
        map.getView().fit([106,26.5,106.14,30.74], map.getSize());
    }
</script>
</body>
</html>
##调动地图
<!doctype html>
<html lang="en">
<head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/css/ol.css" type="text/css">
    <style>
        .map {
            height: 200px;
            width: 100%;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/build/ol.js"></script>
    <title>OpenLayers example</title>
</head>
<body>
<h2>My Map</h2>
<p>地图1</p>
<div id="map1" class="map"></div>
<p>地图2</p>
<div id="map2" class="map"></div>
<button onclick="swapMap()">地图交换</button>
<script type="text/javascript">
    // 创建一个视图
    // 创建第一个地图
    var map1 = new ol.Map({
        layers: [
            new ol.layer.Tile({source: new ol.source.OSM()})
        ],
        view: new ol.View({
            center: [0, 0],
            zoom: 2
        }),
        target: 'map1'
    });

    // 创建第二个地图
    var map2 = new ol.Map({
        layers: [
            new ol.layer.Tile({source: new ol.source.OSM()})
        ],
        view: new ol.View({
            center: [0, 0],
            zoom: 2
        }),
        target: 'map2'
    });

    function swapMap() {
        // 改变两个地图的容器
        map1.setTarget('map2');
        map2.setTarget('map1');
    }
</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值