Polyline

public final classPolylineextendsObject

A polyline is a list of points, where line segments are drawn between consecutive points. A polyline has the following properties:

Points
The vertices of the line. Line segments are drawn between consecutive points. A polyline isnotclosed by default; to form a closed polyline, the start and end points must be the same.
Width
Line segment width in screen pixels. The width is constant and independent of the camera's zoom level. The default value is 10.
Color
Line segment color in ARGB format, the same format used byColor.The default value is black (0xff000000).
Start/end cap
Defines the shape to be used at the start or end of a polyline. Supported cap types: ButtCap, SquareCap, RoundCap (applicable for solid stroke pattern) andCustomCap (applicable for any stroke pattern). Default for both start and end:ButtCap.
Joint type
The joint type defines the shape to be used when joining adjacent line segments at all vertices of the polyline except the start and end vertices. SeeJointType for supported joint types. The default value isJointType.DEFAULT.
Stroke pattern
Solid (default, represented bynull) or a sequence ofPatternItem objects to be repeated along the line. AvailablePatternItemtypes: Gap(defined by gap length in pixels),Dash(defined by line width and dash length in pixels) andDot(circular, centered on the line, diameter defined by line width in pixels).
Z-Index
The order in which this tile overlay is drawn with respect to other overlays (including GroundOverlays, TileOverlays, Circles, and Polygons but notMarkers). An overlay with a larger z-index is drawn over overlays with smaller z-indices. The order of overlays with the same z-index is arbitrary. The default zIndex is 0.
Visibility
Indicates if the polyline is visible or invisible, i.e., whether it is drawn on the map. An invisible polyline is not drawn, but retains all of its other properties. The default istrue,i.e., visible.
Geodesic status
Indicates whether the segments of the polyline should be drawn as geodesics, as opposed to straight lines on the Mercator projection. A geodesic is the shortest path between two points on the Earth's surface. The geodesic curve is constructed assuming the Earth is a sphere
Clickability
If you want to handle events fired when the user clicks the polyline, set this property totrue.You can change this value at any time. The default is false.If this property is set totrue,your app will receive notifications to theGoogleMap.OnPolylineClickListener registered through GoogleMap.setOnPolylineClickListener(GoogleMap.OnPolylineClickListener).
Tag
AnObjectassociated with the polyline. For example, the Objectcan contain data about what the polyline represents. This is easier than storing a separateMap<Polyline, Object>.As another example, you can associate aStringID corresponding to the ID from a data set. Google Maps SDK for Android neither reads nor writes this property..
Spans
Defines the spans with different styles used to render this polyline. You can use this property to create a polyline with differentStrokeStyle on different segments of the polyline.

Methods in this class must be called on the Android UI thread. If not, anIllegalStateException will be thrown at runtime.

Example

GoogleMap map;
//... get a map.
// Add a thin red line from London to New York.
Polyline line = map.addPolyline(new PolylineOptions()
.add(new LatLng(51.5, -0.1), new LatLng(40.7, -74.0))
.width(5)
.color(Color.RED));

Developer Guide

For more information, read theShapesdeveloper guide.

Public Method Summary

boolean
equals(Objectother)
Tests if thisPolyline is equal to another.
int
getColor()
Gets the color of this polyline.
Cap
getEndCap()
Gets the cap at the end vertex of this polyline.
String
getId()
Gets this polyline's id.
int
getJointType()
Gets the joint type used at all vertices of the polyline except the start and end vertices.
List<PatternItem>
getPattern()
Gets the stroke pattern of this polyline.
List<LatLng>
getPoints()
Returns a snapshot of the vertices of this polyline at this time.
List<StyleSpan>
getSpans()
Gets spans of the polyline.
Cap
getStartCap()
Gets the cap at the start vertex of this polyline.
Object
getTag()
Gets the tag for the polyline.
float
getWidth()
Gets the width of this polyline.
float
getZIndex()
Gets the zIndex of this polyline.
int
boolean
isClickable()
Gets the clickability of the polyline.
boolean
isGeodesic()
Gets whether each segment of the line is drawn as a geodesic or not.
boolean
isVisible()
Gets the visibility of this polyline.
void
remove()
Removes this polyline from the map.
void
setClickable(boolean clickable)
Sets the clickability of the polyline.
void
setColor(int color)
Sets the color of this polyline.
void
setEndCap(CapendCap)
Sets the cap at the end vertex of this polyline.
void
setGeodesic(boolean geodesic)
Sets whether to draw each segment of the line as a geodesic or not.
void
setJointType(int jointType)
Sets the joint type for all vertices of the polyline except the start and end vertices.
void
setPattern(List<PatternItem> pattern)
Sets the stroke pattern of the polyline.
void
setPoints(List<LatLng> points)
Sets the points of this polyline.
void
setSpans(List<StyleSpan> spans)
Sets spans of the polyline.
void
setStartCap(CapstartCap)
Sets the cap at the start vertex of this polyline.
void
setTag(Objecttag)
Sets the tag for the polyline.
void
setVisible(boolean visible)
Sets the visibility of this polyline.
void
setWidth(float width)
Sets the width of this polyline.
void
setZIndex(float zIndex)
Sets the zIndex of this polyline.

Inherited Method Summary

Public Methods

