Skip to content

自定义控件 ​

自定义控件示例。 自定义 控件 旋转 北向 SVG

本示例创建了一个带有内联 SVG 北向箭头的"旋转到北向"控件。该控件的 render 方法会让箭头随地图一起旋转,使其始终指向北方,并在地图未旋转时通过 ol-hidden 类隐藏控件。该控件替代了默认的 ol/control/Rotate 控件(后者已从地图控件中移除)。使用 Alt+Shift+Drag 可旋转地图。

main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import Control from 'ol/control/Control.js';
import {defaults as defaultControls} from 'ol/control/defaults.js';
import TileLayer from 'ol/layer/Tile.js';
import OSM from 'ol/source/OSM.js';

//
// 定义旋转到北向的控件。
//

const northArrow =
  '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">' +
  '<path d="M12 2 5 21l7-4z"/>' +
  '<path d="M12 2l7 19-7-4z" opacity=".5"/>' +
  '</svg>';

class RotateNorthControl extends Control {
  /**
   * @param {Object} [opt_options] 控件选项。
   */
  constructor(opt_options) {
    const options = opt_options || {};

    const button = document.createElement('button');
    button.type = 'button';
    button.title = 'Rotate to north';
    button.innerHTML = northArrow;

    const element = document.createElement('div');
    element.className = 'rotate-north ol-unselectable ol-control';
    element.appendChild(button);

    super({
      element: element,
      target: options.target,
    });

    /**
     * 与地图一起旋转的北向箭头。
     * @type {SVGElement}
     * @private
     */
    this.arrow_ = button.querySelector('svg');

    /**
     * 箭头上一次渲染时的旋转角度。
     * @type {number|undefined}
     * @private
     */
    this.rotation_ = undefined;

    button.addEventListener('click', this.handleRotateNorth.bind(this), false);
  }

  handleRotateNorth() {
    this.getMap().getView().animate({rotation: 0, duration: 250});
  }

  /**
   * 保持箭头指向北方,并在地图已经旋转到北向时隐藏控件。
   * @param {import('ol/MapEvent.js').default} mapEvent 地图事件。
   * @override
   */
  render(mapEvent) {
    const frameState = mapEvent.frameState;
    if (!frameState) {
      return;
    }
    const rotation = frameState.viewState.rotation;
    if (rotation !== this.rotation_) {
      this.arrow_.style.transform = 'rotate(' + rotation + 'rad)';
      this.element.classList.toggle('ol-hidden', rotation === 0);
      this.rotation_ = rotation;
    }
  }
}

//
// 创建地图,用一个旋转到北向的控件替代默认的
// 旋转控件。
//

const map = new Map({
  controls: defaultControls({rotate: false}).extend([new RotateNorthControl()]),
  layers: [
    new TileLayer({
      source: new OSM(),
    }),
  ],
  target: 'map',
  view: new View({
    center: [0, 0],
    zoom: 3,
    rotation: 1,
  }),
});
index.html
html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>自定义控件</title>
    <link rel="stylesheet" href="node_modules/ol/ol.css">
    <style>
      .map {
        width: 100%;
        height: 400px;
      }
      .rotate-north {
        top: 65px;
        left: .5em;
      }
      .ol-touch .rotate-north {
        top: 80px;
      }
      .rotate-north svg {
        width: 1em;
        height: 1em;
        vertical-align: middle;
      }
      .rotate-north.ol-hidden {
        opacity: 0;
        visibility: hidden;
        transition: opacity .25s linear, visibility 0s linear .25s;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>

    <script type="module" src="main.js"></script>
  </body>
</html>
package.json
json
{
  "name": "custom-controls",
  "dependencies": {
    "ol": "10.11.0"
  },
  "devDependencies": {
    "vite": "^8.1.5"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}