Document Maps JavaScript API Reference Manual Point Marker Class LabelMarker

LabelMarker

AMap.LabelMarker

Annotation class, supports drawing a large number of point annotations and text annotations, based on the AMap.LabelsLayer layer. Construct a mass annotation object, set the properties of the point marker object through LabelMarkerOptions

new AMap.LabelMarker(opts: LabelMarkerOptions);

Parameter

opt(LabelMarkerOptions)Mass annotation parameters

Attribute

Type

Description

name

string

Annotation name, used as an identifier for the annotation, not the final text content displayed on the map, to display text content please set opts.text.content

position

Vector2 | LngLat

The location where the annotation points are displayed on the map, supports passing longitude and latitude objects or a one-dimensional array composed of longitude and latitude, such as: AMap.LngLat(77.19,28.59) or [77.19,28.59]

zooms

Vector2

The level range of the annotation points display, only visible when the zoom level is within the specified range, not displayed beyond the range. Optional value: [2, 20]

opacity

number

Marker opacity, range [0 ~ 1], 1 means completely opaque, 0 means completely transparent

rank

number

Avoidance priority, the priority of obtaining markers. This priority is used when labelsLayer supports avoidance, and markers with higher rank values will avoid markers with lower rank values. Default value: 1

zIndex

number

The display hierarchy of markers within the same LabelsLayer, the larger the number, the more to the front, default value: 1

visible

boolean

Whether the marker is visible, default value: true

extData

any

User-defined properties, supporting any data type of the JavaScript API. Custom data can be stored on this property for easy subsequent operations

icon

IconOptions

Marker Icon Settings

image

string

The image capture address of the icon (accessible icon URL string)

size

Vector2 | Size

Icon size, default value: [36, 36]

clipOrigin

Vector2 | Pixel

The offset of the icon image capture. When a large image is specified, the specified range of the icon can be displayed by combining size and clipOrigin, which is equivalent to the CSS background-position property, default value: [0, 0]

clipSize

Vector2 | Size

The cropping size of the image where the icon is located, if not set, the image size is used

offset

Vector2 | Pixel

The offset of the icon image display position, default value is [0,0], after specifying the position of the annotation point, the default is to use the top-left corner of the annotation point as the reference point (if anchor is set, then anchor is used). To accurately align a specific position of the annotation point to the given position, you need to set an appropriate offset based on the size of the icon image. If you have any questions about this attribute, it is recommended to consult the related tutorials

anchor

Vector2 | Pixel | string

Set the anchor point orientation of the icon, default: 'top-left'. Options: '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 the related tutorial

text

TextOptions

Text label settings

content

string

The content of the text label, which is the text directly displayed on the label

direction

string

The orientation of the text label. If an icon is set, the direction is the offset centered on the icon; if no icon is set, it is relative to the position. Options: 'top'|'right'|'bottom'|'left'|'center'. Default: right

offset

Pixel|Vector2

As the offset, it is based on the direction. Default value: [0, 0]

zooms

Vector2

Text display level range, can be set individually for text display range

style

TextStyleOptions

Text style settings

fontSize

number

Text size, default value: 12

fillColor

string|array<number>

Text color, for example:"#FF0000" or [255, 0, 0]

strokeColor

string|array<number>

Text stroke color, for example: "#FF0000" or [255, 0, 0]

padding

string | array<(string | number)

Text padding. Default value: [3, 3, 3, 3]

backgroundColor

string|array<number>

Text background color

borderColor

string|array<number>

Text background stroke color

borderWidth

number

Text background stroke thickness

fold

boolean

Whether the text should be wrapped (6 characters per line), default value: false

Demo

// Create a LabelMarker instance
var labelMarker = new AMap.LabelMarker({
  position: [116.468599, 39.995847],
  opacity: 1,
  zIndex: 2,
  icon: {
    image: "https://a.amap.com/jsapi_demos/static/images/poi-marker.png",
    anchor: "bottom-center",
    size: [25, 34],
    clipOrigin: [459, 92],
    clipSize: [50, 68],
  },
  text: {
    content: "香猪坊",
    direction: "right",
    style: {
      fontSize: 15,
      fillColor: "#fff",
      strokeColor: "rgba(255,0,0,0.5)",
      padding: [3, 10],
      backgroundColor: "yellow",
      borderColor: "#ccc",
      borderWidth: 3,
    },
  },
});
// Create a LabelsLayer instance to host LabelMarker, [LabelsLayer Documentation](https://lbs.amap.com/api/jsapi-v2/documentation#labelslayer)
var labelsLayer = new AMap.LabelsLayer({
  collision: true,
});
// Add the LabelMarker instance to the LabelsLayer
labelsLayer.add(labelMarker);
// Add the LabelsLayer to the map
map.add(labelsLayer);

Method

getName()

Get the name of the annotation as the annotation identifier, not the final text displayed on the map

return value: (string | undefined) If a label is set, return the label name; if not, return undefined

Demo:

labelMarker.getName();

setName(name)

Set the name of the annotation as an identifier, not the text content displayed on the map

Parametername (string) Name of the annotation point

Demo:

labelMarker.setName("Annotation");

getPosition()

Get the display position of the annotation

return value: (object) Location information of the annotation point

Demo:

labelMarker.getPosition();

setPosition(position)

Set the position of the annotation

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

Demo:

labelMarker.setPosition([116.397428, 39.90923]);

getZooms()

Get annotation display level range

return value: (Vector2 | undefined) Annotation point display level range

Demo:

labelMarker.getZooms();

setZooms(zooms)

Set display level range

