框选
框选示例。 DragBox 要素 选择 box
本示例展示如何使用 DragBox 交互来选择要素。要素通过选择交互(ol/interaction/Select)进行选取,以获得高亮显示并应用过滤(颜色为 #CC6767 的要素不可选择)
使用 Ctrl+Drag(Mac 上为 Command+Drag)绘制选择框。
main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import {platformModifierKeyOnly} from 'ol/events/condition.js';
import {getWidth} from 'ol/extent.js';
import GeoJSON from 'ol/format/GeoJSON.js';
import DragBox from 'ol/interaction/DragBox.js';
import Select from 'ol/interaction/Select.js';
import VectorLayer from 'ol/layer/Vector.js';
import VectorSource from 'ol/source/Vector.js';
import Fill from 'ol/style/Fill.js';
import Stroke from 'ol/style/Stroke.js';
import Style from 'ol/style/Style.js';
const vectorSource = new VectorSource({
url: 'https://openlayers.org/data/vector/ecoregions.json',
format: new GeoJSON(),
});
const style = new Style({
fill: new Fill({
color: '#eeeeee',
}),
});
const map = new Map({
layers: [
new VectorLayer({
source: vectorSource,
background: '#1a2b39',
style: function (feature) {
const color = feature.get('COLOR_BIO') || '#eeeeee';
style.getFill().setColor(color);
return style;
},
}),
],
target: 'map',
view: new View({
center: [0, 0],
zoom: 2,
constrainRotation: 16,
}),
});
const selectedStyle = new Style({
fill: new Fill({
color: 'rgba(255, 255, 255, 0.6)',
}),
stroke: new Stroke({
color: 'rgba(255, 255, 255, 0.7)',
width: 2,
}),
});
// 一个用于处理点击的普通选择交互
const select = new Select({
filter: function (feature) {
return !(feature.get('COLOR_BIO') === '#CC6767');
},
style: function (feature) {
const color = feature.get('COLOR_BIO') || '#eeeeee';
selectedStyle.getFill().setColor(color);
return selectedStyle;
},
});
map.addInteraction(select);
// 一个用于通过绘制框来选择要素的 DragBox 交互
const dragBox = new DragBox({
condition: platformModifierKeyOnly,
});
map.addInteraction(dragBox);
dragBox.on('boxend', function () {
const boxExtent = dragBox.getGeometry().getExtent();
// 如果范围跨越了反子午线,则对每个世界分别处理
const worldExtent = map.getView().getProjection().getExtent();
const worldWidth = getWidth(worldExtent);
const startWorld = Math.floor((boxExtent[0] - worldExtent[0]) / worldWidth);
const endWorld = Math.floor((boxExtent[2] - worldExtent[0]) / worldWidth);
for (let world = startWorld; world <= endWorld; ++world) {
const left = Math.max(boxExtent[0] - world * worldWidth, worldExtent[0]);
const right = Math.min(boxExtent[2] - world * worldWidth, worldExtent[2]);
const extent = [left, boxExtent[1], right, boxExtent[3]];
const boxFeatures = vectorSource
.getFeaturesInExtent(extent)
.filter((feature) => feature.getGeometry().intersectsExtent(extent));
// 与框几何图形相交的要素会被添加到
// 已选要素集合中
// 如果视图没有倾斜旋转,框几何图形与
// 其范围是等价的,因此相交的要素可以
// 直接添加到集合中
const rotation = map.getView().getRotation();
const oblique = rotation % (Math.PI / 2) !== 0;
// 当视图倾斜旋转时,框的范围会
// 大于其几何图形,因此需要将框和候选
// 要素的几何图形绕一个公共锚点旋转,
// 以确认在框几何图形与其范围对齐后,
// 两者确实相交
if (oblique) {
const anchor = [0, 0];
const geometry = dragBox.getGeometry().clone();
geometry.translate(-world * worldWidth, 0);
geometry.rotate(-rotation, anchor);
const extent = geometry.getExtent();
boxFeatures.forEach(function (feature) {
const geometry = feature.getGeometry().clone();
geometry.rotate(-rotation, anchor);
if (geometry.intersectsExtent(extent)) {
select.selectFeature(feature);
}
});
} else {
boxFeatures.forEach(select.selectFeature.bind(select));
}
}
});
// 在绘制新框时以及点击地图时清除选择
dragBox.on('boxstart', function () {
select.clearSelection();
});
const infoBox = document.getElementById('info');
select.on('select', function () {
const names = select
.getFeatures()
.getArray()
.map((feature) => {
return feature.get('ECO_NAME');
});
if (names.length > 0) {
infoBox.innerHTML = names.join(', ');
} else {
infoBox.innerHTML = 'None';
}
});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>
<div>已选区域:<span id="info">无</span></div>
<script type="module" src="main.js"></script>
</body>
</html>package.json
json
{
"name": "box-selection",
"dependencies": {
"ol": "10.11.0"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}