3

I need to draw a Polygon from several points (I have their latitude, longitude). I am basing my implementation from these two answers: Drawing an empty polygon given a set of points on a Map Overylay (Android 2.1) Drawing a line/path on Google Maps

In my MapOverlayAction.java I set the overlay for some pins like this:

mapOverlays.add(itemizedoverlay);
setLocationOverlay(mapView, mapController);

where itemizedoverlay is an array of OverlayItems

This works fine. But I also need to draw a polygon for these points (where each point is a vertex). So what I do is:

Path path = new Path();

 for (int j = 0; j < itemizedoverlay.size(); j++) {

   GeoPoint gP1 = itemizedoverlay.getItem(j).getPoint();
   Point currentScreenPoint = new Point();

    Projection projection = mapView.getProjection();
    projection.toPixels(gP1, currentScreenPoint);

    if (j == 0)
      path.moveTo(currentScreenPoint.x, currentScreenPoint.y); 
    else
      path.lineTo(currentScreenPoint.x, currentScreenPoint.y);
}

In both of the answersI am basing my solution, the following method is being called:

    Paint   mPaint = new Paint();
    mPaint.setDither(true);
    mPaint.setColor(Color.RED);
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(2);    

    canvas.drawPath(path, mPaint);

My question is, where do I get that canvas from? I have all this code in my activity class.

Thanks!

Community
  • 1
  • 1
marimaf
  • 5,382
  • 3
  • 50
  • 68

1 Answers1

0

You will need to subclass the Overlay class and override the Draw method to get your canvas.

then instantiate your new class and add it to the list of Overlays to have it appear on the map. This question should help.

Community
  • 1
  • 1
riotopsys
  • 564
  • 4
  • 7
  • Thanks, I am using that question on my solution as I quoted in my question. Anyways, now I tried exactly as the answer there says (and as you said it should be done and it works) I just thought it could be done some other way. Thanks – marimaf Nov 09 '11 at 04:14