OSM Android(osmdroid)自定义叠加层,如何绘制第二个自定义形状

时间:2019-02-12 13:32:45

标签: android android-canvas osmdroid

我创建了一个类,该类扩展了Overlay类以创建曲线。初次使用时,一切正常,但是当我尝试绘制第二条曲线时,地图保留了draw方法,并且不允许我再绘制其他任何东西。我想每次按下按钮时都绘制一个单独的形状

private void drawCurvedLine() {
            OsmCurvedLine osmCurvedLine = new OsmCurvedLine(c);
            osmCurvedLine.setFillColour(ShapeColour.getInstance(c).defaultPinColour());
            osmCurvedLine.setBorderColour(ShapeColour.getInstance(c).paintBorderColour());
            map.getMapView().getOverlays().add(osmCurvedLine);
            osmCurvedLine.setEditMode(true);
}

public class OsmCurvedLine extends Overlay {
        private boolean editMode = false;
        private float x1, y1;
        private GeoPoint p1 = null, p2 = null;
        private Context c;
        private Paint fillColour, borderColour;
        private ArrayList<GeoPoint> geoPoints = new ArrayList<>();

        public OsmCurvedLine(Context context) {
            this.c = context;
        }

        @Override
        public void draw(Canvas canvas, MapView mapView, boolean shadow) {

                Point screenPts1 = new Point();
                Point screenPts2 = new Point();
                Point screenPts3 = new Point();

                // draw dot on first point
                if (p1 != null) {
                    mapView.getProjection().toPixels(p1, screenPts1);
                    canvas.drawCircle(screenPts1.x, screenPts1.y, 20, getFillColour());
                }
                for (int i = 0; i < geoPoints.size(); i++) {
                    mapView.getProjection().toPixels(geoPoints.get(i), screenPts2);
                    mapView.getProjection().toPixels(geoPoints.get(i + 1), screenPts3);

                    canvas.drawLine(screenPts2.x, screenPts2.y, screenPts3.x, screenPts3.y, getBorderColour());
                }
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e, MapView mapView) {
            if (editMode) {
                x1 = e.getX();
                y1 = e.getY();

                if (p1 == null) {
                    p1 = (GeoPoint) mapView.getProjection().fromPixels((int) x1, (int) y1);
                }
                p2 = (GeoPoint) mapView.getProjection().fromPixels((int) x1, (int) y1);
                geoPoints.add(p2);
                mapView.invalidate();

                return true;
            }
            return false;
        }

    //getters setters
    } 

0 个答案:

没有答案