Skip to content
Nutiteq edited this page Jan 31, 2014 · 1 revision

Here is a sample how to draw circle to GeometryLayer, based on location and circle radius.

  // 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);

The drawing itself is quite simple, only a bit tricky thing is to calculate radius in appropriate units.

// 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);
 }
Clone this wiki locally