Polygons
Learn how to work with polygons on a map.
We'll cover the following
Polygons represent a two-dimensional plane enclosed by a closed path where a series of coordinates define the enclosed path. A polygon has at least three sides. A Polygon
object is drawn with a stroke and a fill. We can define custom style options for the stroke and custom colors for the enclosed area of the polygon.
Add a polygon to a map
A polygon’s area may consist of several separate paths. Therefore, the Polygon
object’s path
property specifies an array of arrays, and in each array, we specify a distinct sequence of ordered LatLng
coordinates.
The Polygon
constructor takes in the PolygonOptions
parameter. The PolygonOptions
parameter defines the LatLng
coordinates for the vertices and a style set that alters the polygon’s visual features.
PolygonOptions Parameters
All PolygonOptions
parameters are optional since there are no required parameters. Here’s a list of all the optional PolygonOptions
parameters that we can pass to the Polygon
constructor:
Parameters | Type | Description |
|
| This parameter represents an ordered set of coordinates that form a closed loop defining the |
|
| The term geodesic refers to the shortest possible line between two points on a curved surface. If we set this parameter to |
|
| This parameter specifies a hexadecimal code for the color of the line stroke, like |
|
| This parameter specifies a numerical value between |
|
| This parameter specifies the direction of the stroke. The The |
|
| This parameter specifies the width of the line in pixels. |
|
| This parameter specifies whether the |
|
| This parameter allows the user to drag the polygons across the map when it's set to |
|
| When set to |
|
| This parameter specifies a hexadecimal code for the fill color of the polygon, like |
|
| This parameter specifies a numerical value between |
|
| This parameter specifies the map that will have polylines rendered on it. |
|
| This parameter specifies whether the polygons are displayed on the map or not. |
|
| This parameter represents the overlay order in which different drawing objects are placed in front of each other. A smaller-value |
We can construct a Polygon
object without any parameters, since all the parameters are optional. However, if we want to display a Polygon
, the map
and paths
parameters are required, and the visible
parameter value must be true
.
We can either provide map
, paths
, and all other parameters during construction or provide them after construction using the setValues()
method. The setMap()
can also be used to update the map
parameter after construction.
Here’s an example of how a Polygon
object is constructed:
const polygonObj = new google.maps.Polygon({paths: [{ lat: 25.774, lng: -80.19 }, // First vertice of the Bermuda Triangle at Miami, Florida (US){ lat: 18.466, lng: -66.118 }, // Second vertice of the Bermuda Triangle at San Juan, Puerto Rico{ lat: 32.321, lng: -64.757 }, // Third vertice of the Bermuda Triangle at Bermuda (UK)],geodesic: true,strokeColor: "#FF0000",strokeOpacity: 1.0,strokePosition: google.maps.StrokePosition.CENTER,strokeWeight: 2,clickable: true,draggable: false,editable: false,fillColor: "#FF0000",fillOpacity: 0.35,map: map,visible: true,zIndex: 1,});
In each path of coordinates, the first and last locations are usually the same so that the loop is complete. However, since polygons are closed areas, we do not need to provide the last set of coordinates. In this case, the API will automatically complete the polygon by drawing a stroke between the first and last locations.
Code example
In the following example, we create a simple polygon on a map that represents the Bermuda Triangle:
Here’s a brief explanation of the JavaScript file in the code widget above:
- Line 8: We define a
polygonCoords
array, that defines the sequence of coordinates that form the path boundary of the Bermuda Triangle. - Line 9: We define the coordinates of Miami, the first checkpoint from where we’re going to start our path boundary.
- Line 12: We again define the coordinates of Miami as the final stop of our path boundary. We perform this action to draw the last stroke from the last vertice to the first vertice to close the path boundary of the Bermuda Triangle.
- Lines 15–23: We construct our
Polygon
object and assign it to the variablebermudaTriangle
. Note that we have not provided themap
parameter here, which means that the Bermuda Triangle will only be constructed and not displayed on the map. - Line 16: We provide the
polygonCoords
array to thepaths
parameter in thebermudaTriangle
object’s constructor to designate our polygon’s boundary. - Line 20: We set the
draggable
property totrue
, which allows us to drag the polygon across the map. - Line 25: We use the
setValues()
method to set and render the polygonbermudaTriangle
on the specified map object,map
.
Remove a polygon
We can remove a polygon from the map by calling the setMap()
method on polygonObj
and passing null
as its argument:
polygonObj.setMap(null);
Code example
In the example below, we create a polyline object, bermudaTriangle
, with a UI control that allows the user to remove the polygon from the map:
Here’s a brief explanation of the HTML file:
- Lines 8–11: We create a separate
div
element just above our map’sdiv
element. In this separatediv
element, we create two input buttons, namelyadd-polygon
on line 9 to add the polygon andremove-polygon
on line 10 to remove the polygon.
Here’s a brief explanation of the JavaScript file:
- Lines 1–2: We define two global variables,
map
andbermudaTriangle
. - Line 11: We define the array
polygonCoords
. This defines the sequence of coordinates that form the path boundary of the Bermuda Triangle. - Line 18: We initialize our
Polygon
object,bermudaTriangle
. - Lines 34–38: We define a function
addPolygon()
to add the polygon. When this function is called, thesetMap()
method on line 35 renders thebermudaTriangle
polygon over the map. - Lines 40-44: We define a function
removePolygon()
to remove the polygon, and using thesetMap()
method on line 41, we set the map parameter tonull
, which removes the polygon from the map. - Line 30: We define an event listener that listens for a
click
event for theadd-polygon
button element and calls theaddPolygon()
method. - Line 31: We define an event listener that listens for a
click
event for theremove-polygon
button element and calls theremovePolygon()
method.