Polygon
AMap.Polygon
Construct a polygon object, specify the polygon style through PolygonOptions
new AMap.Polygon(opts: PolygonOptions)Parameter
opts (PolygonOptions): Polygon parameters
Demo
//Node coordinate array of the polygon contour
var path = [
new AMap.LngLat(99.907748, 14.210075),
new AMap.LngLat(99.945608, 13.444189),
new AMap.LngLat(101.441075, 13.612899),
new AMap.LngLat(101.327495, 14.323209),
];
//Create a Polygon instance
var polygon = new AMap.Polygon({
path: path,
fillColor: "#fff",
strokeWeight: 2,
strokeColor: "red",
});
map.add(polygon);Method
hide()
Hide polygon
Demo:
polygon.hide();show()
Show polygon
Demo:
polygon.show();getExtData()
Get user-defined attributes
return value: (Object) The value set for the extData property, returns an empty object if not set
Demo:
polygon.getExtData();setExtData(extData)
Set user-defined attributes, supporting any data type in JavaScript API
Parameter: extData (Object) User-defined data
Demo:
polygon.setExtData({id:1});getOptions()
Get polygon attributes
return value: (Object) Polygon configuration
Demo:
polygon.getOptions();setOptions(PolygonOptions)
Modify polygon attributes (style, including nodes that form the polygon outline, outline style, etc. For attribute details, refer to the PolygonOptions list)
Parameter: PolygonOptions (Object) Properties of Polygon
Demo:
polygon.setOptions({ fillColor: "#333" });getPath()
Get Polygon Outline Node Array
return value: (Array<LngLat> | Array<Array<LngLat>>) Return Path
Demo:
polygon.getPath();setHeight(height)
Set Polygon Height Above Ground (This method and the related property height are applicable to JSAPI v2.1Beta and above)
Parameter: height (Number) Polygon Height Above Ground
Demo:
polygon.setHeight(0);setExtrusionHeight(extrusionHeight)
Set elevation value
Parameter: extrusionHeight (number) Elevation value
Demo:
polygon.setExtrusionHeight(0);getExtrusionHeight()
Get elevation value
return value: (number) Elevation value
Demo:
polygon.getExtrusionHeight();getBounds()
Obtain a rectangular range that can completely contain the given polygon, with the latitude and longitude values of the southwest and northeast corners of the rectangle completely enclosing the given polygon
return value: (Bounds) Rectangular extent that fully contains the polygon (latitude and longitude coordinates of the southwest and northeast corners)
Demo:
polygon.getBounds();destroy()
Destroy Memory - Polygon
Demo:
polygon.destroy();getArea(isTerrain)
Get the area of the polygon (unit: square meters)
Parameter: isTerrain (Boolean) Specifies whether to measure area based on terrain. If set to true, the area calculation will consider terrain undulations; if set to false or not set, it will default to measuring area on a flat plane. Default value: false
return value: (Bounds) Area Value
Demo:
polygon.getArea(false);contains(originPoint)
Determine whether the coordinate is inside the polygon
Parameter: originPoint (Vector2 | LngLat) The coordinate point to be judged, such as: AMap.LngLat(116.39,39.9) or [116.39, 39.9]
return value: (boolean) true includes, false excludes
Demo:
polygon.contains(new AMap.LngLat(116.39, 39.9));setPath(path?)
Set the array of node coordinates for the polygon outline. Supports a single regular polygon ([lnglat, lnglat, ...]), a single polygon with holes ([[lnglat, lnglat, ...], [lnglat, lnglat, ...]]), and multiple polygons with holes ([[lnglat, lnglat, ...], [lnglat, lnglat, ...], [lnglat, lnglat, ...], ...])
Parameter: path (Array<LngLat> | Array<Array<LngLat>> ) Array of node coordinates for the polygon outline
Demo:
var path = [
new AMap.LngLat(116.368904, 39.913423),
new AMap.LngLat(116.387271, 39.912501),
new AMap.LngLat(116.398258, 39.9046),
];
polygon.setPath(path);getPlaneHeight()
Obtain the absolute elevation value of the off-ground polygon plane (This method is applicable to JSAPI v2.1Beta and above)
return value: (number) Elevation value of the polygon plane
Demo:
polygon.getPlaneHeight();Event
For event object property descriptions, go to:MapsEvent