# Error/Bug in latest ol3 version (3.9) with insertAt

I've recently updated my ol version from 3.4 towards 3.9

I have a dragandrop layertree which basically gets the current position of the layer, removes it and inserts back in the new position using:

map.getLayers().insertAt(newIndex, layer);


this works great on V3.4 for both (bingsmap/google and for my WMS layers)

However using the same code on the latest release (3.9) I get an error for my WMS layers only (not for bings/googlemaps) as follow

Uncaught AssertionError: Assertion failed: listeners already registered


Any thoughts on the matter?

• Wouldn't be better setAt(index, layer) since you are just changing position? – Jonatas Walker Oct 1 '15 at 8:21
• It gave me the same error. In the end i used :map.getLayers().getArray().splice(new_index, 0, layer) – julsbreakdown Oct 1 '15 at 8:28

try first to remove the layer and then add it back. Lets say you want to move it from index 6 to index 2, you should do:

var oldindex = 6;
var newindex = 2;
map.getLayers().removeAt(oldindex);
map.getLayers().insertAt(newIndex, layer);


This worked for me. I got "Assertion failed: listeners already registered" with

 var next = layers.item(index - 1); layers.setAt(index - 1, layer); layers.setAt(index, next);  which I got working with:

 

(using ol version v3.15.1) layers.removeAt(index); layers.insertAt(index-1, layer);