我正在使用GeoPoints的ArrayList来在地图上绘制点并将它们与直线连接。我有“点”部分的工作,但没有线。我的ItemizedOverlay类:

private class myItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
    private ArrayList<OverlayItem> items = new ArrayList<OverlayItem>();
    private ArrayList<GeoPoint> geoPoints = new ArrayList<GeoPoint>();
    private int color;

    public myItemizedOverlay (Drawable marker, ArrayList<GeoPoint> gp, int c)
    {
        super(marker);
        geoPoints = gp;
        color = c;

        boundCenter(marker);

        for (int i = 0; i < geoPoints.size() - 1; i++)
        {
            items.add(new OverlayItem(geoPoints.get(i)));
        }

        populate();
    }

    @Override
    protected OverlayItem createItem(int i)
    {
        return items.get(i);
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow)
    {
        super.draw(canvas, mapView, false);

        Paint paint = new Paint();
        paint.setColor(color);
        paint.setDither(true);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(2);

        Point p1 = new Point();
        Point p2 = new Point();

        for (int i = 1; i < geoPoints.size() - 1; i++)
        {
            mapView.getProjection().toPixels(geoPoints.get(i), p1);
            int x1 = p1.x;
            int y1 = p1.y;

            mapView.getProjection().toPixels(geoPoints.get(i - 1), p2);
            int x2 = p2.x;
            int y2 = p2.y;

            canvas.drawLine(x1, y1, x2, y2, paint);
        }
    }

    @Override
    public int size()
    {
        return items.size();
    }
}


致电:

myItemizedOverlay mo = new myItemizedOverlay(marker, geoPoints, color);
mapView.getOverlays().add(mo);


我发现在覆盖的“绘制”方法中“ geoPoints”为空,我不知道为什么。更令人困惑的是,“ color”为/ not /为空,并且“ color”和“ geoPoints”的分配方式相同。有人可以推荐解决方案吗?

最佳答案

您正在将gp分配给geoPoints。我希望您在其他地方清除gp。您应该做一个副本:

geoPoints = new ArrayList<GeoPoint>(geoPoints.size());
for (int i = 0; i < geoPoints.size() - 1; i++)
        {
            geoPoints.add(gp.get(i));
            items.add(new OverlayItem(geoPoints.get(i)));
        }


试试看,让我知道它是如何工作的。

09-28 04:00