views:

52

answers:

1

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 OverlayItem. Possibly some tracking of objects outside of this method but not sure where to put it....

A: 

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.

Macarse
I have seen this example quite a lot and can see how this would draw a line between the points. I cannot see how you would use the getTitle and retrieve the name to write that and more importantly at the correct point.
Lee Armstrong
@Lee Armstrong Inside the draw method you can call: `mItemizedOverlay.getItem(i).getTitle()` to get the title. The position where to draw can be done with the information of `.getPoint()` and `projection.toPixels`.
Macarse
@Macarse thanks. I see what you mean now. Take a look at this... http://www.john-ambler.com/ais/google.html How would I go about drawing those types of Polylines that are not connected?
Lee Armstrong
@Lee Armstrong: Use `canvas.drawLine` between your marker x,y and the end of the line x,y
Macarse