Skip to content
Advertisement

OpenLayers map.addLayer TypeError

I’m trying to do some simple drawing on OpenStreetMap data using OpenLayers (version 6.5.0). The map loads fine. I try to do the drawing when the button in the top right is clicked.

I convert this array of GPS coordinates into a Polygon, into a Feature, into an ol.source.Vector, into an ol.layer.Vector. I log every object constructed along the way on the console. This appears to go fine.

I finally want to add the (Vector) layer to the existing map using the .addLayer() function. At this point, things go wrong inside the OpenLayer 6.5.0 JavaScript code. Deep inside the ol.js code, it throws a TypeError: t.addEventListener is not a function. Browser screenshot

I’ve looked at multiple examples:

So far, I have no clue whether this a bug in OpenLayer 6.5.0 or I’m missing something during conversion of my GPS coordinates array into an ol.layer.vector object. Any hints on this?

Entire html/javascript code below:

JavaScript

P.S. I am aware that I still may have to swap latitude and longitude and convert the coordinates in some other way for OpenLayer to interpret them correctly. But that’s not the main point here. I guess…

Advertisement

Answer

As well as missing and misplaced [ ] geojson coordinates must be specified in lon, lat order and features must be read into the view projection

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement