Draw Circle on Google Maps Android
The Google Maps API for Android offers some unproblematic means for you lot to add shapes to your maps in club to customize them for your application.
- A
Polyline
is a series of connected line segments that can form any shape you want and tin can exist used to marker paths and routes on the map. - A
Polygon
is an enclosed shape that can be used to mark areas on the map. - A
Circle
is a geographically authentic projection of a circumvolve on the World's surface drawn on the map.
For all these shapes, you can customize their advent past altering a number of properties.
Code samples
The tutorial on adding polygons and polylines to represent areas and routes includes all the code for a unproblematic Android app.
In addition, the ApiDemos repository on GitHub includes samples that demonstrate the use of shapes and their features:
- CircleDemoActivity (Java / Kotlin): Circle
- PolygonDemoActivity (Java / Kotlin): Polygon
- PolylineDemoActivity (Java / Kotlin): Polyline
Polylines
The Polyline
class defines a set of continued line segments on the map. A Polyline
object consists of a set of LatLng
locations, and creates a series of line segments that connect those locations in an ordered sequence.
This video gives ideas on how to help your users get to where they're going, using polylines to depict a path on the map.
To create a Polyline, outset create a PolylineOptions
object and add points to it. Points represent a point on the world's surface, and are expressed as a LatLng
object. Line segments are drawn between points co-ordinate to the club in which you add them to the PolylineOptions
object. To add points to a PolylineOptions
object, phone call PolylineOptions.add together()
. Notice that this method takes a variable number of parameters so you are able to add multiple points at a time (you can besides call PolylineOptions.addAll(Iterable<LatLng>)
if the points are already in a listing).
Y'all tin can then add the polyline to a map past calling GoogleMap.addPolyline(PolylineOptions)
. The method returns a Polyline
object with which you tin alter the polyline at a afterwards fourth dimension.
The following code snippet illustrates how to add a rectangle to a map:
Java
// Instantiates a new Polyline object and adds points to define a rectangle PolylineOptions polylineOptions = new PolylineOptions() .add(new LatLng(37.35, -122.0)) .add(new LatLng(37.45, -122.0)) // Northward of the previous indicate, but at the same longitude .add(new LatLng(37.45, -122.2)) // Same latitude, and 30km to the west .add(new LatLng(37.35, -122.2)) // Aforementioned longitude, and 16km to the s .add(new LatLng(37.35, -122.0)); // Closes the polyline. // Get back the mutable Polyline Polyline polyline = map.addPolyline(polylineOptions);
Kotlin
// Instantiates a new Polyline object and adds points to define a rectangle val polylineOptions = PolylineOptions() .add together(LatLng(37.35, -122.0)) .add(LatLng(37.45, -122.0)) // Northward of the previous point, only at the same longitude .add(LatLng(37.45, -122.2)) // Aforementioned latitude, and 30km to the west .add(LatLng(37.35, -122.2)) // Same longitude, and 16km to the south .add(LatLng(37.35, -122.0)) // Closes the polyline. // Become back the mutable Polyline val polyline = map.addPolyline(polylineOptions)
To alter the shape of the polyline after it has been added, you can call Polyline.setPoints()
and provide a new list of points for the polyline.
You tin can customize the advent of the polyline both before adding it to the map and after it has been added to the map. See the section on customizing appearances below for further details.
Polyline events
By default, polylines are not clickable. You lot can enable and disable the clickability by calling Polyline.setClickable(boolean)
.
Use an OnPolylineClickListener
to listen to click events on a clickable polyline. To set this listener on the map, call GoogleMap.setOnPolylineClickListener(OnPolylineClickListener)
. When a user clicks on a polyline, you will receive an onPolylineClick(Polyline)
callback.
Polygons
Polygon
objects are similar to Polyline
objects in that they consist of a series of coordinates in an ordered sequence. However, instead of being open up-ended, polygons are designed to ascertain regions inside a closed loop with the interior filled in.
You tin can add a Polygon
to the map in the same way every bit yous add a Polyline
. First create a PolygonOptions
object and add together some points to it. These points will class the outline of the polygon. You then add the polygon to the map past calling GoogleMap.addPolygon(PolygonOptions)
which volition render a Polygon
object.
The following code snippet adds a rectangle to a map (note that since we have not defined a fill colour, and the default fill color is transparent, this will announced exactly the same every bit the polyline from the snippet in the previous section):
Java
// Instantiates a new Polygon object and adds points to ascertain a rectangle PolygonOptions polygonOptions = new PolygonOptions() .add(new LatLng(37.35, -122.0), new LatLng(37.45, -122.0), new LatLng(37.45, -122.2), new LatLng(37.35, -122.2), new LatLng(37.35, -122.0)); // Go back the mutable Polygon Polygon polygon = map.addPolygon(polygonOptions);
Kotlin
// Instantiates a new Polygon object and adds points to define a rectangle val rectOptions = PolygonOptions() .add together( LatLng(37.35, -122.0), LatLng(37.45, -122.0), LatLng(37.45, -122.2), LatLng(37.35, -122.2), LatLng(37.35, -122.0) ) // Go dorsum the mutable Polygon val polygon = map.addPolygon(rectOptions)
To alter the shape of the polygon after information technology has been added, you tin telephone call Polygon.setPoints()
and provide a new list of points for the outline of the polygon.
You can customize the advent of the polygon both earlier adding information technology to the map and after it has been added to the map. See the section on customizing appearances below for further details.
Polygon automobile-completion
The Polygon in the example higher up consists of five coordinates, but notice that the get-go and last coordinates are the same location, which defines the loop. In do, withal, since polygons define closed areas, you don't need to ascertain this terminal coordinate. If the last coordinate differs from the first, the API will automatically "close" the polygon by appending the get-go coordinate at the end of the sequence of coordinates.
The below two polygons are equivalent, and calling polygon.getPoints()
for each of them will return all 4 points.
Coffee
Polygon polygon1 = map.addPolygon(new PolygonOptions() .add together(new LatLng(0, 0), new LatLng(0, 5), new LatLng(three, 5), new LatLng(0, 0)) .strokeColor(Color.RED) .fillColor(Color.Bluish)); Polygon polygon2 = map.addPolygon(new PolygonOptions() .add(new LatLng(0, 0), new LatLng(0, 5), new LatLng(3, five)) .strokeColor(Color.Reddish) .fillColor(Color.BLUE));
Kotlin
val polygon1 = map.addPolygon( PolygonOptions() .add together( LatLng(0.0, 0.0), LatLng(0.0, five.0), LatLng(3.0, 5.0), LatLng(0.0, 0.0) ) .strokeColor(Colour.Carmine) .fillColor(Color.Blueish) ) val polygon2 = map.addPolygon( PolygonOptions() .add( LatLng(0.0, 0.0), LatLng(0.0, five.0), LatLng(3.0, 5.0) ) .strokeColor(Color.RED) .fillColor(Color.BLUE) )
Create a hollow polygon
Multiple paths can be combined in a single Polygon
object to create complex shapes, such as filled rings, or "donuts" (where polygonal areas appear inside the polygon as "islands"). Complex shapes are always the composition of multiple, simpler, paths.
Two paths must be defined in the aforementioned area. The larger of the two regions defines the fill surface area, and is a simple polygon with no additional options. Then, pass a second path to the addHole()
method. When the second, smaller path is full enclosed by the larger path, it will appear as if a slice of the polygon has been removed. If the hole intersects the outline of the polygon, the polygon volition be rendered without any fill.
The beneath snippet will create a unmarried rectangle, with a smaller rectangular hole.
Java
Listing<LatLng> hole = Arrays.asList(new LatLng(1, i), new LatLng(one, ii), new LatLng(2, 2), new LatLng(ii, one), new LatLng(i, 1)); Polygon hollowPolygon = map.addPolygon(new PolygonOptions() .add(new LatLng(0, 0), new LatLng(0, 5), new LatLng(3, 5), new LatLng(three, 0), new LatLng(0, 0)) .addHole(hole) .fillColor(Color.Blue));
Kotlin
val hole = listOf( LatLng(one.0, 1.0), LatLng(1.0, 2.0), LatLng(two.0, 2.0), LatLng(ii.0, 1.0), LatLng(i.0, i.0) ) val hollowPolygon = map.addPolygon( PolygonOptions() .add( LatLng(0.0, 0.0), LatLng(0.0, five.0), LatLng(iii.0, v.0), LatLng(3.0, 0.0), LatLng(0.0, 0.0) ) .addHole(hole) .fillColor(Colour.Blueish) )
Polygon events
By default, polygons are not clickable. You tin enable and disable the clickability by calling Polygon.setClickable(boolean)
.
Use an OnPolygonClickListener
to listen to click events on a clickable polygon. To set this listener on the map, call GoogleMap.setOnPolygonClickListener(OnPolygonClickListener)
. When a user clicks on a polygon, you volition receive an onPolygonClick(Polygon)
callback.
Circles
In add-on to a generic Polygon
class, the Maps API as well includes specific classes for Circle
objects, to simplify their structure.
To construct a circumvolve, you lot must specify the following two properties:
-
eye
as aLatLng
. -
radius
in meters.
A circumvolve is and then defined to exist the gear up of all points on the World'south surface which are radius
meters away from the given center
. Because of how the Mercator projection used by the Maps API renders a sphere on a apartment surface, this will announced as an almost perfect circumvolve on the map when located near the equator, and will appear increasingly not-round (on the screen) as the circumvolve moves away from the equator.
The following lawmaking snippet adds a circle to the map by constructing a CircleOptions
object and calling GoogleMap.addCircle(CircleOptions)
:
Java
// Instantiates a new CircleOptions object and defines the center and radius CircleOptions circleOptions = new CircleOptions() .eye(new LatLng(37.4, -122.1)) .radius(1000); // In meters // Become dorsum the mutable Circle Circle circumvolve = map.addCircle(circleOptions);
Kotlin
// Instantiates a new CircleOptions object and defines the eye and radius val circleOptions = CircleOptions() .centre(LatLng(37.4, -122.ane)) .radius(k.0) // In meters // Go back the mutable Circle val circumvolve = map.addCircle(circleOptions)
To change the shape of the circle subsequently it has been added, y'all can call Circle.setRadius()
or Circle.setCenter()
and provide new values.
You can customize the advent of the circle both before adding it to the map and later on it has been added to the map. See the department on customizing appearances beneath for farther details.
Circle events
By default, circles are not clickable. You tin can enable and disable the clickability by calling GoogleMap.addCircle()
with CircleOptions.clickable(boolean)
, or by calling Circle.setClickable(boolean)
.
Apply an OnCircleClickListener
to listen to click events on a clickable circle. To set this listener on the map, call GoogleMap.setOnCircleClickListener(OnCircleClickListener)
.
When a user clicks on a circumvolve, yous will receive an onCircleClick(Circle)
callback, as shown in the following lawmaking sample:
Java
Circle circle = map.addCircle(new CircleOptions() .center(new LatLng(37.4, -122.ane)) .radius(g) .strokeWidth(10) .strokeColor(Colour.Light-green) .fillColor(Color.argb(128, 255, 0, 0)) .clickable(true)); map.setOnCircleClickListener(new GoogleMap.OnCircleClickListener() { @Override public void onCircleClick(Circumvolve circle) { // Flip the r, grand and b components of the circumvolve'due south stroke color. int strokeColor = circumvolve.getStrokeColor() ^ 0x00ffffff; circle.setStrokeColor(strokeColor); } });
Kotlin
val circle = map.addCircle( CircleOptions() .heart(LatLng(37.iv, -122.1)) .radius(grand.0) .strokeWidth(10f) .strokeColor(Colour.Light-green) .fillColor(Color.argb(128, 255, 0, 0)) .clickable(true) ) map.setOnCircleClickListener { // Flip the r, g and b components of the circle's stroke color. val strokeColor = it.strokeColor xor 0x00ffffff information technology.strokeColor = strokeColor }
Customizing appearances
You can modify the advent of a shape both before it has been added to the map (by specifying the desired property on the options object) or after it has been added to the map. Getters are also exposed for all properties and then that you can hands access the current land of the shape.
The post-obit snippet adds a thick blue polyline with geodesic segments from Melbourne to Perth. The sections below will explain these properties in more particular.
Java
Polyline polyline = map.addPolyline(new PolylineOptions() .add(new LatLng(-37.81319, 144.96298), new LatLng(-31.95285, 115.85734)) .width(25) .color(Color.Bluish) .geodesic(truthful));
Kotlin
val polyline = map.addPolyline( PolylineOptions() .add(LatLng(-37.81319, 144.96298), LatLng(-31.95285, 115.85734)) .width(25f) .color(Color.Bluish) .geodesic(true) )
Note: While most of these can be applied to any of the shapes described, some of the properties may not brand sense for certain shapes (east.g., a Polyline tin can't have a fill color because information technology doesn't have an interior).
Stroke color
The stroke colour is a 32-flake blastoff-reddish-greenish-bluish (ARGB) integer specifying the opacity and color of the stroke of the shape. Set this property on the shape's options object by calling *Options.strokeColor()
(or PolylineOptions.color()
in the case of a polyline). If unspecified, the default stroke color is blackness (Color.Black
).
After the shape has been added to the map, the stroke color may be accessed past calling getStrokeColor()
(or getColor()
for a polyline) and may be changed by calling setStrokeColor()
(setColor() for a polyline
).
Fill colour
Fill colour only applies to polygons and circles. It does not utilize to polylines equally they do not accept defined interiors. For a polygon, the regions inside its holes are not function of the interior of the polygon and will non be colored in if a fill color is set.
The make full colour is a 32-scrap alpha-red-light-green-blue (ARGB) integer specifying the opacity and color of the interior of the shape. Fix this property on the shape'south options object by calling *Options.fillColor()
. If unspecified, the default stroke color is transparent (Color.TRANSPARENT
).
Later on the shape has been added to the map, the fill up color may exist accessed by calling getFillColor()
and may be changed by calling setFillColor()
.
Stroke width
The width of the line stroke, equally a float in pixels (px). The width does not scale when the map is zoomed (i.e., a shape will have the aforementioned stroke width at all zoom levels). Set this property on the shape'due south option object past calling *Options.strokeWidth()
(or PolylineOptions.width()
for a polyline). If unspecified, the default stroke with is 10 pixels.
Afterward the shape has been added to the map, the stroke width may be accessed past calling getStrokeWidth()
(or getWidth()
for a polyline) and may exist inverse by calling setStrokeWidth()
(setWidth() for a polyline
).
Stroke pattern
The default stroke blueprint is a solid line for polylines and for the outlines of polygons and circles. You tin can specify a custom stroke pattern of PatternItem
objects, where each item is a dash, a dot, or a gap.
The following sample sets the pattern for a polyline to a repeated sequence of a dot, followed by a gap of length 20 pixels, a dash of length 30 pixels, and another 20-pixel gap.
Coffee
List<PatternItem> pattern = Arrays.asList( new Dot(), new Gap(twenty), new Dash(xxx), new Gap(twenty)); polyline.setPattern(design);
Kotlin
val blueprint = listOf( Dot(), Gap(20F), Dash(30F), Gap(20F) ) polyline.blueprint = pattern
The blueprint repeats along the line, starting with the start blueprint item at the starting time vertex specified for the shape.
Joint types
For polylines and the outlines of polygons, you can specify a bevel or circular JointType
to replace the default stock-still miter articulation type.
The following sample applies a round joint type to a polyline:
Java
polyline.setJointType(JointType.Round);
Kotlin
polyline.jointType = JointType.Round
The joint blazon affects the internal bends in the line. If the line has a stroke pattern that includes dashes, the joint type likewise applies when a nuance straddles a articulation. Joint types practise not affect dots, as they are always circular.
Line caps
You lot can specify a Cap
style for each end of a polyline. The options are butt (default), square, round, or a custom bitmap. Set the style in PolylineOptions.startCap
and PolylineOptions.endCap
, or use the advisable getter and setter methods.
The following snippet specifies a circular cap at the get-go of a polyline.
Coffee
polyline.setStartCap(new RoundCap());
Kotlin
polyline.startCap = RoundCap()
The following snippet specifies a custom bitmap for the end cap:
Java
polyline.setEndCap( new CustomCap(BitmapDescriptorFactory.fromResource(R.drawable.arrow), 16));
Kotlin
polyline.endCap = CustomCap(BitmapDescriptorFactory.fromResource(R.drawable.arrow), 16F)
When you apply a custom bitmap, y'all should specify a reference stroke width in pixels. The API scales the bitmap appropriately. The reference stroke width is the stroke width that you used when designing the bitmap image for the cap, at the original dimension of the image. The default reference stroke width is ten pixels. Hint: To determine the reference stroke width, open up your bitmap epitome at 100% zoom in an prototype editor, and plot the desired width of the line stroke relative to the prototype.
If you lot use BitmapDescriptorFactory.fromResource()
to create the bitmap, make sure y'all employ a density-contained resources (nodpi).
Geodesic segments
The geodesic setting only applies to polylines and polygons. Information technology does not apply to circles considering they are not divers equally a collection of segments.
The geodesic setting determines how the line segments between sequent vertices of the polyline/polygon are drawn. Geodesic segments are those that follow the shortest path forth the Globe'southward surface (a sphere) and oft appear as curved lines on a map with a Mercator projection. Non-geodesic segments are drawn as direct lines on the map.
Set this property on the shape'due south option object by calling *Options.geodesic()
where truthful
indicates the segments should be drawn as geodesics and false
indicates the segments should be drawn every bit direct lines. If unspecified, the default is not-geodesic segments (false
).
After the shape has been added to the map, the geodesic setting may exist accessed by calling isGeodesic()
and may exist inverse past calling setGeodesic()
.
Z-alphabetize
The z-index specifies the stack order of this shape, relative to other overlays (other shapes, ground overlays and tile overlays) on the map. An overlay with a high z-index is drawn above overlays with lower z-indexes. Two overlays with the same z-index are drawn in an arbitrary club.
Note that markers are always drawn above other overlays, regardless of the z-alphabetize of the other overlays.
Set up this property on the shape's options object by calling *Options.zIndex()
. If unspecified, the default z-index is 0
. Later on the shape has been added to the map, the z-index may be accessed by calling getZIndex()
and may be changed by calling setZIndex()
.
Visibility
The visibility specifies whether the shape should be drawn on the map, where truthful
indicates it should be drawn and false
indicates it should non. It allows yous to temporarily not display a shape on the map. To permanently remove shape from the map, call remove()
on that shape.
Set this property on the shape's options object by calling *Options.visible()
. If unspecified, the default visibility is true
. After the shape has been added to the map, the visibility may be accessed by calling isVisible()
and may be inverse by calling setVisible()
.
Associate data with a shape
You lot can shop an arbitrary data object with a polyline, polygon or circumvolve using the shape's setTag()
method, and retrieve the object using getTag()
. For instance, call Polyline.setTag()
to store a data object with a polyline, and telephone call Polyline.getTag()
to call back the data object.
The code below defines an capricious tag (A
) for the specified polyline:
Java
Polyline polyline = map.addPolyline((new PolylineOptions()) .clickable(true) .add together(new LatLng(-35.016, 143.321), new LatLng(-34.747, 145.592), new LatLng(-34.364, 147.891), new LatLng(-33.501, 150.217), new LatLng(-32.306, 149.248), new LatLng(-32.491, 147.309))); polyline.setTag("A");
Kotlin
val polyline = map.addPolyline( PolylineOptions() .clickable(truthful) .add( LatLng(-35.016, 143.321), LatLng(-34.747, 145.592), LatLng(-34.364, 147.891), LatLng(-33.501, 150.217), LatLng(-32.306, 149.248), LatLng(-32.491, 147.309) ) ) polyline.tag = "A"
Here are some examples of scenarios when it'south useful to store and think information with shapes:
- Your app may cater for different types of shapes, and you want to treat them differently when the user clicks them.
- You lot may be interfacing with a system that has unique tape identifiers, where the shapes stand for specific records in that system.
- The shape data may betoken a priority to determine the z-index for the shape.
Source: https://developers.google.com/maps/documentation/android-sdk/shapes
0 Response to "Draw Circle on Google Maps Android"
Post a Comment