开发者

Get current OverlayItem being drawn

I want to be able to get some reference to the curent object being drawn

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

Above is my draw method and I want to extend the draw method to write the title underneath each item for example. This would require the .getTitle() method from the Overlay开发者_运维知识库Item. Possibly some tracking of objects outside of this method but not sure where to put it....


I've done something similar. I've added some markers to a MapView and afterwards connecting them with a line.

I have a class LineOverlay which extends Overlay. In the constructor it gets the list of items to be connected by lines.

Something like:

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);
}

and then on the onDraw() I do this:

@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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