mapbox-gl-js icon indicating copy to clipboard operation
mapbox-gl-js copied to clipboard

Mechanism to access existing controls in map

Open stevage opened this issue 7 years ago • 6 comments

Motivation

I'm trying to implement a utility function to solve #6531: map.U.lockRotation().

Because the navigation control would have already been constructed and added to the map, it's too late to just not add it. Hence I need to find the existing navigation control and either modify it, or remove it then re-add it.

Design Alternatives

  • map.getControl(mapboxgl.NavigationControl) ?

stevage avatar Dec 06 '18 23:12 stevage

@stevage in your design, does mapboxgl.NavigationControl refer to the class/ or an instance of it? How would this work if a control type was able to add multiple instances with different values/options?

Alternatively

  • Extend IControl to include a read-only controlName property that can be set to a fixed name or dynamically - that is up to each individual control implementation.
  • Implement map.getControl(controlName: string) and update map.removeControl(control: string | IControl) to also act on control names.

Want to put up a PR for it?

asheemmamoowala avatar Dec 07 '18 19:12 asheemmamoowala

does mapboxgl.NavigationControl refer to the class/ or an instance of it?

The class. If I had an instance of it, I wouldn't need this function.

How would this work if a control type was able to add multiple instances with different values/options?

Probably the function should always return an array of instances.

I like your alternative suggestion. I can't really see a way, atm, to work out which type of control an element of map._controls[] is.

stevage avatar Dec 07 '18 21:12 stevage

Hi @stevage @asheemmamoowala

I'm in a situation where I particularly need this potential map.getControl(controlName: string) method, would love to know if there's any update on it.

Thanks.

IAMOTZ avatar Mar 15 '21 17:03 IAMOTZ

@IAMOTZ We'd welcome a PR.

asheemmamoowala avatar Mar 15 '21 23:03 asheemmamoowala

You can for some get the name via control.constructor.name

webjay avatar Nov 12 '23 13:11 webjay

Any update please? Need a way to get the existing draw control from mapbox-gl-draw

lymine1996 avatar May 14 '25 18:05 lymine1996