简体   繁体   中英

Canvas to draw polygon over mapview in Android

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM