A polygon on the earth's surface. A polygon can be convex or concave, it may span the 180 meridian and it can have holes that are not filled in. It has the following properties:
- Outline
- The outline is specified by a list of vertices in clockwise or counterclockwise order. It is not necessary for the start and end points to coincide; if they do not, the polygon will be automatically closed. Line segments are drawn between consecutive points in the shorter of the two directions (east or west).
- Holes
- A hole is a region inside the polygon that is not filled. A hole is specified in exactly the same way as the outline. A hole must be fully contained within the outline. Multiple holes can be specified, however overlapping holes are not supported.
- Stroke Width
- Line segment width in screen pixels. The width is constant and independent of the camera's zoom level. The default value is 10.
- Stroke Color
- Line segment color in ARGB format, the same format used by
Color
.The default value is black (0xff000000
). - Stroke Joint Type
- The joint type defines the shape to be used when joining adjacent line segments at all
vertices of the polygon's outline. See
JointType
for supported joint types. The default value isJointType.DEFAULT
. - Stroke pattern
- Solid (default, represented by
null
) or a sequence ofPatternItem
objects to be repeated along the polygon's outline. AvailablePatternItem
types:Gap
(defined by gap length in pixels),Dash
(defined by stroke width and dash length in pixels) andDot
(circular, centered on the polygon's outline, diameter defined by stroke width in pixels). - Fill Color
- Fill color in ARGB format, the same format used by
Color
.The default value is transparent (0x00000000
). If the polygon geometry is not specified correctly (see above for Outline and Holes), then no fill will be drawn. - Z-Index
- The order in which this polygon is drawn with respect to other overlays, including
Polyline
s,Circle
s,GroundOverlay
s andTileOverlay
s, but notMarker
s. An overlay with a larger z-index is drawn over overlays with smaller z-indices. The order of overlays with the same z-index value is arbitrary. The default is 0. - Visibility
- Indicates if the polygon is visible or invisible, i.e., whether it is drawn on the map.
An invisible polygon is not drawn, but retains all of its other properties. The default is
true
,i.e., visible. - Geodesic status
- Indicates whether the segments of the polygon 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 polygon, set this property
to
true
.You can change this value at any time. The default isfalse
.If this property is set totrue
,your app will receive notifications to theGoogleMap.OnPolygonClickListener
registered throughGoogleMap.setOnPolygonClickListener(GoogleMap.OnPolygonClickListener)
. - Tag
- An
Object
associated with the polygon. For example, theObject
can contain data about what the polygon represents. This is easier than storing a separateMap<Polygon, Object>
.As another example, you can associate aString
ID corresponding to the ID from a data set. Google Maps SDK for Android neither reads nor writes this property.
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 triangle in the Gulf of Guinea
Polygon polygon = map.addPolygon(new PolygonOptions()
.add(new LatLng(0, 0), new LatLng(0, 5), new LatLng(3, 5), new LatLng(0, 0))
.strokeColor(Color.RED)
.fillColor(Color.BLUE));
Developer Guide
For more information, read theShapesdeveloper guide.
Public Method Summary
boolean | |
int |
getFillColor()
Gets the fill color of this polygon.
|
List<List<LatLng>> |
getHoles()
Returns a snapshot of the holes of this polygon at this time.
|
String |
getId()
Gets this polygon's id.
|
List<LatLng> |
getPoints()
Returns a snapshot of the vertices of this polygon at this time.
|
int |
getStrokeColor()
Gets the stroke color of this polygon.
|
int |
getStrokeJointType()
Gets the stroke joint type used at all vertices of the polygon's outline.
|
List<PatternItem> |
getStrokePattern()
Gets the stroke pattern of this polygon's outline.
|
float |
getStrokeWidth()
Gets the stroke width of this polygon.
|
Object |
getTag()
Gets the tag for the polygon.
|
float |
getZIndex()
Gets the zIndex of this polygon.
|
int |
hashCode()
|
boolean |
isClickable()
Gets the clickability of the polygon.
|
boolean |
isGeodesic()
Gets whether each segment of the line is drawn as a geodesic or not.
|
boolean |
isVisible()
Gets the visibility of this polygon.
|
void |
remove()
Removes the polygon from the map.
|
void |
setClickable(boolean clickable)
Sets the clickability of the polygon.
|
void |
setFillColor(int color)
Sets the fill color of this polygon.
|
void |
setGeodesic(boolean geodesic)
Sets whether to draw each segment of the line as a geodesic or not.
|
void | |
void | |
void |
setStrokeColor(int color)
Sets the stroke color of this polygon.
|
void |
setStrokeJointType(int jointType)
Sets the joint type for all vertices of the polygon's outline.
|
void | |
void |
setStrokeWidth(float width)
Sets the stroke width of this polygon.
|
void | |
void |
setVisible(boolean visible)
Sets the visibility of this polygon.
|
void |
setZIndex(float zIndex)
Sets the zIndex of this polygon.
|
Inherited Method Summary
Public Methods
public booleanequals(Objectother)
public intgetFillColor()
Gets the fill color of this polygon.
Returns
- the color in ARGB format.
publicList<List<LatLng>> getHoles()
Returns a snapshot of the holes of this polygon at this time. The list returned is a copy of the list of holes and so changes to the polygon's holes will not be reflected by this list, nor will changes to this list be reflected by the polygon.
publicStringgetId()
Gets this polygon's id. The id will be unique amongst all Polygons on a map.
publicList<LatLng> getPoints()
Returns a snapshot of the vertices of this polygon at this time. The list returned
is a copy of the list of vertices and so changes to the polygon's vertices will not be
reflected by this list, nor will changes to this list be reflected by the polygon. To
change the vertices of the polygon, call
setPoints(List)
.
public intgetStrokeColor()
Gets the stroke color of this polygon.
Returns
- the color in ARGB format.
public intgetStrokeJointType()
Gets the stroke joint type used at all vertices of the polygon's outline. See
JointType
for possible values.
Returns
- the stroke joint type.
publicList<PatternItem> getStrokePattern()
Gets the stroke pattern of this polygon's outline.
Returns
- the stroke pattern, or
null
if default pattern is used.
public floatgetStrokeWidth()
Gets the stroke width of this polygon.
Returns
- the width in screen pixels.
publicObjectgetTag()
Gets the tag for the polygon.
Returns
- the tag if a tag was set with
setTag(Object)
;null
if no tag has been set.
public floatgetZIndex()
Gets the zIndex of this polygon.
Returns
- the zIndex of the polygon.
public inthashCode()
public booleanisClickable()
Gets the clickability of the polygon. If the polygon is clickable, your app will
receive notifications to theGoogleMap.OnPolygonClickListener
when the user clicks the polygon. The event listener is registered through
GoogleMap.setOnPolygonClickListener(GoogleMap.OnPolygonClickListener)
.
Returns
true
if the polygon is clickable; otherwise, returnsfalse
.
public booleanisGeodesic()
Gets whether each segment of the line is drawn as a geodesic or not.
Returns
true
if each segment is drawn as a geodesic;false
if each segment is drawn as a straight line on the Mercator projection.
public booleanisVisible()
Gets the visibility of this polygon.
Returns
- this polygon visibility.
public voidremove()
Removes the polygon from the map. After a polygon has been removed, the behavior of all its methods is undefined.
public voidsetClickable(boolean clickable)
Sets the clickability of the polygon. If the polygon is clickable, your app will
receive notifications to theGoogleMap.OnPolygonClickListener
when the user clicks the polygon. The event listener is registered through
GoogleMap.setOnPolygonClickListener(GoogleMap.OnPolygonClickListener)
.
Parameters
clickable | New clickability setting for the polygon. |
---|
public voidsetFillColor(int color)
Sets the fill color of this polygon.
Parameters
color | the color in ARGB format |
---|
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 voidsetHoles(List<? extendsList<LatLng>> holes)
Sets the holes of this polygon. This method will take a copy of the holes, so
further mutations toholes
will have no effect on this polygon.
Parameters
holes | an list of holes, where a hole is an list ofLatLng s. |
---|
public voidsetPoints(List<LatLng> points)
Sets the points of this polygon. This method will take a copy of the points, so
further mutations topoints
will have no effect on this polygon.
Parameters
points | a list ofLatLng s
that are the vertices of the polygon. Must not benull . |
---|
public voidsetStrokeColor(int color)
Sets the stroke color of this polygon.
Parameters
color | the color in ARGB format |
---|
public voidsetStrokeJointType(int jointType)
Sets the joint type for all vertices of the polygon's outline.
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 stroke joint type. |
---|
public voidsetStrokePattern(List<PatternItem> pattern)
Sets the stroke pattern of the polygon's outline. The default stroke pattern is
solid, represented bynull
.
Parameters
pattern | the stroke pattern. |
---|
public voidsetStrokeWidth(float width)
Sets the stroke width of this polygon.
Parameters
width | the width in display pixels. |
---|
public voidsetTag(Objecttag)
Sets the tag for the polygon.
You can use this property to associate an arbitraryObject
with this
polygon. For example, theObject
can contain data about what the polygon
represents. This is easier than storing a separateMap<Polygon,
Object>
.As another example, you can associate aString
ID
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 polygon. When not visible, a polygon is not drawn, but it keeps all its other properties.
Parameters
visible | iftrue ,then the polygon is visible; iffalse ,it is
not. |
---|
public voidsetZIndex(float zIndex)
Sets the zIndex of this polygon. Polygons with higher zIndices are drawn above those with lower indices.
Parameters
zIndex | the zIndex of this polygon. |
---|