3

我希望能够获得对正在绘制的当前对象的一些参考

@Override
        public void draw(Canvas canvas, MapView mapView,boolean shadow) {
            //Log.i("DRAW","MARKER");
            super.draw(canvas, mapView, false);
        }

以上是我的 draw 方法,我想扩展 draw 方法以在每个项目下面写标题。这需要来自 OverlayItem 的 .getTitle() 方法。可能在此方法之外对对象进行了一些跟踪,但不确定将其放在哪里....

4

1 回答 1

0

我做过类似的事情。我在 a 添加了一些标记,MapView然后用一条线将它们连接起来。

我有一个LineOverlay扩展类Overlay。在构造函数中,它获取要按行连接的项目列表。

就像是:

public LineOverlay(ItemizedOverlay<? extends OverlayItem> itemizedOverlay, int lineColor) {
    mItemizedOverlay = itemizedOverlay;
    colorRGB = lineColor;
    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setColor(colorRGB);
    mPaint.setStrokeWidth(LINE_WIDTH);
}

然后在onDraw()我这样做:

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    if ( mItemizedOverlay.size() == 0 || mItemizedOverlay.size() == 1 )
        return;

    Projection projection = mapView.getProjection();

    int i = 0;

    while( i < mItemizedOverlay.size() - 1 ){
        OverlayItem begin = mItemizedOverlay.getItem(i);
        OverlayItem end = mItemizedOverlay.getItem(i+1);
        paintLineBetweenStations(begin,end,projection,canvas);
        i++;
    }

    super.draw(canvas, mapView, shadow);
}

private void paintLineBetweenStations(OverlayItem from, OverlayItem to, Projection projection, Canvas canvas){
    GeoPoint bPoint = from.getPoint();
    GeoPoint ePoint = to.getPoint();

    Point bPixel = projection.toPixels(bPoint, null);
    Point ePixel = projection.toPixels(ePoint, null);

    canvas.drawLine(bPixel.x, bPixel.y, ePixel.x, ePixel.y, mPaint);
}

In your case you can do something similar creating a SubtitleOverlay which extends Overlay that receives all items in the constructor and then on the draw method create a subtitle in the correct position.

于 2010-07-12T14:53:35.297 回答