Box Selection

No countries selected

Using a DragBox interaction to select features.

This example shows how to use a DragBox interaction to select features. Selected features are added to the feature overlay of a select interaction (ol/interaction/Select) for highlighting.

Use Ctrl+Drag (Command+Drag on Mac) to draw boxes.

<!DOCTYPE html>
    <title>Box Selection</title>
    <link rel="stylesheet" href="" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src=",Element.prototype.classList,URL"></script>

      .ol-dragbox {
        background-color: rgba(255,255,255,0.4);
        border-color: rgba(100,150,0,1);
    <div id="map" class="map"></div>
    <div id="info">No countries selected</div>
      import Map from 'ol/Map.js';
      import View from 'ol/View.js';
      import {platformModifierKeyOnly} from 'ol/events/condition.js';
      import GeoJSON from 'ol/format/GeoJSON.js';
      import {DragBox, Select} from 'ol/interaction.js';
      import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';
      import {OSM, Vector as VectorSource} from 'ol/source.js';

      var vectorSource = new VectorSource({
        url: 'data/geojson/countries.geojson',
        format: new GeoJSON()

      var map = new Map({
        layers: [
          new TileLayer({
            source: new OSM()
          new VectorLayer({
            source: vectorSource
        target: 'map',
        view: new View({
          center: [0, 0],
          zoom: 2

      // a normal select interaction to handle click
      var select = new Select();

      var selectedFeatures = select.getFeatures();

      // a DragBox interaction used to select features by drawing boxes
      var dragBox = new DragBox({
        condition: platformModifierKeyOnly


      dragBox.on('boxend', function() {
        // features that intersect the box are added to the collection of
        // selected features
        var extent = dragBox.getGeometry().getExtent();
        vectorSource.forEachFeatureIntersectingExtent(extent, function(feature) {

      // clear selection when drawing a new box and when clicking on the map
      dragBox.on('boxstart', function() {

      var infoBox = document.getElementById('info');

      selectedFeatures.on(['add', 'remove'], function() {
        var names = selectedFeatures.getArray().map(function(feature) {
          return feature.get('name');
        if (names.length > 0) {
          infoBox.innerHTML = names.join(', ');
        } else {
          infoBox.innerHTML = 'No countries selected';