# Text
Text creates a text component using div and internally uses the Marker object.
# Type Definitions
/**
* Create text component.
*
* @param {Object} [options]
* @param {object} [options.text] Text content
* @param {object} [options.style] Text style
* @param {HTMLElement} [options.element] DOM element to use as text.
* @param {string} [options.anchor='center'] A string indicating which part of the marker should be placed closest to the coordinates. Options are "center", "top", "bottom", "left", "right", "top-left", "top-right", "bottom-left", and "bottom-right".
* @param {PointLike} [options.offset] Offset in pixels relative to element center. Negative values indicate left and up.
* @param {string} [options.color='#3FB1CE'] If `options.element` is not provided.
* @param {number} [options.scale=1] Scale for default text if `options.element` is not provided.
* @param {boolean} [options.draggable=false] A boolean indicating whether the text can be dragged to a new position on the map.
* @param {number} [options.clickTolerance=0] Maximum pixels the mouse pointer can move during a click to be considered a valid click (as opposed to text drag). Default inherits map's `clickTolerance`.
* @param {number} [options.rotation=0] Rotation angle of text relative to its "rotationAlignment" setting (in degrees). Positive values rotate text clockwise.
* @param {string} [options.pitchAlignment='auto'] `map` aligns `Text` with map plane. `viewport` aligns `Text` with viewport plane. `auto` matches `rotationAlignment` value.
* @param {string} [options.rotationAlignment='auto'] `map` aligns `Text` rotation relative to map, maintaining orientation when map rotates. `viewport` aligns `Text` rotation relative to viewport, independent of map rotation. `auto` equals `viewport`.
* @example
* // Create a new Text.
* const text = new vjmap.Text({text: "abc"})
* .setLngLat([30.5, 50.5])
* .addTo(map);
* @example
* // Set text options.
* const text = new vjmap.Text({
* text: "abc",
* draggable: true
* }).setLngLat([30.5, 50.5])
* .addTo(map);
*/
class Text extends Evented {
private readonly _marker;
_textContainerDom: HTMLElement;
_style: object;
/**
* Constructor
* @param options
*/
constructor(options?: {
/** Text content. */
text?: string;
/** Text style. */
style?: object;
/** DOM element for marker. Default is light blue, droplet-shaped SVG marker */
element?: HTMLElement | undefined;
/** Offset in pixels as PointLike object relative to element center. Negative values indicate left and up.*/
offset?: PointLike | undefined;
/** A string indicating which part of the marker should be placed closest to the anchor via Marker.setLngLat.
* Options are `'center'`, `'top'`, `'bottom'`, `'left'`, `'right'`, `'top-left'`, `'top-right'`, `'bottom-left'`, and `'bottom-right'`.
* The default value is `'center'`
*/
anchor?: Anchor | undefined;
/** Default marker color if options.element is not provided. Default is light blue (#3FB1CE) */
color?: string | undefined;
/** A boolean indicating whether the marker can be dragged to a new position on the map. Default is false */
draggable?: boolean | undefined;
/**
* Maximum pixels the mouse pointer can move during a marker click to be considered a valid click
* (as opposed to marker drag). Default (0) inherits map's clickTolerance.
*/
clickTolerance?: number | null | undefined;
/** Marker rotation angle relative to "rotation alignment" setting (degrees). Positive values rotate marker clockwise.
* The default value is 0.
*/
rotation?: number | undefined;
/** map aligns Marker rotation relative to map, maintaining orientation when map rotates.
*`viewport` aligns Marker rotation relative to viewport, independent of map rotation.
*`auto` equals `viewport`.
*/
rotationAlignment?: Alignment | undefined;
/** map aligns Marker with map plane.
* `viewport` aligns Marker with viewport plane.
* `auto` matches `rotationAlignment` value`
* The default value is `auto`.
*/
pitchAlignment?: Alignment | undefined;
/** Default marker scale if options.element is not provided.
* Default scale (1) corresponds to height "41px" and width "27px"`.
*/
scale?: number | undefined;
});
/**
* Attach `Text` to `Map` object.
*
* @param {Map} map to add the text to.
* @returns {Text} Returns itself to allow for method chaining.
* @example
* const text = new vjmap.Text({text: "abc"})
* .setLngLat([30.5, 50.5])
* .addTo(map); // add the text to the map
*/
addTo(map: Map): Text;
/**
* Remove text from map.
*
* @example
* const text = new vjmap.Text().addTo(map);
* text.remove();
* @returns {Text} Returns itself to allow for method chaining.
*/
remove(): Text;
/**
* Get longitude and latitude
* @return {any}
*/
getLngLat(): LngLatLike;
/**
* Set longitude and latitude
* @param lnglat
* @return {this}
*/
setLngLat(lnglat: LngLatLike): Text;
/**
* Return the HTML element of `Text`.
*
* @returns {HTMLElement} Returns the text element.
* @example
* const element = text.getElement();
*/
getElement(): HTMLElement;
/**
* Return the Marker object of `Text`.
*/
getMarker(): Marker;
/**
* Binds a `Popup` to the `Text`
* @param popup
* @return {this}
*/
setPopup(popup?: Popup): Text;
/**
* Return the `Popup` instance bound to `Text`.
*
* @returns {Popup} Returns the popup.
*/
getPopup(): Popup;
/**
* Open or close the `Popup` instance bound to `Text` based on its current state.
*
* @returns {Text} Returns itself to allow for method chaining.
*/
togglePopup(): Text;
/**
* Get text offset.
*
* @returns {Point} The text's screen coordinates in pixels.
*/
getOffset(): number;
/**
* Set text offset.
*
* @param {PointLike} offset Offset in pixels as PointLike object relative to element center. Negative values indicate left and up.
* @returns {Text} Returns itself to allow for method chaining.
*/
setOffset(offset: PointLike): Text;
/**
* Set the `draggable` property and functionality of text.
*
* @param {boolean} [shouldBeDraggable=false] Turns drag functionality on/off.
* @returns {Text} Returns itself to allow for method chaining.
*/
setDraggable(shouldBeDraggable: boolean): Text;
/**
* Return true if text is draggable.
*
* @returns {boolean} True if the text is draggable.
*/
isDraggable(): boolean;
/**
* Sets the `rotation` property of the text.
*
* @param {number} [rotation=0] Rotation angle of text relative to its setting (clockwise, in degrees).
* @returns {Text} Returns itself to allow for method chaining.
*/
setRotation(rotation: number): Text;
/**
* Return the current rotation angle of text (in degrees).
*
* @returns {number} The current rotation angle of the text.
*/
getRotation(): number;
/**
* Set the `rotationAlignment` property of text.
*
* @param {string} [alignment='auto'] Set the `rotationAlignment` property of text.
* @returns {Text} Returns itself to allow for method chaining.
*/
setRotationAlignment(alignment: string): this;
/**
* Return the current `rotationAlignment` property of text.
*
* @returns {string} The current rotational alignment of the text.
*/
getRotationAlignment(): string;
/**
* Set the `pitchAlignment` property of text.
*
* @param {string} [alignment] Set the `pitchAlignment` property of text. If alignment is 'auto', it matches 'rotationAlignment'.
* @returns {Text} Returns itself to allow for method chaining.
*/
setPitchAlignment(alignment: string): Text;
/**
* Return the current `pitchAlignment` property of text.
*
* @returns {string} The current pitch alignment of the text in degrees.
*/
getPitchAlignment(): string;
/**
* Set animation
* @param animationType
* MAP_ANIMATION_NONE No animation
* MAP_ANIMATION_BOUNCE Bounce
* MAP_ANIMATION_DROP Drop
*/
setAnimation(animationType: string): void;
/**
* Show
*/
show(): void;
/**
* Hide
*/
hide(): void;
/**
* Set cursor
* @param cur Cursor name
*/
setCursor(cur: string): void;
/**
* Set text content
* @param txt
*/
setText(txt: string): Text;
/**
* Get text content
*/
getText(): string;
/**
* Set text style
* @param style
*/
setStyle(style?: object): Text;
/**
* Get text style
*/
getStyle(): object;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# Usage
let text = new vjmap.Text({
text: "ChinaABC",
anchor: "top",
draggable: true,
offset: [0, 0], // Pixel offset in x, y direction
style:{ // Custom style
'cursor': 'pointer',
'opacity': 1,
'padding': '12px',
'border-radius': '4px',
'background-color': '#0ff',
'border-width': 0,
'box-shadow': '0px 2px 6px 0px rgba(97,113,166,0.2)',
'text-align': 'center',
'font-size': '14px',
'color': '#F33'
}
});
text.setLngLat(map.getCenter()).addTo(map);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Examples

Add text marker (opens new window)
Scalable DIV text (opens new window)
← SVG Overlay Polyline →