【问题标题】:issue with drawing polygons on a Google maps View on Android在 Android 上的 Google 地图视图上绘制多边形的问题
【发布时间】:2012-04-18 21:05:46
【问题描述】:

所以我正在为安卓制作游戏,我需要在谷歌地图窗口上覆盖区域或多边形。

到目前为止,我做了以下工作:

    class polygonOverlay extends Overlay {
    //this is the array of vertices we need to draw
    GeoPoint[] vet;
    Point[] points;
    private float[] fVet;
    //get the vertices

    public polygonOverlay(GeoPoint[] v) {
        vet = v;
        points = new Point[v.length];
        fVet = new float[(v.length)*2];

    }

    //this is how we draw it.
    @Override
    public void draw(Canvas canvas, MapView mapv, boolean shadow) {

        super.draw(canvas,mapv, shadow);

        //do some things
        //set all the points to a point.
        for(int i = 0; i < points.length; i++) {
            points[i] = new Point();
        }

        //convert from the array of geoPoints to the array of points using the projection.
        for(int i = 0; i < vet.length; i++){
        projection.toPixels(vet[i], points[i]);

        }

        //convert the point to the float array
        for(int i = 0; i < points.length; i++) {
        fVet[2*i] = points[i].x;
        fVet[(2*i)+1]  = points[i].y;
        }
        //things be done...

        //create a array of int colors.
        int[] colorArray = new int[points.length];

        for(int i = 0; i < points.length; i++) {
            colorArray[i] = Color.RED;
        }

        //if we are drawing a shadow, then dont draw anything
        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(4);
        //lets draw some things.
        canvas.drawLine(points[1].x, points[1].y, 200, 200, mPaint);
        canvas.drawVertices(
                Canvas.VertexMode.TRIANGLES,
                fVet.length,
                fVet,
                0,
                null,
                0,
                colorArray,
                0,
                null,
                0,
                0,
                mPaint
                ); 


    }

}

但问题是它不会显示多边形。我已经尝试了所有方法,昨晚一直到凌晨 2 点才这样做,但它就是行不通。

我开始认为这是我的手机而不是我的密码...

谁能看到我做错了什么?

【问题讨论】:

  • 你有没有尝试在画布上画一些简单的东西?
  • 是的,我画了一条线,它有效。它只是不起作用的drawVertices

标签: java android polygon android-canvas


【解决方案1】:

多边形创建很简单:

首先定义一个Paint对象:

Paint mPaint = new Paint();
mPaint.setStrokeWidth(2);  //2 pixel line width
mPaint.setColor(0xFF097286); //tealish with no transparency
mPaint.setStyle(Paint.Style.STROKE); //stroked, aka a line with no fill
mPaint.setAntiAlias(true);  // no jagged edges, etc

然后绘制路径:

yourCanvas.drawPath(path,mPaint);

这里是地图叠加的链接:android maps circle overlay, dynamically change radius?

【讨论】:

【解决方案2】:

bug in the drawVertices method 要求 colorArray 与代码中的 fVet 数组大小相同。
注意:只有第一个fVet/2颜色实际用于绘制,其他被忽略。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多