Having the following code to draw circle (taken from Google Play Services "maps" sample):
PolylineOptions options = new PolylineOptions();
int radius = 5; //What is that?
int numPoints = 100;
double phase = 2 * Math.PI / numPoints;
for (int i = 0; i <= numPoints; i++) {
options.add(new LatLng(SYDNEY.latitude + radius * Math.sin(i * phase),
SYDNEY.longitude + radius * Math.cos(i * phase)));
}
int color = Color.RED;
mMap.addPolyline(options
.color(color)
.width(2));
This is what gets drawn on different part of the world:
As you see circles are not really circles and even second one is ellipse basically.
I guess that "anti-aliasing" of circle depending on number of points in int numPoints
variable.
- What is
int radius = 5
variable in example code? I mean what measure it is? - And main question what would be correct way of drawing nice circle with given radius in meters? Something smiliar to what we had in api v1 with
canvas.drawCircle()
UPDATE --------------------
OK after improving math I was able to draw "right" circle:
private void addCircle(LatLng latLng, double radius)
{
double R = 6371d; // earth's mean radius in km
double d = radius/R; //radius given in km
double lat1 = Math.toRadians(latLng.latitude);
double lon1 = Math.toRadians(latLng.longitude);
PolylineOptions options = new PolylineOptions();
for (int x = 0; x <= 360; x++)
{
double brng = Math.toRadians(x);
double latitudeRad = Math.asin(Math.sin(lat1)*Math.cos(d) + Math.cos(lat1)*Math.sin(d)*Math.cos(brng));
double longitudeRad = (lon1 + Math.atan2(Math.sin(brng)*Math.sin(d)*Math.cos(lat1), Math.cos(d)-Math.sin(lat1)*Math.sin(latitudeRad)));
options.add(new LatLng(Math.toDegrees(latitudeRad), Math.toDegrees(longitudeRad)));
}
mMap.addPolyline(options.color(Color.BLACK).width(2));
}
However anti-aliasing of circle I guess is somewhat beyond control, and on some zoom levels circle might get ugly:
options.add(
is very basic and pretends the earth is a flat surface, just like before Copernicus. If you draw it on smaller areas you'll get a rounder circle. If you want a precise circle you'll have extend that math to proper take into account the shape of Earth. – BudiusPolyline
instead ofPolygon
to draw the circle? I tried using Polygon and i got the same output. So, you have any idea which one of them is best to use? Thank You. – Archie.bpgc