Skip to content

选择要素 ​

选择要素示例。 选择 矢量

在下方组合框中选择 Single-click、Click、Hover 和 Alt+Click 作为选择的事件类型。使用 Single-click 或 Click 时,可以按住 Shift 键来切换要素的选中状态。

注意:使用 Single-click 时,双击不会选中要素。这与 Click 不同,使用 Click 时双击既会选中要素又会缩放地图(因为存在 DoubleClickZoom 交互)。另外请注意,Single-click 的响应不如 Click 灵敏,因为它需要延迟一段时间来检测双击。

在本示例中,为 Select 交互的 select 事件注册了一个监听器,用于更新上方的选择状态。

main.js
js
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import {altKeyOnly, click, never, pointerMove} from 'ol/events/condition.js';
import GeoJSON from 'ol/format/GeoJSON.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 style = new Style({
  fill: new Fill({
    color: '#eeeeee',
  }),
});

const vector = new VectorLayer({
  source: new VectorSource({
    url: 'https://openlayers.org/data/vector/ecoregions.json',
    format: new GeoJSON(),
  }),
  background: 'white',
  style: function (feature) {
    const color = feature.get('COLOR') || '#eeeeee';
    style.getFill().setColor(color);
    return style;
  },
});

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

let select = null; // 对当前所选交互的引用

const selected = new Style({
  fill: new Fill({
    color: '#eeeeee',
  }),
  stroke: new Stroke({
    color: 'rgba(255, 255, 255, 0.7)',
    width: 2,
  }),
});

function selectStyle(feature) {
  const color = feature.get('COLOR') || '#eeeeee';
  selected.getFill().setColor(color);
  return selected;
}

// 基于 "singleclick" 工作的选择交互
const selectSingleClick = new Select({style: selectStyle});

// 基于 "click" 工作的选择交互
const selectClick = new Select({
  condition: click,
  style: selectStyle,
});

// 基于 "pointermove" 工作的选择交互
const selectPointerMove = new Select({
  condition: pointerMove,
  toggleCondition: never, // 否则按住 Shift 时移动事件会导致要素闪烁
  style: selectStyle,
});

const selectAltClick = new Select({
  style: selectStyle,
  condition: function (mapBrowserEvent) {
    return click(mapBrowserEvent) && altKeyOnly(mapBrowserEvent);
  },
});

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

const changeInteraction = function () {
  if (select !== null) {
    map.removeInteraction(select);
  }
  const value = selectElement.value;
  if (value == 'singleclick') {
    select = selectSingleClick;
  } else if (value == 'click') {
    select = selectClick;
  } else if (value == 'pointermove') {
    select = selectPointerMove;
  } else if (value == 'altclick') {
    select = selectAltClick;
  } else {
    select = null;
  }
  if (select !== null) {
    map.addInteraction(select);
    select.on('select', function (e) {
      document.getElementById('status').innerHTML =
        ' ' +
        e.target.getFeatures().getLength() +
        ' selected features (last operation selected ' +
        e.selected.length +
        ' and deselected ' +
        e.deselected.length +
        ' features)';
    });
  }
};

/**
 * select 元素的 onchange 回调。
 */
selectElement.onchange = changeInteraction;
changeInteraction();
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>
    <form>
      <label for="type">操作类型 &nbsp;</label>
      <select id="type">
        <option value="click" selected>单击(Click)</option>
        <option value="singleclick">单次点击(Single-click)</option>
        <option value="pointermove">悬停</option>
        <option value="altclick">Alt+点击</option>
        <option value="none">无</option>
      </select>
      <span id="status">&nbsp;0 个已选要素</span>
    </form>

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