public booleanequals(Objectother)

Tests if thisPolyline is equal to another.

Parameters
other anObject.
Returns
  • true if both objects are the same object, that is, this == other.

public intgetColor()

Gets the color of this polyline.

Returns
  • the color in ARGB format.

publicCapgetEndCap()

Gets the cap at the end vertex of this polyline.

Returns
  • the end cap type.

publicStringgetId()

Gets this polyline's id. The id will be unique amongst all Polylines on a map.

Returns
  • this polyline's id.

public intgetJointType()

Gets the joint type used at all vertices of the polyline except the start and end vertices. SeeJointType for possible values.

Returns
  • the joint type.

publicList<PatternItem> getPattern()

Gets the stroke pattern of this polyline.

Returns
  • the stroke pattern, ornullif default is used.

publicList<LatLng> getPoints()

Returns a snapshot of the vertices of this polyline at this time. The list returned is a copy of the list of vertices and so changes to the polyline's vertices will not be reflected by this list, nor will changes to this list be reflected by the polyline. To change the vertices of the polyline, call setPoints(List).

publicList<StyleSpan> getSpans()

Gets spans of the polyline.

publicCapgetStartCap()

Gets the cap at the start vertex of this polyline.

Returns
  • the start cap.

publicObjectgetTag()

Gets the tag for the polyline.

Returns
  • the tag if a tag was set withsetTag;nullif no tag has been set.

public floatgetWidth()

Gets the width of this polyline.

Returns
  • the width in screen pixels.

public floatgetZIndex()

Gets the zIndex of this polyline.

Returns
  • the zIndex of the polyline.

public inthashCode()

public booleanisClickable()

Gets the clickability of the polyline. If the polyline is clickable, your app will receive notifications to the GoogleMap.OnPolylineClickListenerwhen the user clicks the polyline. The event listener is registered through GoogleMap.setOnPolylineClickListener(GoogleMap.OnPolylineClickListener).

Returns
  • trueif the polyline is clickable; otherwise, returns false.

public booleanisGeodesic()

Gets whether each segment of the line is drawn as a geodesic or not.

Returns
  • trueif each segment is drawn as a geodesic;falseif each segment is drawn as a straight line on the Mercator projection.

public booleanisVisible()

Gets the visibility of this polyline.

Returns
  • this polyline's visibility.

public voidremove()

Removes this polyline from the map. After a polyline has been removed, the behavior of all its methods is undefined.

public voidsetClickable(boolean clickable)

Sets the clickability of the polyline. If the polyline is clickable, your app will receive notifications to the GoogleMap.OnPolylineClickListenerwhen the user clicks the polyline. The event listener is registered through GoogleMap.setOnPolylineClickListener(GoogleMap.OnPolylineClickListener).

Parameters
clickable New clickability setting for the polyline.

public voidsetColor(int color)

Sets the color of this polyline.

Parameters
color the color in ARGB format

public voidsetEndCap(CapendCap)

Sets the cap at the end vertex of this polyline. The default end cap is ButtCap.

Parameters
endCap the end cap. Must not benull.

public voidsetGeodesic(boolean geodesic)

Sets whether to draw each segment of the line as a geodesic or not.

Parameters
geodesic iftrue,then each segment is drawn as a geodesic; if false,each segment is drawn as a straight line on the Mercator projection.

public voidsetJointType(int jointType)

Sets the joint type for all vertices of the polyline except the start and end vertices.

SeeJointType for allowed values. The default valueJointType.DEFAULT will be used if joint type is undefined or is not one of the allowed values.

Parameters
jointType the joint type.

public voidsetPattern(List<PatternItem> pattern)

Sets the stroke pattern of the polyline. The default stroke pattern is solid, represented bynull.

Parameters
pattern the stroke pattern.

public voidsetPoints(List<LatLng> points)

Sets the points of this polyline. This method will take a copy of the points, so further mutations topointswill have no effect on this polyline.

Parameters
points a list ofLatLngs that are the vertices of the polyline. Must not benull.

public voidsetSpans(List<StyleSpan> spans)

Sets spans of the polyline.

public voidsetStartCap(Cap startCap)

Sets the cap at the start vertex of this polyline. The default start cap is ButtCap.

Parameters
startCap the start cap. Must not benull.

public voidsetTag(Objecttag)

Sets the tag for the polyline.

You can use this property to associate an arbitraryObjectwith this polyline. For example, theObjectcan contain data about what the polyline represents. This is easier than storing a separateMap<Polyline, Object>.As another example, you can associate aStringID corresponding to the ID from a data set. Google Maps SDK for Android neither reads nor writes this property. It is your responsibility to callsetTag(null)to clear the tag when you no longer need it, to prevent memory leaks in your app.

Parameters
tag ifnull,the tag is cleared.

public voidsetVisible(boolean visible)

Sets the visibility of this polyline. When not visible, a polyline is not drawn, but it keeps all its other properties.

Parameters
visible iftrue,then the polyline is visible; iffalse,it is not.

public voidsetWidth(float width)

Sets the width of this polyline.

Parameters
width the width in screen pixels

public voidsetZIndex(float zIndex)

Sets the zIndex of this polyline. Polylines with higher zIndices are drawn above those with lower indices.

Parameters
zIndex the zIndex of this polyline.