三章-18-自定义概览(鹰眼)控件

2020-04-20  本文已影响0人  彩云飘过

本文基于腾讯课堂老胡的课《跟我学Openlayers--基础实例详解》做的学习笔记,使用的openlayers 5.3.x api。

源码 见 1018.html ,对应的 官网示例 https://openlayers.org/en/latest/examples/overviewmap.html?q=OverviewMap

image.png
<!DOCTYPE html>
<html>

<head>
  <title>自定义概览(鹰眼)控件
  </title>
  <link rel="stylesheet" href="../include/ol.css" type="text/css">
  <script src="../include/ol.js"></script>

</head>
<style>
  .ol-custom-overviewmap,
  .ol-custom-overviewmap.ol-uncollapsible {
    bottom: auto;
    left: auto;
    right: 0;
    top: 0;
  }

  .ol-custom-overviewmap:not(.ol-collapsed) {
    border: 1px solid black;
  }

  .ol-custom-overviewmap .ol-overviewmap-map {
    border: none;
    width: 300px;
  }

  .ol-custom-overviewmap .ol-overviewmap-box {
    border: 2px solid red;
  }

  .ol-custom-overviewmap:not(.ol-collapsed) button {
    bottom: auto;
    left: auto;
    right: 1px;
    top: 1px;
  }

  .ol-rotate {
    top: 170px;
    right: 0;
  }
</style>

<body>

  <div id="map" class="map"></div>

  <script>
  //使用OverviewMap自定义鹰眼控件,自定义样式,
    var overviewMapControl = new ol.control.OverviewMap({

      className: 'ol-overviewmap ol-custom-overviewmap',
      layers: [
        new ol.layer.Tile({
          source: new ol.source.OSM({
            'url': 'https://{a-c}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png' +
              '?apikey=0e6fc415256d4fbb9b5166a718591d71'
          })
        })
      ],
      collapseLabel: '\u00BB',//收缩时的标签样式
      label: '\u00AB', //展开时的样式
      collapsed: false
    });

    var map = new ol.Map({
      controls: ol.control.defaults().extend([
        overviewMapControl
      ]),
//当地图拖动时,鹰眼地图也拖动到对应区域
     // interactions: ol.interaction.defaults().extend([ //交互控制,有没有在这里没有区别
      //  new ol.interaction.DragRotateAndZoom()
     // ]),
      layers: [
        new ol.layer.Tile({
          source: new ol.source.OSM()
        })
      ],
      target: 'map',
      view: new ol.View({
        center: [500000, 6000000],
        zoom: 7
      })
    });
  </script>
</body>

</html>
上一篇下一篇

猜你喜欢

热点阅读