形状和线条
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以向地图添加各种形状。形状是地图上的一种对象,与 LatLng
坐标系相关联。Maps JavaScript API 中的仿真 3D 地图支持向地图添加线条和多边形。
多段线
要在地图上绘制线条,可以使用多段线。Polyline3DElement
类可定义地图上的线性相连线段叠加层。Polyline
对象包含一组 LatLng
位置,并可创建一系列线段,依照先后次序将这些位置连接起来。
添加多段线
Polyline
构造函数可接受一组 Polyline3DElementOptions
(用于指定线条的 LatLng
坐标)和一组样式(用于调整多段线的视觉行为)。
多段线对象在地图上绘制为一系列直线段。您可以在构建线条时通过 Polyline3DElementOptions
指定线条描边的自定义颜色、宽度、不透明度、高度和几何样式选项,也可以在构建后更改这些属性。多段线支持下列描边样式:
outerColor
:格式为 "#FFFFFF"
的十六进制 HTML 颜色。
outerWidth
:介于 0.0
和 1.0
之间的数值,可解读为 strokeWidth
的百分比。
strokeColor
:格式为 "#FFFFFF"
的十六进制 HTML 颜色。
strokeWidth
:线条的宽度(以像素为单位)。
geodesic
:多边形的边是否遵循地球的曲率,还是绘制为直线。
altitudeMode
:如何解读坐标中的海拔高度分量
drawsOccludedSegments
:一个布尔值,用于指示是否应绘制被对象(例如建筑物)遮挡的多边形部分。
extruded
:一个布尔值,用于指示折线是否应连接到地面。
async function init() {
const { Map3DElement, MapMode } = await google.maps.importLibrary("maps3d");
const map = new Map3DElement({
center: { lat: 37.772675996068915, lng: -122.3978382542777, altitude: 2500 },
tilt: 45,
heading: 5.9743656,
mode: MapMode.HYBRID,
});
const { Polyline3DElement, AltitudeMode } = await google.maps.importLibrary("maps3d");
const polylineOptions = {
strokeColor: "#EA433580",
strokeWidth: 10,
altitudeMode: "ABSOLUTE",
extruded: true,
drawsOccludedSegments: true,
}
const polyline = new google.maps.maps3d.Polyline3DElement(polylineOptions)
polyline.coordinates = [
{lat: 37.80515638571346, lng: -122.4032569467164},
{lat: 37.80337073509504, lng: -122.4012878349353},
{lat: 37.79925208843463, lng: -122.3976697250461},
{lat: 37.7989102378512, lng: -122.3983408725656},
{lat: 37.79887832784348, lng: -122.3987094864192},
{lat: 37.79786443410338, lng: -122.4066878788802},
{lat: 37.79549248916587, lng: -122.4032992702785},
{lat: 37.78861484290265, lng: -122.4019489189814},
{lat: 37.78618687561075, lng: -122.398969592545},
{lat: 37.7892310309145, lng: -122.3951458683092},
{lat: 37.7916358762409, lng: -122.3981969390652}
];
map.append(polyline)
document.body.append(map);
}
init();
交互式多段线
以下示例在注册点击事件后更改了多段线的描边颜色。
async function init() {
const colors = ["red", "blue", "green", "yellow"];
const { Map3DElement, MapMode, Polyline3DInteractiveElement } = await google.maps.importLibrary("maps3d");
const map = new Map3DElement({
center: {lat: 0, lng: -180, altitude: 15000000},
mode: MapMode.HYBRID,
});
document.body.append(map);
const polyline = new Polyline3DInteractiveElement({
coordinates: [
{ lat: 37.772, lng: -122.214 },
{ lat: 21.291, lng: -157.821 },
{ lat: -18.142, lng: 178.431 },
{ lat: -27.467, lng: 153.027 }
],
strokeColor: 'red',
strokeWidth: 10,
});
polyline.addEventListener('gmp-click', (event) => {
polyline.strokeColor = colors[~~(Math.random() * colors.length)];
});
map.append(polyline);
}
init();
多边形
多边形表示由闭合路径(或环路)封闭的区域,由一系列坐标定义。Polygon
对象与 Polyline
对象类似,因为它们都包含一系列有序的坐标。多边形使用描边和填充区绘制而成。
您可以为多边形的边缘(描边)定义自定义颜色和宽度,并为封闭区域(填充区)定义自定义颜色和不透明度。颜色应以十六进制 HTML 格式表示,不支持颜色名称。
Polygon
对象可以描述复杂形状,其中包括:
- 由单个多边形定义的多个不连续区域。
- 带孔的区域。
- 一个或多个区域的交集。
要定义复杂形状,需要使用包含多条路径的多边形。
添加多边形
由于多边形区域可能包含多条单独路径,因此 Polygon
对象的路径属性会指定一个数组的数组,其中每个数组的类型均为 MVCArray
。每个数组都会定义一个单独的有序 LatLng
坐标序列。
对于仅包含一条路径的基本多边形,您可以只使用一个 LatLng
坐标数组来构建 Polygon
。在构建完成后将此数组存储到 outerCoordinates
属性内时,Maps JavaScript API 会将其转换为一个数组的数组。
async function init() {
const { Map3DElement, MapMode } = await google.maps.importLibrary("maps3d");
const map3DElement = new Map3DElement({
center: { lat: 43.6425, lng: -79.3871, altitude: 400 },
range: 1000,
tilt: 60,
mode: MapMode.SATELLITE,
});
const { Polygon3DElement, AltitudeMode } = await google.maps.importLibrary("maps3d");
const polygonOptions = {
strokeColor: "#EA433580",
strokeWidth: 4,
fillColor: "#0000FF80",
altitudeMode: "ABSOLUTE",
extruded: true,
drawsOccludedSegments: true,
}
const towerPolygon = new google.maps.maps3d.Polygon3DElement(polygonOptions);
towerPolygon.outerCoordinates = [
{ lat: 43.6427196, lng: -79.3876802, altitude: 600 },
{ lat: 43.6421742, lng: -79.3869184, altitude: 600 },
{ lat: 43.643001, lng: -79.3866475, altitude: 600 }
];
map3DElement.append(towerPolygon);
document.body.append(map3DElement);
}
init();
交互式多边形
以下示例在注册点击事件后更改了多边形的描边颜色。
async function init() {
const colors = ["red", "blue", "green", "yellow"];
const { Map3DElement, MapMode } = await google.maps.importLibrary("maps3d");
const map3DElement = new Map3DElement({
center: { lat: 43.6425, lng: -79.3871, altitude: 400 },
range: 1000,
tilt: 60,
mode: MapMode.SATELLITE,
});
const { Polygon3DInteractiveElement, AltitudeMode } = await google.maps.importLibrary("maps3d");
const polygonOptions = {
strokeColor: "#EA433580",
strokeWidth: 4,
fillColor: "#0000FF80",
altitudeMode: "ABSOLUTE",
extruded: true,
drawsOccludedSegments: true,
}
const towerPolygon = new google.maps.maps3d.Polygon3DInteractiveElement(polygonOptions);
towerPolygon.outerCoordinates = [
{ lat: 43.6427196, lng: -79.3876802, altitude: 600 },
{ lat: 43.6421742, lng: -79.3869184, altitude: 600 },
{ lat: 43.643001, lng: -79.3866475, altitude: 600 }
];
towerPolygon.addEventListener('gmp-click', (event) => {
towerPolygon.strokeColor = colors[~~(Math.random() * colors.length)];
});
map3DElement.append(towerPolygon);
document.body.append(map3DElement);
}
init();
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-12。
[null,null,["最后更新时间 (UTC):2025-08-12。"],[],[],null,["Select platform: [Android](/maps/documentation/maps-3d/android-sdk/add-polygons \"View this page for the Android platform docs.\") [iOS](/maps/documentation/maps-3d/ios-sdk/add-polygons \"View this page for the iOS platform docs.\")\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| This product or feature is in Preview (pre-GA). Pre-GA products and features might have limited support, and changes to pre-GA products and features might not be compatible with other pre-GA versions. Pre-GA Offerings are covered by the [Google\n| Maps Platform Service Specific Terms](https://cloud.google.com/maps-platform/terms/maps-service-terms). For more information, see the [launch stage\n| descriptions](/maps/launch-stages).\n\n\u003cbr /\u003e\n\nYou can add various shapes to your map. A shape is an object on the map, tied to\na `LatLng` coordinate system. The Photorealistic 3D Maps in Maps JavaScript API supports the addition of\nlines and polygons to the map.\n\nPolylines\n\nTo draw a line on your map, use a polyline. The\n[`Polyline3DElement`](/maps/documentation/javascript/reference/3d-map#Polyline3DElement)\nclass defines a linear overlay of connected line segments on the map. A\n`Polyline` object consists of an array of `LatLng` locations, and creates a\nseries of line segments that connect those locations in an ordered sequence.\n\nAdd a polyline\n\nThe `Polyline` constructor takes a set of `Polyline3DElementOptions` specifying\nthe `LatLng` coordinates of the line and a set of styles to adjust the\npolyline's visual behavior.\n\nPolyline objects are drawn as a series of straight segments on the map. You can\nspecify custom colors, widths, opacities, heights, and geometric styling options\nfor the stroke of the line within the `Polyline3DElementOptions` when\nconstructing your line, or you can change those properties after construction. A\npolyline supports the following stroke styles:\n\n- `outerColor`: A hexadecimal HTML color of the format `\"#FFFFFF\"`.\n- `outerWidth`: A numerical value between `0.0` and `1.0`, which is interpreted as a percentage of the `strokeWidth`.\n- `strokeColor`: A hexadecimal HTML color of the format `\"#FFFFFF\"`.\n- `strokeWidth`: The width of the line in pixels.\n- `geodesic`: whether the edges of the polyon follows the curvature of the earth, or be drawn as straight lines.\n- `altitudeMode`: How altitude components in the coordinates are interpreted\n- `drawsOccludedSegments`: A boolean indicating whether parts of the polygon obscured by objects (such as buildings) should be drawn.\n- `extruded`: A boolean indicating if the polyline should be connected to the ground.\n\n async function init() {\n const { Map3DElement, MapMode } = await google.maps.importLibrary(\"maps3d\");\n\n const map = new Map3DElement({\n center: { lat: 37.772675996068915, lng: -122.3978382542777, altitude: 2500 },\n tilt: 45,\n heading: 5.9743656,\n mode: MapMode.HYBRID,\n });\n\n const { Polyline3DElement, AltitudeMode } = await google.maps.importLibrary(\"maps3d\");\n\n const polylineOptions = {\n strokeColor: \"#EA433580\",\n strokeWidth: 10,\n altitudeMode: \"ABSOLUTE\",\n extruded: true,\n drawsOccludedSegments: true,\n }\n const polyline = new google.maps.maps3d.Polyline3DElement(polylineOptions)\n\n polyline.coordinates = [\n {lat: 37.80515638571346, lng: -122.4032569467164},\n {lat: 37.80337073509504, lng: -122.4012878349353},\n {lat: 37.79925208843463, lng: -122.3976697250461},\n {lat: 37.7989102378512, lng: -122.3983408725656},\n {lat: 37.79887832784348, lng: -122.3987094864192},\n {lat: 37.79786443410338, lng: -122.4066878788802},\n {lat: 37.79549248916587, lng: -122.4032992702785},\n {lat: 37.78861484290265, lng: -122.4019489189814},\n {lat: 37.78618687561075, lng: -122.398969592545},\n {lat: 37.7892310309145, lng: -122.3951458683092},\n {lat: 37.7916358762409, lng: -122.3981969390652}\n ];\n\n map.append(polyline)\n document.body.append(map);\n }\n init();\n\nInteractive polylines\n\nThe following example changes the polyline's stroke color after registering a\nclick event. \n\n async function init() {\n const colors = [\"red\", \"blue\", \"green\", \"yellow\"];\n const { Map3DElement, MapMode, Polyline3DInteractiveElement } = await google.maps.importLibrary(\"maps3d\");\n const map = new Map3DElement({\n center: {lat: 0, lng: -180, altitude: 15000000},\n mode: MapMode.HYBRID,\n });\n\n document.body.append(map);\n const polyline = new Polyline3DInteractiveElement({\n coordinates: \\[\n { lat: 37.772, lng: -122.214 },\n { lat: 21.291, lng: -157.821 },\n { lat: -18.142, lng: 178.431 },\n { lat: -27.467, lng: 153.027 }\n \\],\n strokeColor: 'red',\n strokeWidth: 10,\n });\n polyline.addEventListener('gmp-click', (event) =\\\u003e {\n polyline.strokeColor = colors\\[\\~\\~(Math.random() \\* colors.length)\\];\n });\n map.append(polyline);\n }\n\n init();\n\nPolygons\n\nA polygon represents an area enclosed by a closed path (or loop), which is\ndefined by a series of coordinates.\n[`Polygon`](/maps/documentation/javascript/reference/3d-map#Polygon3DElement)\nobjects are similar to `Polyline` objects in that they consist of a series of\ncoordinates in an ordered sequence. Polygons are drawn with a stroke and a fill.\nYou can define custom colors and widths for the edge of the polygon (the stroke)\nand custom colors and opacities for the enclosed area (the fill). Colors should\nbe indicated in hexadecimal HTML format. Color names are not supported.\n\n`Polygon` objects can describe complex shapes, including:\n\n- Multiple non-contiguous areas defined by a single polygon.\n- Areas with holes in them.\n- Intersections of one or more areas.\n\nTo define a complex shape, you use a polygon with multiple paths.\n\nAdd a polygon\n\nBecause a polygonal area may include several separate paths, the `Polygon`\nobject's paths property specifies an array of arrays, each of type `MVCArray`.\nEach array defines a separate sequence of ordered `LatLng` coordinates.\n\nFor basic polygons consisting of only one path, you can construct a `Polygon`\nusing a single array of `LatLng` coordinates. The Photorealistic 3D Maps in Maps JavaScript API will\nconvert the array into an array of arrays upon construction when storing\nit within the `outerCoordinates` property. \n\n async function init() {\n const { Map3DElement, MapMode } = await google.maps.importLibrary(\"maps3d\");\n\n const map3DElement = new Map3DElement({\n center: { lat: 43.6425, lng: -79.3871, altitude: 400 },\n range: 1000,\n tilt: 60,\n mode: MapMode.SATELLITE,\n });\n\n const { Polygon3DElement, AltitudeMode } = await google.maps.importLibrary(\"maps3d\");\n\n const polygonOptions = {\n strokeColor: \"#EA433580\",\n strokeWidth: 4,\n fillColor: \"#0000FF80\",\n altitudeMode: \"ABSOLUTE\",\n extruded: true,\n drawsOccludedSegments: true,\n }\n\n const towerPolygon = new google.maps.maps3d.Polygon3DElement(polygonOptions);\n\n towerPolygon.outerCoordinates = [\n { lat: 43.6427196, lng: -79.3876802, altitude: 600 },\n { lat: 43.6421742, lng: -79.3869184, altitude: 600 },\n { lat: 43.643001, lng: -79.3866475, altitude: 600 }\n ];\n\n map3DElement.append(towerPolygon);\n document.body.append(map3DElement);\n }\n init();\n\nInteractive polygons\n\nThe following example changes the polygon's stroke color after registering a\nclick event. \n\n async function init() {\n const colors = [\"red\", \"blue\", \"green\", \"yellow\"];\n const { Map3DElement, MapMode } = await google.maps.importLibrary(\"maps3d\");\n\n const map3DElement = new Map3DElement({\n center: { lat: 43.6425, lng: -79.3871, altitude: 400 },\n range: 1000,\n tilt: 60,\n mode: MapMode.SATELLITE,\n });\n const { Polygon3DInteractiveElement, AltitudeMode } = await google.maps.importLibrary(\"maps3d\");\n const polygonOptions = {\n strokeColor: \"#EA433580\",\n strokeWidth: 4,\n fillColor: \"#0000FF80\",\n altitudeMode: \"ABSOLUTE\",\n extruded: true,\n drawsOccludedSegments: true,\n }\n const towerPolygon = new google.maps.maps3d.Polygon3DInteractiveElement(polygonOptions);\n towerPolygon.outerCoordinates = [\n { lat: 43.6427196, lng: -79.3876802, altitude: 600 },\n { lat: 43.6421742, lng: -79.3869184, altitude: 600 },\n { lat: 43.643001, lng: -79.3866475, altitude: 600 }\n ];\n towerPolygon.addEventListener('gmp-click', (event) =\\\u003e {\n towerPolygon.strokeColor = colors\\[\\~\\~(Math.random() \\* colors.length)\\];\n });\n map3DElement.append(towerPolygon);\n\n document.body.append(map3DElement);\n }\n\n init();"]]