I just started experimenting with OpenLayers 4, and I have the following problem:

I have a map with a default raster(OSM), and on top of it a vector layer for drawing features, that covers only a small area. Now when I start drawing (e.g.) a polygon within this area(orange) I don't want the user be able to pull the blue form(form to be drawn) out of this while editing. Or just disable drawing geometry features on the raster layer.

enter image description here

1 Answer 1


I've done a sample doing this kind of things.

They are two mains parts (some incomplete excerpts to illustrate):

  • a function to manage condition(s) to use draw interaction

    var conditionNoModifierKeysWithin = function(mapBrowserEvent) {
      // Spatial constraints
      var drawWithin = rewindedPolygons.features.some(feat => {
        return turf.booleanPointInPolygon(
      var originalEvent = mapBrowserEvent.originalEvent;
      // condition combining keyboard keys & the spatial contraints
      return (
        !originalEvent.altKey &&
          !(originalEvent.metaKey || originalEvent.ctrlKey) &&
          !originalEvent.shiftKey &&
  • The condition option when instantiating your draw interaction e.g

    draw = new ol.interaction.Draw({
      condition: conditionNoModifierKeysWithin,
      source: source,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.