The problem is that you are using getMap()
in the constructor of your control. But at the moment you instantiate the control, it is not yet added to the map. So, getMap()
returns undefined.
You can overwrite setMap(map)
in your control which is called by the map when executing map.setControl()
.
ol3_legend.prototype.setMap = function(map) {
ol.control.Control.prototype.setMap.call(this, map);
console.log(map);
};