Document Maps JavaScript API Reference Manual Point Marker Class Text

Text

AMap.Text

Construct a text marker object, set the properties of the text marker object through TextOptions

new AMap.Text(opts: TextOptions);

Parameter

optsTextOptionsText marker parameters

Attribute

Type

Description

map

Map

The map object on which to display the text marker

position

Vector2 | LngLat

The position where the text marker is displayed on the map, supports passing in a longitude and latitude object or a one-dimensional array of longitude and latitude, such as: AMap.LngLat(77.19,28.59) or [77.19,28.59]

text

LabelOptions

The text content to be displayed on the marker

title

string

The text tip displayed when the mouse hovers over the text marker. If not set, there will be no text tip

visible

boolean

Whether the text marker is visible, default value: true

zIndex

number

The stacking order of text markers. When multiple point markers overlap on the map, a higher zIndex value will cause the point marker to be displayed on top. Default value: 12

offset

Vector2 | Pixel

The offset of the text marker's display position, default value: [0,0]. After specifying the position of the text marker, the default reference point is the top-left corner of the text marker (if anchor is set, then anchor is used). To precisely align a specific position of the text marker to the given position, you need to set an appropriate offset based on the size of the text marker. If you have questions about this property, it is recommended to consult related tutorials

anchor

string | Vector2

Set the anchor position of the text marker, default value: 'center'. Optional values: 'top-left','top-center','top-right', 'middle-left', 'center', 'middle-right', 'bottom-left', 'bottom-center', 'bottom-right'. If you have questions about this property, it is recommended to consult related tutorials

angle

number

The rotation angle of the text marker (Note: the angle property is implemented using CSS3, supported in IE9 and above). Default value: 0

clickable

boolean

Control whether the text marker can be clicked. When set to true, users can click on the marker and trigger related events (e.g., click event). If set to false, the marker cannot be clicked. Default value: true

draggable

boolean

Set whether the text marker is draggable, default value: false

bubble

boolean

Whether the event bubbles, default value: false

zooms

Vector2

The level range of text marker display, only visible when the zoom level is within the specified range, otherwise hidden. Default: [2, 20]

cursor

string

Specify the style when the mouse hovers over the text label. Custom styles should conform to CSS cursor property specifications. IE only supports cur/ani/ico formats, and Opera does not support custom cursors

topWhenClick

boolean

When a text label is clicked by the mouse, whether to place it on the top layer to highlight it. Default value: false, not top. If set to true, the text label will be placed above other labels when clicked, to make it more visible to the user

extData

any

User-defined attributes support any data type of JavaScript API, such as the id of a text label, etc. Custom data can be saved on this attribute for convenient use in subsequent operations.

style

object

Set text styles, where Object is the same as a CSS style sheet, e.g., {'background-color':'red'}

Demo

var text = new AMap.Text({
  position: new AMap.LngLat(116.397428, 39.90923),
  anchor: "bottom-center",
  text: "Text Label",
  style: { "background-color": "red" },
});
map.add(text);

Method

getText()

Get the text mark content

return value: (string | undefined) Text content set by the text attribute

Demo:

text.getText();

setText()

Set the text mark content

Parametertext (string | number) Content of the text mark

Demo:

text.setText("Text Label");

setStyle()

Modify the text mark style. Object is the same as CSS style sheet, such as:{'background-color':'red'}

Parameterstyle (object) Text marker style

Demo:

text.setStyle({'background-color':'red'});

getTitle()

Get the text prompt of the text marker

return value: (string) The text prompt set by the title attribute, returns an empty string if not set

Demo:

text.getTitle();

setTitle(title)

Set the text prompt when the mouse hovers over the text marker

Parametertitle (string) Text marker's text prompt

Demo:

text.setTitle("Text prompt");

getClickable()

Get whether the text marker supports mouse click events

return value: (boolean) Boolean value

Demo:

text.getClickable();

setClickable(clickable)

Set whether the text marker supports mouse click events

