O'Reilly logo

OpenLayers 3 : Beginner's Guide by Erik Hazzard, Paul Spencer, Thomas Gratier

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – making rectangle export to GeoJSON with ol.interaction.DragBox

After introducing the goal, we should do the following:

  1. Reuse the usual sample for the chapter that include the osm_default and map variables.
  2. Between the two variables, declare ol.interaction.DragBox, as follows:
    var dragBoxInteraction = new ol.interaction.DragBox({
      condition: ol.events.condition.shiftKeyOnly,
      style: new ol.style.Style({
        stroke: new ol.style.Stroke({
          color: 'red',
          width: 2
        })
      })
    });
  3. Like for DragRotateAndZoom, deactivate DragZoom, and add the new interaction in the interactions parameter within ol.Map, as follows:
    interactions: ol.interaction.defaults({
      shiftDragZoom: false
    }).extend([dragBoxInteraction]),
  4. Then, open your browser and try to draw a rectangle ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required