Skip to content

Instantly share code, notes, and snippets.

@nutiteq
Created January 30, 2014 18:38
Show Gist options
  • Save nutiteq/8715686 to your computer and use it in GitHub Desktop.
Save nutiteq/8715686 to your computer and use it in GitHub Desktop.
Draw circle in Nutiteq SDK
{
// create layer and add a circle to it
GeometryLayer locationLayer = new GeometryLayer(mapView.getLayers().getBaseProjection());
mapView.getComponents().layers.addLayer(locationLayer);
circle(-122.416667f, 37.766667f, 100, locationLayer);
}
// helper to draw a circle to given layer
private void circle(float lat, float lon, float circleRadius, GeometryLayer layer){
// number of circle line points
int NR_OF_CIRCLE_VERTS = 18;
List<MapPos> circleVerts = new ArrayList<MapPos>(NR_OF_CIRCLE_VERTS);
MapPos circlePos = layer.getProjection().fromWgs84(lat, lon);
// width of map to scale circle
float projectionScale = (float) layer.getProjection().getBounds().getWidth();
// convert radius from meters to map units
float circleScale = circleRadius / 7500000f * projectionScale;
for (float tsj = 0; tsj <= 360; tsj += 360 / NR_OF_CIRCLE_VERTS) {
MapPos mapPos = new MapPos(circleScale * Math.cos(tsj * Const.DEG_TO_RAD) + circlePos.x, circleScale * Math.sin(tsj * Const.DEG_TO_RAD) + circlePos.y);
circleVerts.add(mapPos);
}
LineStyle style = LineStyle.builder().setWidth(0.1f).setColor(Color.argb(192, 255, 255, 0)).build();
Line circle = new Line(circleVerts, null, style, null);
layer.add(circle);
}
@Tonioul
Copy link

Tonioul commented Jan 30, 2014

Hi Jaak,
Thanks for the code sample it works nicely.
I think there is a problem concerning the circle radius conversion though.
The comment says it should be in meters so for a circle with a radius of 2km (it's for an antenna's signal) I put 2000 as the circleRadius parameter.
The circle is drawn, but is way too big. I checked on google maps and it's around 8km.
Any ideas on what might be the issue ?

I could put 500 instead of 2000, It would draw the correct circle but it's not very clean :)

@jaakla
Copy link

jaakla commented Oct 1, 2014

This function works on given latitudes only, there is "magic number" 7500000f in the calculations. Please use better one, which is independent from latitude: https://gist.github.com/jaakla/bd26bb02109f17e8135c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment