Document Maps JavaScript API Introductory Tutorial Add a polygon

Add a polygon

Polygon vector graphics are one of the important map elements of map overlays and can be used to highlight the shape of a region. There are various types of vector graphic overlays, and you can choose the appropriate type according to your needs. This article describes how to add an interactive polygon AMap.Polygon to the map surface.

Example of polygon drawing

1、Add vector polygon

1

1.1 Create a map

const map = new AMap.Map("container", {
  center: [77.193045, 28.59086], 
  showOversea: true, //Enable world Map
  zoom: 8.8, 
});
2

1.2 Create Polygon object

The AMap.Polygon object provides users with the ability to draw polygons on the map surface. Properties such as fillColorstrokeColor, and outline style can be set for the polygon.

const pathArr = [
  [
    [
      [76.989798, 28.659269],
      [76.969885, 28.603219],
      [77.081808, 28.548949],
      [77.193732, 28.615275],
      [77.137427, 28.694209],
    ],
  ],
];
const polygon = new AMap.Polygon({
  path: pathArr, 
  fillColor: "#ccebc5", 
  strokeOpacity: 1, 
  fillOpacity: 0.5, 
  strokeColor: "#2b8cbe", 
  strokeWeight: 1, 
  strokeStyle: "dashed", 
  strokeDasharray: [5, 5], 
});
Prompt

path is the node coordinate array of the polygon contour. The polygon supports the drawing of ordinary polygons, polygons with a single hole, and polygons with multiple holes.

 

Illustration of the contour coordinate array for various types of polygons: (assuming lnglat = new AMap.LngLat(lng, lat) or lnglat = [lng, lat])

Regular polygon:path = [lnglat, lnglat ...] or path = [[lnglat, lnglat ...]]
Polygon with a single hole:path = [[lnglat, lnglat ...], [ lnglat, lnglat ...]]
Polygon with multiple holes:path = [[lnglat, lnglat ...], [lnglat, lnglat ... ], [lnglat, lnglat ...] ...] 
3

1.3 Add events to Polygon

Add mouseover and mouseout events to the Polygon instance in the example.

//Change style on mouse hover
polygon.on("mouseover", () => {
  polygon.setOptions({
    fillOpacity: 0.7, //Polygon Fill Transparency
    fillColor: "#7bccc4",
  });
});
//Restore style on mouse out
polygon.on("mouseout", () => {
  polygon.setOptions({
    fillOpacity: 0.5,
    fillColor: "#ccebc5",
  });
});
4

1.4 Add Polygon instance to the map

map.add(polygon)

2、Description of properties and methods involved in this chapter

2.1 AMap.Polygon

parameters/methods

Description

Type

Description description

Default

fillColor

Polygon fill color

String

Assign using hexadecimal color codes

'#00B2D5'

strokeWeight

contour line width

Number

-

2

strokeColor

Line color

String

Assign using hexadecimal color codes

'#00D3FC'

fillOpacity

Polygon fill transparency value range 

Number

The value range is [0,1], where 0 indicates complete transparency and 1 indicates opacity.

0.5

strokeOpacity

Outline transparency

Number

The range is [0,1], where 0 means completely transparent and 1 means opaque

0.9

strokeStyle

Contour line style

String

Solid line:"solid" 

Dashed line:"dashed"

"solid" 

strokeDasharray

The style of the dashed lines and gaps outlining the shape is effective when strokeStyle is dashed and works in ie9+ browsers

Array<number>

See the reference manual

-

setOptions()

Methods for modifying polygon attributes, such as style features, including nodes that form the polygon outline, outline style, etc.

Function

-

-