Document Maps JavaScript API Reference Manual Point Marker Class Marker

Marker

AMap.Marker

Construct a point marker object and set the properties of the point marker object through MarkerOptions

new AMap.Marker(options: MarkerOptions)

Parameter

options (MarkerOptions)Point marker parameters

Attribute

Type

Description

map

Map

The map object to display this marker

position

Vector2 | LngLat

The position where the point marker is displayed on the map. It 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].

icon

Icon | string

The icon displayed in the point marker. The icon URL can be directly passed in, such as: "//vdata.amap.com/icons/b18/1/2.png". You can also pass in an Icon object for configuration, refer to AMap.Icon for specific configurations. If you have questions about this property, it is recommended to consult the related tutorials. Note that this property will be invalid when a valid content is set

content

string | HTMLElement

The content displayed in the marker, the value is a user-defined DOM node or an HTML fragment of a DOM node. If you have questions about this property, it is recommended to consult the relevant tutorial. When content is valid, the icon property will be overridden.

title

string

When the mouse hovers over the marker, text prompts will be displayed. If not set, there will be no text prompt

visible

boolean

Whether the marker is visible, default value: true

zIndex

number

The stacking order of markers, when multiple markers overlap on the map, a higher zIndex value will make the marker display on the top, default value: 12

offset

Vector2 | Pixel

The offset of the marker's display position, default value: [0,0]. After specifying the marker's position, the top-left corner of the marker is used as the reference point by default (if anchor is set, the anchor is used as the reference point). To precisely align a specific position of the marker to the given position, an appropriate offset should be set according to the size of the marker. If you have questions about this property, it is recommended to consult the relevant tutorial

anchor

string | Vector2

Set the anchor position of the point marker, default value: 'top-left'. 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 the relevant tutorial

angle

number

The rotation angle of the point marker, widely used to change the direction of vehicle movement. Default value: 0

clickable

boolean

Control whether the point marker can be clicked. When set to true, users can click the marker and trigger related events (such as click events). If set to false, the marker cannot be clicked. Default value: true

draggable

boolean

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

bubble

boolean

Whether the event bubbles, default value: false

zooms

Vector2

The hierarchy range of the point marker display, only displayed when the zoom level is within the specified range, not displayed outside the range. Default value: [2, 20]

cursor

string

Specify the style when the mouse hovers over the point marker. Custom styles should conform to the CSS cursor property specification. IE only supports cur/ani/ico formats, Opera does not support custom cursor, default value: 'pointer'

topWhenClick

boolean

When the mouse clicks on the marker, whether to place it on the top layer for highlighting. Default value: false, not on top. If set to true, when the marker is clicked, it will be placed above other markers to be more prominently displayed to the user

height

number

Set whether the marker point is drawn above ground, default value: 0, when equal to 0, it is drawn on the ground. When greater than 0, it is drawn above ground, and the height of the marker point is equal to the height value plus the elevation value of the marker point (this new attribute in JSAPI v2.1 is currently only applicable to version 2.1)

label

object

Add text label

content

string

The content of the text annotation

offset

Pixel | Vector2 | Array<number>

The offset of the text annotation, default value: [0,0], which defaults to the right ('right') direction of the marker as the reference point. If direction is set, the offset is based on that direction

direction

string

Text annotation direction optional values: 'top'|'right'|'bottom'|'left'|'center', default value: 'right'

extData

any

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

Demo

var marker = new AMap.Marker({
  position: new AMap.LngLat(77.193045, 28.59086),
  icon: "https://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
  anchor: "bottom-center",
});
map.add(marker);

Method

getTitle()

Get the text hint of the point marker

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

Demo:

marker.getTitle();

setTitle(title)

Set the text hint when hovering over the point marker

Parametertitle(string) Text hint for text marker

Demo:

marker.setTitle("Text hint");

getHeight()

Get the height of Marker above the ground (This method applies to JS API v2.1Beta and above)

return value: (number | undefined) The value of marker's altitude above the ground

Demo:

marker.getTitle();

getIcon()

Get Icon content

return value: (Icon | string | undefined) The value set for the icon property can be a String type icon address or an Icon object, if the icon property is not set, it returns undefined

Demo:

marker.getIcon();

setIcon(icon)

Set the display icon of the point marker, if a valid content is set, the icon will not take effect

Parametericon(Icon | string) The icon displayed in the point marker can be a String type icon address or an Icon object

Demo:

marker.setIcon("//vdata.amap.com/icons/b18/1/2.png");

getLabel()

Get the content of the point marker text label

return value: (LabelOptions) Settings for the label (text label), if the label property is not set, an empty object is returned

Demo:

marker.getLabel();

setLabel(opts)

Set the content of the point marker text label

Parameteropts(LabelOptions) Settings for the label (text label)

Demo:

marker.setLabel({content:'点标记',direction:'bottom',offset:[0,5]});

getClickable()

Check if the point marker supports mouse click events

return value: (boolean) Boolean value

Demo:

marker.getClickable();

setClickable(clickable)

Set whether the point marker supports mouse click events