Parameterzooms ([number]Displayable level range of the marker

Demo:

labelMarker.setZooms([10,12]);

getOpacity()

Get the value of the marker's transparency

return value: (number | undefined) Value of the marker's transparency

Demo:

labelMarker.getOpacity();

setOpacity(opacity)

Set the transparency of the marker, range [0 ~ 1], where 1 means completely opaque and 0 means completely transparent

Parameteropacity (number) Transparency of the marker

Demo:

labelMarker.setOpacity(0.5);

getzIndex()

Get the stacking order of label points

return value: (number | undefined) Stacking order of label points

Demo:

labelMarker.getzIndex()

setzIndex(zIndex)

Set the stacking order of label points

ParameterzIndex(number) Set the stacking order

Demo:

labelMarker.setzIndex(99);

getRank()

Get the priority of labels, which is used when the labelsLayer supports collision avoidance. Labels with higher rank values will avoid those with lower rank values

return value: (number) The priority of the label point

Demo:

labelMarker.getRank();

setRank(rank)

Set the label priority, which is used when the labelsLayer supports avoidance, and labels with a higher rank value will avoid labels with a lower rank value

Parameterrank (number) Set the label point priority

Demo:

labelMarker.setRank(100);

getText()

Get the label text settings, including text content, anchor position, style, etc

return value: (LabelMarkerTextOptionsIf the label text is set, the label text settings are returned; if not, null is returned

Demo:

labelMarker.getText();

setText(textOpts)

Set the text of the annotation, which can set the displayed text content and text style, etc

ParametertextOpts (LabelMarkerTextOptions) The textOpts parameter configuration refers to the text attribute settings

Demo:

labelMarker.setText({
  content: "Annotation Points",
});

getIcon()

Get the icon settings of the annotation, including the URL, size, etc. of the icon

return value: (LabelMarkerIconOptions) If the annotation icon is set, return the icon settings of the annotation; if not, return null

Demo:

labelMarker.getIcon();

setIcon(iconOpts)

Set the icon of the annotation, which can set the displayed annotation image

ParametericonOpts (LabelMarkerIconOptionsiconOpts Parameter Configuration Reference for Icon Attribute Settings

Demo:

labelMarker.setIcon({
  image: "https://a.amap.com/jsapi_demos/static/images/poi-marker.png",
});

getOptions()

Retrieve All Attribute Configurations for Annotations

return value: (LabelMarkerOptionsAll Attribute Configurations for Annotations

Demo:

labelMarker.getOptions();

getExtData()

Get User Custom Attributes

return value: (any | undefined) The value set for the extData property, null if not set

Demo:

labelMarker.getExtData();

setExtData(extData)

Set user-defined properties

ParameterextData (anyUser-defined data, supports any data type in the JavaScript API

Demo:

labelMarker.setExtData({id:1});

setTop(isTop)

Whether to set the top annotation

ParameterisTop(booleanWhen isTop is set to true, it indicates that the annotation will be displayed before all other annotations; when set to false, the annotation will be displayed in the normal order

Demo:

labelMarker.setTop(true);

getVisible()

Get the visibility of the annotation

return value: (boolean | undefined) Visibility of the annotation

Demo:

labelMarker.getVisible();

setVisible()

Set the visibility of the annotation

Parameter(boolean) Visibility of the annotation

Demo:

labelMarker.setVisible(true);

getCollision()

Get whether the annotation is avoided

return value: (boolean | undefined) Whether it is avoided

Demo:

labelMarker.getCollision();

show()

Annotation Display

Demo:

labelMarker.show();

hide()

Annotation hidden

Demo:

labelMarker.hide();

remove()

Remove itself from the annotation layer

Demo:

labelMarker.remove();

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

Parameter 

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:

const labelsLayer = new AMap.LabelsLayer({}); // Create LabelsLayer layer
map.add(labelsLayer); // Add the LabelsLayer layer to the map
//Create LabelMarker
AMap.plugin("AMap.MoveAnimation", function () {
  // After loading the AMap.MoveAnimation plugin, create a LabelMarker instance
  const animationLabel = new AMap.LabelMarker({
    content: "Annotation",
    position: new AMap.LngLat(77.01486, 28.602616),
    text: {
      content: "Animated Annotation",
    },
  });
  labelsLayer.add(animationLabel); // Add Annotation Points to LabelsLayer
  // Call moveTo Method
  animationLabel.moveTo([77.299132, 28.573676], {
    duration: 1000, // 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. 

Demo:

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:

const labelsLayer = new AMap.LabelsLayer({}); // Create LabelsLayer
map.add(labelsLayer); // Add the LabelsLayer to the map
// 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 LabelMarker instance
  const animationLabel = new AMap.LabelMarker({
    content: "Annotation",
    position: new AMap.LngLat(77.176909, 28.619495),
    text: {
      content: "Animated annotation",
    },
  });
  labelsLayer.add(animationLabel); // Add annotation points to the LabelsLayer
  animationLabel.moveAlong(customData); // Invoke the moveAlong method
});

startMove()

Start point marker animation, point markers created after loading AMap.MoveAnimation can be used

Demo:

animationLabel.startMove();

stopMove()

Stop point marker animation, point markers created after loading AMap.MoveAnimation can be used

Demo:

animationLabel.stopMove();

pauseMove()

Pause point marker animation, point markers created after loading AMap.MoveAnimation can be used

Demo:

animationLabel.pauseMove();

resumeMove()

Restart the point marker animation, point markers created after loading AMap.MoveAnimation can be used

Demo:

animationLabel.resumeMove();

Event

Event Name

Description

click

Left 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

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