Skip to content

Instantly share code, notes, and snippets.

@jaakla
Last active August 29, 2015 14:07
Show Gist options
  • Save jaakla/bd26bb02109f17e8135c to your computer and use it in GitHub Desktop.
Save jaakla/bd26bb02109f17e8135c to your computer and use it in GitHub Desktop.
Draw circle on earth
GeometryLayer geomLayer = new GeometryLayer(proj);
// circle of 1000 km around London
addCircle(geomLayer, new MapPos(0,51), 1000);
mapView.getLayers().addLayer(geomLayer);
/**
* Draw circle around given point, from latitude/longitude
* Note: many parameters are hardcoded, like style, circle resolution etc - feel free to modify or make parametric
* @param gl GeometryLayer where circle is added
* @param circlePos Location of circle center, in longitude (x) and latitude (y)
* @param radius circle radius in kilometers
*/
private void addCircle(GeometryLayer gl, MapPos circlePos, double radius){
// constants
double earthRadius = 6371000; // in m
// input
radius *= 1000; // from km to m
double cLon = circlePos.x * Const.DEG_TO_RAD;
double cLat = circlePos.y * Const.DEG_TO_RAD;
double d = radius / earthRadius;
PolygonStyle polyStyle = PolygonStyle.builder()
.setColor(Color.GREEN & 0x20ffffff)
.setLineStyle(LineStyle.builder().setColor(Color.BLACK).build())
.build();
List<MapPos> mp = new ArrayList<MapPos>();
for(int i=0; i<360; i+=20){
double bearing = i * Math.PI / 180;
double lat = Math.asin(Math.sin(cLat) * Math.cos(d) + Math.cos(cLat) * Math.sin(d) * Math.cos(bearing));
double lon = (cLon + Math.atan2(Math.sin(bearing) * Math.sin(d) * Math.cos(cLat), Math.cos(d) - Math.sin(cLat) * Math.sin(lat))) * Const.RAD_TO_DEG;
lat *= Const.RAD_TO_DEG;
MapPos mapPos = new MapPos(gl.getProjection().fromWgs84(lon, lat));
mp.add(mapPos);
}
Polygon poly = new Polygon(mp, null, polyStyle, null);
gl.add(poly);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment