Custom Controls
As you might expect, this same method can be used to add custom controls. The list of issues is similar, with the main difference being that 'import the code' is replaced by 'create the ui'. This topic also covers CSS. A template component.ts
is shown at the bottom of the page and can be copied.
Create Typings
No typings are required for the basic control, since the @types/leaflet definition cover the basic L.Control
class.
Addition typings are only required if you want to add methods to the control, and as discussed above any additional functionality should really be done in the component typescript.
Create the UI
The UI for the control is defined in the HTML for the component (in the standard cae that is ./xxx.component.html
).
There should be an element for the control (usually a DIV) that defines the UI. This is passed to the map when the control is added to the map. This element must have the id that is used in the component code - in the template this is 'custom' but that should be changed to the name of your control.
Create a Directive
This is set in the component. The template would, for instance, create the directive :
Add the Control to the Map
The template implements the same mechanism for adding the control to the map as proposed for existing controls above.
There should not be a need to do anything in addition to the basic template.
Interact with the Control
The scope for interaction with the control is, of course, as large as HTML and Angular allow!
However, there are few points to remember.
Template Code
The basic framework for a new custom control component is shown below.
Example
To see the description of a worked example, see :
The demo code is shown in this stackblitz. Click anywhere on the map to see the control update.
Last updated
Was this helpful?