views:

456

answers:

1

I am currently building an application that allows users to track where their phone has been on a Google Map. At the moment, when the onLocationChanged() method is called, the application stores the current GPS longitude and latitude in a database and calls the animateTo() method to the current position.

Using SDK 1.5, how would I go about connecting these points with a coloured line drawn on the MapView using an Overlay?.

+1  A: 

You have to create own class extending ItemizedOverlay and in draw method draw the line on Canvas.

For example:

public class MyOverlay extends ItemizedOverlay<OverlayItem> {
    private Projection projection;
    private Point linePaint;
    private Vector<GeoPoint> points;
    public MyOverlay(Drawable defaultMarker) {
        points = new Vector<GeoPoint>()
        linePaint = new Paint();
        //set color, stroke width etc.
    }

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

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

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

In onLocationChanged() you should add new geopoint via overlay.addPoint. In onCreate() of Activity where MapView will be displayed you must add

overlay = new MyOverlay(null); //overlay must be accessible from onLocationChanged
map.getOverlays().add(overlay); //map = (MapView) findViewById(R.id.mapview)

You should also check in draw (or somewhere else) if the point will be in visible rectangle to increase drawing speed.

I haven't tried to compile this so don't blame me if there are some minor mistakes.

skyman
Thanks for the reply, I have implemented the class WaypointsOverlay, however, I cannot find anything about the addPoint keyword that needs to go under onLocationChanged().
LordSnoutimus