views:

651

answers:

2

Hello,

I am building an application which stores GPS locations in a SQLite database and then outputs the data onto a MapView using an Overlay by drawing a red line between the points.

I want to be able to show graphical markers (images) for each of these points as well as the red line. My code is as follows:

public class MyOverlay extends ItemizedOverlay {

// private Projection projection; private Paint linePaint; private Vector points;

public MyOverlay(Drawable defaultMarker) {
    super(defaultMarker);
    points = new Vector<GeoPoint>();
    //set colour, stroke width etc.
    linePaint = new Paint();
    linePaint.setARGB(255, 255, 0, 0);
    linePaint.setStrokeWidth(3);
    linePaint.setDither(true);
    linePaint.setStyle(Style.FILL);
    linePaint.setAntiAlias(true);
    linePaint.setStrokeJoin(Paint.Join.ROUND);
    linePaint.setStrokeCap(Paint.Cap.ROUND);

}

public void addPoint(GeoPoint point) {
    populate();
    points.addElement(point);

}

//public void setProjection(Projection projection) {
 //   this.projection = projection;

// }

public void draw(Canvas canvas, MapView view, boolean shadow) {
    populate();
    int size = points.size();
    Point lastPoint = new Point();
    if(size == 0) return;
    view.getProjection().toPixels(points.get(0), lastPoint);
    Point point = new Point();
    for(int i = 1; i<size; i++){
       view.getProjection().toPixels(points.get(i), point);
        canvas.drawLine(lastPoint.x, lastPoint.y, point.x, point.y, linePaint);
        lastPoint = point;

    }
}

@Override
protected OverlayItem createItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int size() {
    // TODO Auto-generated method stub
    return 0;
}

}

What would be the easiest way to implement adding markers for each GeoPoint?

Thanks

+1  A: 

Take a look at http://www.vtgroup.com/index.html#MapLocation and see if it answers your question.

Jay

AndroidRef.com
Hello Jay, that is exactly what I was looking for, the canvas.drawBitmap method. Thanks!
LordSnoutimus
Jay, i seem to be receiving an error with the line canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.arrow)"The method getResources() is undefined for the type MyOverlay"
LordSnoutimus
you need a Context object to call getResources(). As opposed to accessing your resources and creating a new drawable every time you draw you should create the drawable in your map activity and pass it to the overlay or pass a context from your map activity to your overlay and get the drawable one time in the constructor. rule of thumb says its best to avoid creating new objects and doing logic in your draw method as it is very slow. Depending on the use case I would also recommend gathering your projections somewhere else as well.
Dave.B
Dave, thanks for the reply. I am actually having an issue with the points where it is only drawing a red line for the first 2 and not for any others. Would this be because of how I have created the projections?
LordSnoutimus
A: 

how do i Context object to call getResources() ??

AT Balls