Polylines
Learn how to add or remove polylines on a map.
We'll cover the following...
The Polyline object is used to draw connected lines on a map. It defines a linear overlay of line segments connected on the map. A Polyline is a list of LatLng locations on which line segments are drawn in an ordered sequence. They’re an excellent way to highlight paths or routes between specific points.
Add a polyline on a map
The Polyline constructor takes in the PolylineOptions parameter, which defines a set of line coordinates and visual style settings. Here’s an example of a Polyline drawn across the United States:
We draw Polyline objects as a collection of line segments on the map. We can specify custom styles for the stroke of the line within the PolylineOptions when constructing line segments.
PolylineOptions object parameters
A PolylineOptions object has no required parameters, but it does support the following optional parameters:
Parameters | Type | Description |
|
| This parameter represents an ordered set of coordinates that define the route of the |
|
| The term geodesic refers to the shortest possible line between two points on a curved surface. When 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 width of the line in pixels. |
|
| This parameter specifies whether the |
|
| This parameter allows the user to drag the polylines across the map when it's set to |
|
| When set to |
|
| This parameter specifies the icons and images that will be rendered across the polyline. |
|
| This parameter specifies the map on which we are going to render polylines. |
|
| This parameter specifies whether the polyline is displayed on the map. |
|
| 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 Polyline object without any parameters since they are all optional. However, if we want to display a Polyline object, the map and path parameters are required, and the visible parameter value must be true.
We can either provide map, path, 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 Polyline is constructed:
const polylineObj = new google.maps.Polyline({path: [{ lat: 51.5072, lng: -0.1276 }, // Coordinates of London{ lat: 40.7128, lng: -74.006 }, // Coordinates of New York],geodesic: true,strokeColor: "#00FF00",strokeOpacity: 0.75,strokeWeight: 3,clickable: true,draggable: false,editable: false,icons: [{icon: {path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,strokeColor: "#0F0",fillColor: "#0F0",fillOpacity: 1,},offest: "100%",},], // Arrow icon on Seattlemap: map,visible: true,zindex: 1,});
We can render polylines on the map using the setMap method and passing the map object as a parameter, as shown below:
polylineObj.setMap(map);
Code Example
The example below creates a two-pixel-wide red polyline that shows a flight path between Seattle and Brisbane that passes through Fiji and Hawaii:
Here’s a brief explanation of the JavaScript file in the above code widget:
- Line 8: We define a
flightPlanCoordinatesobject that defines the flight plan coordinates for Brisbane to Seattle. - Line 15: We initialize a
Polylineobject calledflightPaththat takes theflightPlanCoordinatesobject as its path. - Line 17: We set the
geodesicparameter offlightPathtotrue, which makes the polyline follow the curvature of the earth. - Line 18: We set the
strokeColorparameter offlightPathto#FF0000, which renders a red-colored polyline on the map. - Lines 19–20: We set the
strokeOpacityto1.0andstrokeWeightto2to render a completely opaque polyline with a width of2pixels. - Line 23: We call the
setMapmethod to render theflightPathpolylines over the map.
Remove a polyline
We can remove a polyline from the map by calling the setMap() method on the polyline object and passing null as its argument.
flightPath.setMap(null);
Code example
In the example below, we create a polyline object, flightPath, with a UI control that allows the user to remove the polyline from the map:
Here’s a brief explanation of the HTML file in the above code widget:
- Lines 8–11: We create a separate
divelement just above our map’sdivelement. - Line 9 and 10: In this separate
divelement, we create two input buttons, namely:add-linefor adding the polylines andremove-linefor removing the polylines.
Here’s a brief explanation of the JavaScript file in the above code widget:
- Lines 1–2: We define two global variables:
flightPathandmap. - Line 11: We define a
flightPlanCoordinatesobject that defines the flight plan coordinates for Brisbane to Seattle. - Line 18: We initialize a
Polylineobject calledflightPaththat takes theflightPlanCoordinatesobject as its path. - Lines 31–35: We define a function
addLine()to add polylines. When this function is called, thesetMap()method on line 32 renders theflightPathpolylines over the map. - Lines 37–41: We define a function
removeLine()to remove polylines. In thesetMap()method on line 38, we set themapparameter tonull, which removes the polylines from the map. - Line 27: We define an event listener that listens for a
clickevent for theadd-linebutton element and calls theaddLine()method. - Line 28: We define an event listener that listens for a
clickevent for theremove-linebutton element and calls theremoveLine()method.