Skip to main content

Control

A thing attached to the map. Anything positioned on a map is a control: a button, a logo, a legend, a message that appears when a search finds nothing.

addCustomControl() puts an element on the map and that is all it does — it gives you back no handle on what it added, so nothing can be moved or taken off again. Control owns that lifecycle, and is the base class to extend when writing a control of your own.

It decides nothing about how a control looks. Every class name, every piece of content and every attribute comes from you, and no stylesheet ships with the library.

const legend = G.control({
element: '.js-mapLegend',
map: myMap,
position: G.ControlPosition.RIGHT_TOP,
});

// Later
legend.remove();

Use Button instead if the control responds to clicks.

Control options​

Type ControlOptions.

OptionTypeDefaultDescription
attributesobjectAttributes to set on the element when it's built. { 'data-tip': 'Go home' }
classNamestringClass name(s) for the element when it's built.
contentstring | HTMLElement | FunctionThe contents when the element is built. An HTML string, an element to append, or a function returning either.
elementHTMLElement | stringAn existing element, or a selector for one. When this is set nothing is built and the element is used exactly as it is, so the other building options are ignored.
indexnumber0The order among the controls at the same position. Lower numbers come first.
mapMapThe map to attach to. It can be attached later with addTo() instead.
positionControlPositionBLOCK_START_INLINE_STARTWhere the control goes on the map.
tagstringdivThe tag to build the element from.

Building or wrapping​

Two ways to get the element. Either the library builds one from tag, className, content and attributes:

G.control({
className: 'MapBtn MapBtn-geo',
content: '<svg class="Icon"><use xlink:href="#icon-location" /></svg>',
tag: 'button',
});

Or you pass one that already exists, which is usually what you want for markup rendered by the server:

G.control({ element: '.js-mapLegend' });

An element you pass is used as it is. Nothing is added to it and nothing is taken off.

Properties​

PropertyTypeDescription
elementHTMLElementThe element for the control. Read only.
indexnumberThe order among the controls at the same position. Google reads this when it lays the controls out, so changing it only takes effect for a control that hasn't been added yet, or one that's removed and added again.
isAttachedbooleanWhether the control is on a map. Read only.
mapMap | undefinedThe map the control is attached to. Read only.
positionControlPositionWhere the control is displayed. Setting it moves a control that's already attached.

Events​

EventDescription
addThe control was attached to a map.
removeThe control was taken off a map.

Methods​

addTo​

addTo(map: Map): Control

Attach the control to a map. This works before the map has been rendered — the map holds the control until it renders and then adds it.

A control that is already on another map is taken off that one first, so the same control can't end up on two maps.

remove​

remove(): Control

Take the control off the map. The element is left in place rather than destroyed, so the control can be added again, and so that an element you supplied is still yours afterwards.

Writing your own control​

Extend it. The subclass gets the element, the position, the ordering and the attach/remove lifecycle, and adds whatever it's for.

class ZoomToHome extends G.Control {
constructor(options) {
super({ tag: 'button', ...options });
this.element.addEventListener('click', () => {
this.map.setCenter(options.home);
});
}
}

Button is the worked example — it adds click handling and state on top of this class and nothing else.

See the plugin guide for how to publish one.