Search code examples
javascriptopenlayers-5

OpenLayers 5 draw interactions - "n is not a constructor" at Draw.js:338


I'm trying to allow the user to draw and modify features, following this example: https://openlayers.org/en/v5.3.0/examples/draw-and-modify-features.html - but I'm getting two errors:

Uncaught TypeError: n is not a constructor
    at e.r [as geometryFunction_] (Draw.js:338)
    at e.startDrawing_ (Draw.js:700)
    at e.handleUpEvent (Draw.js:570)
    at e.handleEvent (Pointer.js:132)
    at e.handleEvent (Draw.js:524)
    at e.handleMapBrowserEvent (PluggableMap.js:933)
    at e (events.js:41)
    at e.dispatchEvent (Target.js:101)
    at e.handlePointerUp_ (MapBrowserEventHandler.js:166)
    at e (events.js:41)

and

Uncaught TypeError: Cannot read property 'getGeometry' of null
    at e.modifyDrawing_ (Draw.js:717)
    at e.handlePointerMove_ (Draw.js:618)
    at e.handleEvent (Draw.js:518)
    at e.handleMapBrowserEvent (PluggableMap.js:933)
    at e (events.js:41)
    at e.dispatchEvent (Target.js:101)
    at e.relayEvent_ (MapBrowserEventHandler.js:279)
    at e (events.js:41)
    at e.dispatchEvent (Target.js:101)
    at e.fireNativeEvent (PointerEventHandler.js:397)

The first error comes up in the console as soon as I click to start drawing a feature. The blue dot then stays in the same place, and the second error fires repeatedly as I move the mouse around. Clicking on the map won't do anything after that first click.

I can't see anything immediately obvious in Draw.js, and as far as I can tell, my code is exactly the same, bar a few minor adaptations to suit my application, as in the example I mentioned above.

I'm using OpenLayers 5.3.0, which I'm loading via rawgit:

<link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>

Other relevant parts of my code are as follows - adapted from the example mentioned above:

var drawSource = new ol.source.Vector();
var drawVector = new ol.layer.Vector({
  source: drawSource,
  style: new ol.style.Style({
    fill: new ol.style.Fill({
      color: 'rgba(255, 255, 255, 0.2)'
    }),
    stroke: new ol.style.Stroke({
      color: '#ffcc33',
      width: 2
    }),
    image: new ol.style.Circle({
      radius: 7,
      fill: new ol.style.Fill({
        color: '#ffcc33'
      })
    })
  })
});
map.addLayer(drawVector);

var modify = new ol.interaction.Modify({source: drawSource});

map.addInteraction(modify);

var draw, snap; // global so we can remove them later

function removeInteractions() {
  map.removeInteraction(draw);
  map.removeInteraction(snap);
}

function addInteractions() {
  draw = new ol.interaction.Draw({
    source: drawSource,
    type: ol.geom.Polygon
  });
  map.addInteraction(draw);
  snap = new ol.interaction.Snap({source: drawSource});
  map.addInteraction(snap);
}

I'm invoking addInteractions() and removeInteractions() via hyperlinks. No errors are coming up before I invoke addInteractions() or after I invoke removeInteractions().


Solution

  • Draw type should be a string

      draw = new ol.interaction.Draw({
        source: drawSource,
        type: 'Polygon'
      });