Parameterclickable(booleanThe clickable parameter is optional (default is true). When set to false, the point marker does not support mouse click events

Demo:

marker.setClickable(true);

getDraggable()

Get whether the point marker object is draggable

return value: (boolean) Boolean value

Demo:

marker.getDraggable();

setDraggable(draggable)

Set whether the point marker object is draggable

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

Demo:

marker.setDraggable(true);

getTop()

Get whether the point marker is brought to the top

return value: (boolean) Boolean value

Demo:

marker.getTop();

setTop(isTop)

Set whether to bring the point marker to the top when there are multiple markers on the map

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

marker.setTop(true);

getCursor()

Get the cursor settings when the mouse hovers

return value: (string) The currently set cursor type

Demo:

marker.getCursor();

setCursor(cursor)

Set the cursor when the mouse hovers

Parametercursor(stringThe string specifying 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:

marker.setCursor("crosshair");

getExtData()

Get user-defined data

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

Demo:

marker.getExtData();

setExtData(extData)

Set user-defined data

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

Demo:

marker.setExtData({id:1});

remove()

Remove the marker

Demo:

marker.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

autoRotation

boolean

Whether the marker direction rotates along the path, default: true

Demo:

AMap.plugin("AMap.MoveAnimation", function () {
  // After loading the AMap.MoveAnimation plugin, create a Marker instance
  const animationMarker = new AMap.Marker({ 
    position: new AMap.LngLat(77.01486, 28.602616),
    icon: "https://a.amap.com/jsapi_demos/static/demo-center-v2/car.png",
    offset: new AMap.Pixel(-13, -26),
  });
  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

autoRotation

boolean

Whether the marker direction rotates along the path, default: true

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()

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

Demo:

animationMarker.startMove();

stopMove()

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

Demo:

animationMarker.stopMove();

pauseMove()

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

Demo:

animationMarker.pauseMove();

resumeMove()

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

Demo:

animationMarker.resumeMove();

getMap()

Get the map instance of the overlay

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:

marker.getMap();

setMap(map)

Set the overlay onto the map

Parametermap(Map | nullIt can 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:

marker.setMap(map);

getMap()

Get the map instance of the overlay

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:

marker.getMap();

addTo(map)

Set the overlay onto the map

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

Demo:

marker.addTo(map);

add(map)

Add overlay to the map

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

Demo:

marker.add(map);

show()

Show overlay

Demo:

marker.show();

hide()

Hide overlay

Demo:

marker.hide();

getPosition()

Get overlay position

return value: (object) Location information of the overlay

Demo:

marker.getPosition();

setPosition(position)

Set the position of the overlay

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

Demo:

marker.setPosition([116.397428, 39.90923]);

getAnchor()

Get the anchor position of the overlay on the map

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

Demo:

marker.getAnchor();

setAnchor(anchor)

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

Parameteranchor(string) Specify the anchor position of the overlay

Demo:

marker.setAnchor('center');

getOffset()

Get overlay offset

return value: (Vector2 | Pixel | undefined | Array<number>) Overlay offset

Demo:

marker.getOffset();

setOffset(offset)

Set overlay offset

Parameteroffset(Vector2 | Pixel) The value of the point marker offset, note: the offset defaults to the top-left corner of the marker (if an anchor is set, then the anchor is used). To accurately align a specific position of the marker to a given position, an appropriate offset needs to be set based on the dimensions of the marker. If you have any questions about this property, it is recommended to consult the relevant tutorial.

Demo:

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

getAngle()

Get overlay rotation angle

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

Demo:

marker.getAngle();

setAngle(angle)

Set overlay rotation angle

Parameterangle(number) Set rotation angle

Demo:

marker.setAngle(5);

getSize()

Get the size of the overlay

return value: (Vector2) An array containing width and height properties

Demo:

marker.getSize();

setSize(size)

Set the size of the overlay

Parametersize(Vector2 | Size) Overlay size

Demo:

marker.setSize([30,30]);

getzIndex()

Get the overlay order of the overlay

return value: (number | undefined) Overlay order of the overlay

Demo:

marker.getzIndex()

setzIndex(zIndex)

Set the overlay order of the overlay

ParameterzIndex(number) Set overlay order

Demo:

marker.setzIndex(99);

getOptions()

Get all properties of the overlay

return value: (OverlayOptions) Object containing all configuration properties of the overlay

Demo:

marker.getOptions()

getContent()

Get the custom content displayed by the point marker

return value: (string | HTMLElement | undefined) If the point marker has the content attribute set, an object containing the custom content will be returned; otherwise, an empty string will be returned

Demo:

marker.getContent()

setContent(content)

Set the custom content displayed by the point marker, which can be a user-defined DOM node or an HTML fragment of a DOM node

Parametercontent(HTMLDOM | string) User-defined DOM node or HTML fragment of a DOM node. If you have questions about this parameter, it is recommended to consult the related tutorial. When the content is valid, the icon attribute will be overwritten

Demo:

marker.setContent(`<div style="width:25px; height:34px;">
<img src="//a.amap.com/jsapi_demos/static/demo-center/icons/dir-via-marker.png" style="width:100%; height:100%;">
</div>`);

getBounds()

Get point marker range

return value: (Bounds) Point Marker Location Range

Demo:

marker.getBounds()

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