Search code examples
javascriptmapboxmapbox-gl-jsreact-map-glmapbox-gl-draw

How to access control instances from map instance in Mapbox


I need a way to access a registered control instance from Mapbox map instance.

For example, say I register a hypothetical Mapbox control:

const control = new IControl(); // Where IControl is the hypothetical mapbox control

map.addControl(control);

How do I access this control instance in some other places in my codebase where I only have access to the map instance??

For context; I need to perform some map actions depending on some values only the control instance is aware of.

Thanks.


Solution

  • There is no standard way to do this. But there is nothing preventing you from doing:

    map.addControl(control);
    map._myControl = control;
    

    and later accessing it with map._myControl;.