Parameterclickable(booleanThe clickable parameter is optional (default is true), when set to false, the text marker will not support mouse click events

Demo:

text.setClickable(true);

getDraggable()

Check if the text marker object is draggable

return value: (boolean) Boolean value

Demo:

text.getDraggable();

getMap()

Get the map instance of the text marker

return value: (Map | nullIf the overlay has been added to the map, return the map instance where the overlay is located. If the overlay has not been added, has been removed, or does not exist on any map, return null

Demo:

text.getMap();

setMap(map)

Set the text marker on the map

Parametermap(Map | nullCan be a Map object or null. If it is a Map object, the overlay will be added to this map. If it is null or default, the overlay will be removed from the current map

Demo:

text.setMap(map);

addTo(map)

Add a text marker to the map, which is not recommended. It is recommended to use map.add(text)

Parametermap(Map) Map object, the overlay will be added to this map

Demo:

text.addTo(map);

add(map)

Add a text marker to the map

Parametermap(Map) Map object, the overlay will be added to this map

Demo:

text.add(map);

show()

Show text marker

Demo:

text.show();

hide()

Hide text marker

Demo:

text.hide();

getPosition()

Get text marker position

return value: (object) Overlay location information

Demo:

text.getPosition();

setPosition(position)

Set text marker position

Parameterposition(Vector2 | LngLat) Supports passing in a longitude and latitude object or a one-dimensional array composed of longitude and latitude

Demo:

text.setPosition([116.397428, 39.90923]);

getAnchor()

Get the anchor point position of the text marker on the map

return value: (string | Vector2 | undefined) Anchor point position of the overlay

Demo:

text.getAnchor();

setAnchor(anchor)

Set overlay anchor point settings. Default value: 'center', options: 'top-left'|'top-center'|'top-right'|'middle-left'|'center'|'middle-right'|'bottom-left'|'bottom-center'|'bottom-right' 

Parameteranchor(string) Specify the anchor point position of the overlay

Demo:

text.setAnchor('center');

getOffset()

Get text marker offset

return value: (Vector2 | Pixel | undefined | Array<number>) Offset of the overlay

Demo:

text.getOffset();

setOffset(offset)

Set text marker offset, relative to the position after the anchor

Parameter:

offset(Vector2 | Pixel) Value of the text marker offset, note: the offset is by default based on the top-left corner of the text marker (if an anchor is set, it is based on the anchor). To precisely align a specific position of the text marker to the given position, an appropriate offset needs to be set based on the size of the text marker. If you have questions about this property, it is recommended to consult the relevant tutorial

Demo:

text.setOffset(new AMap.Pixel(-13, -30));

getAngle()

Get text marker rotation angle

return value: (number | undefined) Rotation angle of the overlay

Demo:

text.getAngle();

setAngle(angle)

Set the rotation angle of text markers

Parameterangle(number) Set rotation angle

Demo:

text.setAngle(5);

getzIndex()

Get the overlay order of text markers

return value: (number | undefined) Overlay order of text markers

Demo:

text.getzIndex()

setzIndex(zIndex)

Set the overlay order of a text label

ParameterzIndex(number) The set overlay order

Demo:

text.setzIndex(99);

getOptions()

Get all attributes of a text label

return value: (OverlayOptions) An object containing all configuration properties of a text label

Demo:

text.getOptions();

getBounds()

Get the range of a text label

return value: (Bounds) Text marking position range

Demo:

text.getBounds();

moveTo(targetPosition, opts)

Move the marker to the specified location with a given duration/speed, the AMap.MoveAnimation plugin needs to be loaded to use this feature

Demo:

targetPosition(LngLat | Vector2The specified location supports passing in a longitude and latitude object or a one-dimensional array composed of longitude and latitude, such as: AMap.LngLat(77.19, 28.59) or [77.19, 28.59]

opts(MoveToOptionsmoveTo animation parameters

Attribute

Type

Description

duration

number

Duration of each animation segment, unit: ms

easing

EasingCallback

easing timing function

Prompt

If there is a need to rotate a marker along a path, it is recommended to use the moveTo method of the Marker

Demo:

AMap.plugin("AMap.MoveAnimation", function () {
  // After loading the AMap.MoveAnimation plugin, create a Text instance
  const animationMarker = new AMap.Text({
    position: new AMap.LngLat(77.01486, 28.602616),
    anchor: "bottom-center",
    text: "文本标记",
    style: { "background-color": "red" },
  });
  map.add(animationMarker); 
  // Call the moveTo method
  animationMarker.moveTo([77.299132, 28.573676], {
    duration: 2000, // Animation duration
  });
});

moveAlong(path, opts)

The point marker moves along the specified path for a specified duration. After loading AMap.MoveAnimation, JS API 2.0 can support segmented speed and duration settings. 

Parameter:

path(Array<LngLat> | Array<Vector2>Array<Object>) Path array, if in doubt, please refer to the sample code below

opts(ObjectmoveAlong animation parameters optional

Attribute

Type

Description

duration

number | AnimationCallback

Duration of each animation segment, unit: ms

easing

EasingCallback

Easing timing function

circlable

boolean

Whether the animation loops, default: false

delay

number | AnimationCallback

Delay animation duration, unit: ms. If duration is set for each segment, the corresponding delay animation duration will be executed for each segment

aniInterval

number

Interval duration between complete animations

Prompt

If there is a need to rotate a marker along a path, it is recommended to use the moveTo method of the Marker

Demo:

// Define the path
const path = [
  new AMap.LngLat(76.993574, 28.664089),
  new AMap.LngLat(76.992888, 28.621906),
  new AMap.LngLat(77.024474, 28.606836),
  new AMap.LngLat(77.092451, 28.606836),
  new AMap.LngLat(77.155623, 28.598999),
];
// Set the animation duration for each path segment
const customData = [
  { position: path[0], duration: 200 },
  { position: path[1], duration: 400 },
  { position: path[2], duration: 600 },
  { position: path[3], duration: 800 },
  { position: path[4], duration: 1000 },
];
AMap.plugin("AMap.MoveAnimation", function () {
  // After loading the AMap.MoveAnimation plugin, create a Marker instance
  const animationMarker = new AMap.Text({
    position: new AMap.LngLat(77.176909, 28.619495),
    anchor: "bottom-center",
    text: "marker",
    style: { "background-color": "red" },
  });
  // Add the Marker instance to the map
  map.add(animationMarker);
  // Call the moveAlong method
  animationMarker.moveAlong(customData, {
    circlable: true, //The animation starts looping
    aniInterval: 2000, //The interval between each complete animation is 2s
  });
});

startMove()

Enable the text marker animation, available after loading AMap.MoveAnimation

Demo:

animationMarker.startMove();

stopMove()

Stop the text marker animation, available after loading AMap.MoveAnimation

Demo:

animationMarker.stopMove();

pauseMove()

Pause the text marker animation, available after loading AMap.MoveAnimation

Demo:

animationMarker.pauseMove();

resumeMove()

Restart the text marker animation, available after loading AMap.MoveAnimation

Demo:

animationMarker.resumeMove();

setDraggable(draggable)

Set whether the text marker object can be dragged and moved

Parameterdraggable(boolean) The draggable parameter is optional (default is false). When set to true, the point marker object can be dragged

Demo:

text.setDraggable(true);

getTop()

Get whether this text label is placed on top

return value: (boolean) Boolean value

Demo:

text.getTop();

setTop(isTop)

When there are multiple markers on the map, set whether to place this text label on top

ParameterisTop(boolean) The isTop parameter is optional (default is false). When set to true, this marker is placed on top when there are multiple markers on the map

Demo:

text.setTop(true);

getCursor()

Get the cursor settings when the mouse is hovering

return value: (string) The currently set cursor type

Demo:

text.getCursor();

setCursor(cursor)

Set the cursor when the mouse hovers

Parameter:

cursor(stringSpecify a string for the cursor style, custom styles should comply with the CSS cursor property specification. IE only supports cur/ani/ico formats, Opera does not support custom cursors

Demo:

text.setCursor("crosshair");

getExtData()

Retrieve user-defined data

return value: (any | undefined) The value set by the extData property, if not set, returns an empty object

Demo:

text.getExtData();

setExtData(extData)

Set user-defined data

ParameterextData(any) User-defined data, supports any data type of JavaScript API

Demo:

text.setExtData({id:1});

remove()

Remove text marker 

Demo:

text.remove();

Event

Event Name

Description

click

Left Mouse Click Event

dblclick

Left Mouse Double Click Event

rightclick

Right Mouse Click Event

mousemove

Mouse Movement

mouseover

Event triggered when the mouse moves close to a point marker

mouseout

Event triggered when the mouse moves out of a point marker

mousedown

Event triggered when the mouse is pressed on a point marker

mouseup

Event triggered when the mouse is pressed and then released on a point marker

dragstart

Event triggered when starting to drag a point marker

dragging

Event triggered when the mouse drags and moves a point marker

dragend

Event triggered when dragging and moving a point marker ends

moving

When a point marker performs a moveTo or moveAlong animation, a moving event is triggered. This event passes an Object parameter, which contains a property named passedPath, whose value is an Array. This passedPath array records the path traveled by the point marker during the moveAlong or moveTo animation. Example code:

marker.on("moving", function (event) {
  var passedPath = event.passedPath; // Get the array of the path traveled
  // Process passedPath, such as printing it out or using it for other logic
  console.log(passedPath);
  /*Print result: array element 1: starting coordinates (the initial position of the point marker)
                  array element 2: current coordinates (the current position of the point marker)*/
});

moveend

Event triggered when the point marker finishes the moveTo animation, and can also be triggered by the moveAlong method

movealong

Event triggered after the point marker executes the moveAlong animation once

touchstart

Event triggered when touch starts, only applicable to mobile devices

touchmove

Event triggered during touch movement, only applicable to mobile devices

touchend

Event triggered when touch ends, only applicable to mobile devices

For event object property descriptions, go to:MapsEvent