Openlayers示例18 | Draw Shapes(绘制形状)

Draw Shapes

这演示geometryFunction了ol/interaction/Draw. 从上面的下拉列表中选择一种形状类型以开始绘制。要激活手绘,请按住该Shift键。方形绘图是通过使用type: 'Circle'带有 a 的 type来实现的geometryFunction,它创建一个 4 边正多边形而不是圆形。箱形图type: 'Circle'与 a 一起使用,geometryFunction它创建一个箱形多边形而不是圆形。星形绘图使用自定义几何函数,该函数使用绘图交互提供的中心和半径将圆转换为星形。

<!DOCTYPE html>
<html lang="zn">

<head>
  <meta charset="UTF-8">
  <!-- 引入OpenLayers CSS样式 -->
  <link rel="stylesheet"
    href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.13.0/css/ol.css">
  <!-- 引入OpenLayers JS库 -->
  <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.13.0/build/ol.js"></script>
  <!-- css代码 -->
  <style>
    .map {
      width: 100%;
      height: 500px;
    }
  </style>
  <title>Draw Shapes</title>
</head>
<body>
  <div id="map" class="map"></div>
  <form class="form-inline">
    <label for="type">Shape type: &nbsp;</label>
    <select class="form-control mr-2 mb-2 mt-2" id="type">
      <option value="Circle">Circle</option>
      <option value="Square">Square</option>
      <option value="Box">Box</option>
      <option value="Star">Star</option>
      <option value="None">None</option>
    </select>
    <input class="form-control mr-2 mb-2 mt-2" type="button" value="Undo" id="undo">
  </form>
</body>
<script>
  const raster = new ol.layer.Tile({
    source: new ol.source.OSM(),
  });

  const source = new ol.source.Vector({ wrapX: false });

  const vector = new ol.layer.Vector({
    source: source,
  });

  const map = new ol.Map({
    layers: [raster, vector],
    target: 'map',
    view: new ol.View({
      center: [-11000000, 4600000],
      zoom: 4,
    }),
  });

  const typeSelect = document.getElementById('type');

  let draw; // global so we can remove it later
  function addInteraction() {
    let value = typeSelect.value;
    if (value !== 'None') {
      let geometryFunction;
      if (value === 'Square') {
        value = 'Circle';
        geometryFunction = ol.interaction.Draw.createRegularPolygon(4);
      } else if (value === 'Box') {
        value = 'Circle';
        geometryFunction = ol.interaction.Draw.createBox();
      } else if (value === 'Star') {
        value = 'Circle';
        geometryFunction = function (coordinates, geometry) {
          const center = coordinates[0];
          const last = coordinates[coordinates.length - 1];
          const dx = center[0] - last[0];
          const dy = center[1] - last[1];
          const radius = Math.sqrt(dx * dx + dy * dy);
          const rotation = Math.atan2(dy, dx);
          const newCoordinates = [];
          const numPoints = 12;
          for (let i = 0; i < numPoints; ++i) {
            const angle = rotation + (i * 2 * Math.PI) / numPoints;
            const fraction = i % 2 === 0 ? 1 : 0.5;
            const offsetX = radius * fraction * Math.cos(angle);
            const offsetY = radius * fraction * Math.sin(angle);
            newCoordinates.push([center[0] + offsetX, center[1] + offsetY]);
          }
          newCoordinates.push(newCoordinates[0].slice());
          if (!geometry) {
            geometry = new ol.geom.Polygon([newCoordinates]);
          } else {
            geometry.setCoordinates([newCoordinates]);
          }
          return geometry;
        };
      }
      draw = new ol.interaction.Draw({
        source: source,
        type: value,
        geometryFunction: geometryFunction,
      });
      map.addInteraction(draw);
    }
  }

  /**
   * Handle change event.
   */
  typeSelect.onchange = function () {
    map.removeInteraction(draw);
    addInteraction();
  };

  document.getElementById('undo').addEventListener('click', function () {
    draw.removeLastPoint();
  });

  addInteraction();
</script>
</html>
posted @ 2022-03-23 10:57  槑孒  阅读(300)  评论(0编辑  收藏  举报