Skip to content

Modify an extent ​

Modify an extent示例。 范围 交互 box bbox modify resize

本示例展示如何使用 Extent 交互,通过拖动范围的四角或四边来修改给定范围。

main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import {never} from 'ol/events/condition.js';
import ExtentInteraction from 'ol/interaction/Extent.js';
import TileLayer from 'ol/layer/Tile.js';
import {transformExtent} from 'ol/proj.js';
import OSM from 'ol/source/OSM.js';

const map = new Map({
  layers: [
    new TileLayer({
      source: new OSM(),
    }),
  ],
  target: 'map',
  view: new View({
    center: [0, 0],
    zoom: 2,
  }),
});

const extent = new ExtentInteraction({
  extent: transformExtent([-10, -10, 10, 10], 'EPSG:4326', 'EPSG:3857'),
  createCondition: never, // 按下指针时不要创建新的范围
  drag: true, // 拖动已有范围
  boxStyle: {
    'fill-color': 'rgba(255, 255, 255, 0.4)',
    'stroke-color': '#3399CC',
    'stroke-width': 1.25,
  },
});
map.addInteraction(extent);
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;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>

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