문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top