0

我有这个代码用于在地图点之间画线

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

    if (!shadow) {
        Projection projection = mapview.getProjection();

        for(int i=0; i< puntos.size()-1; i++) {

            Point origen = new Point();
            Point destino = new Point();

            projection.toPixels(puntos.get(i).getPoint(), origen);
            projection.toPixels(puntos.get(i+1).getPoint(), destino);

            Paint paint = new Paint();

            paint.setStyle(Style.STROKE);
            paint.setStrokeWidth(5);
            paint.setColor(Color.GREEN);
            paint.setAntiAlias(true);

            canvas.drawLine(origen.x, origen.y, destino.x, destino.y, paint);
            mapview.invalidate();
        }
    }
super.draw(canvas, mapview, shadow);
}

但是,当我将地图中的点放置得太近或线与另一条线相交时,地图视图会在附近点绘制多条线。所以如果我有点 1、2、3、4。点 1 有一条到点 2、3、4 的画线

任何想法如何解决这个问题?

4

2 回答 2

0

试试下面的代码。它还会用一些颜色填充形状。您可以省略该功能。

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

        Path path = new Path();

        Projection projection = mapView.getProjection();

        for(int j = 0; j < geoArrayist.size(); j++) 
        {
            Iterator<GeoPoint> it = geoArrayist.iterator();
            while(it.hasNext()) 
            {
                GeoPoint arrayListGeoPoint = it.next();

                Point currentScreenPoint = new Point();
                projection.toPixels(arrayListGeoPoint, currentScreenPoint);

                if(j == 0)
                    path.moveTo(currentScreenPoint.x, currentScreenPoint.y); 
                else
                    path.lineTo(currentScreenPoint.x, currentScreenPoint.y);
            }                 
        }
        canvas.drawPath(path, mPaint);
于 2012-10-10T11:28:23.473 回答
0

更改您的代码:

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

if (!shadow) { 
    if(puntos.size() == 0) return;

    //initialization
    Point origen = new Point(); 
    Point destino = new Point(); 


    Paint paint = new Paint(); 

    paint.setStyle(Style.STROKE); 
    paint.setStrokeWidth(5); 
    paint.setColor(Color.GREEN); 
    paint.setAntiAlias(true); 

    //end of initialization

    Projection projection = mapview.getProjection(); 
    projection.toPixels(puntos.get(0).getPoint(), origen); 

    for(int i=1; i< puntos.size(); i++) { 
        projection.toPixels(puntos.get(i).getPoint(), destino); 
        canvas.drawLine(origen.x, origen.y, destino.x, destino.y, paint); 
    } 
} 
super.draw(canvas, mapview, shadow); 
}

理想情况下,您应该将上面初始化块中的代码移动到覆盖构造函数并制作对象和origen全局变量。这样做,它将提高内存利用率,在覆盖的生命周期内只创建每个对象中的 1 个。destinopaint

祝你好运。

于 2012-10-11T12:42:30.623 回答