开发者

Overlay only draws line between first 2 GPS points in Android

开发者 https://www.devze.com 2022-12-24 07:11 出处:网络
I am experiencing an unusual error using ItemizedOverlay in Android. I am creating a GPS tracking device that plots a route between waypoints stored in a database.

I am experiencing an unusual error using ItemizedOverlay in Android.

I am creating a GPS tracking device that plots a route between waypoints stored in a database.

When I provide the first two sets of longitude and latitude points through the emulator in Eclipse, it draws a red line just how I want it, but if I send another GPS point, it animates to the point, but does not draw a line from the last point.


public class MyOverlay extends ItemizedOverlay<OverlayItem> 
{

   // private Projection projection;
private Paint linePaint;
private Vector<GeoPoint> 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) {
    points.addElement(point);
}


public void draw(Canvas canvas, MapView view, boolean shadow) {
    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开发者_StackOverflow社区.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;
}
}


In your code the draw() methhod is called only once and then it is never recalled. The populate method will repopulate all the overlayes points which is there in your list each time. So it will again call the draw method which will draw a line for you.

package com.example.mymap;
import java.util.ArrayList;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class ItemOverLay extends ItemizedOverlay<OverlayItem> {

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
GeoPoint prePoint=null,currentPoint=null;
MapView mapView=null;
Paint paint=new Paint();

public ItemOverLay(GeoPoint prePoint,GeoPoint currentPoint, Drawable defaultMarker,MapView mapview) {

    super(boundCenterBottom(defaultMarker));
    this.currentPoint=currentPoint;
    this.prePoint = prePoint;
    mapView=mapview;
    // TODO Auto-generated constructor stub
}
public ItemOverLay(Drawable defaultMarker) {
    super(defaultMarker);
    // TODO Auto-generated constructor stub
}


public void addOverlay(OverlayItem item){
    mOverlays.add(item);
    populate();
}
@Override
protected OverlayItem createItem(int i) {
     // TODO Auto-generated method stub
    return mOverlays.get(i);
}



    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {

        super.draw(canvas, mapView, shadow);

        Paint paint=new Paint();
        Point screenCoords=new Point();
        Point screenCoords1=new Point();

        mapView.getProjection().toPixels(prePoint, screenCoords);
        int x1=screenCoords.x;
        int y1=screenCoords.y;

        mapView.getProjection().toPixels(currentPoint, screenCoords1);
        int x2=screenCoords1.x;
        int y2=screenCoords1.y;

        paint.setStrokeWidth(1);
        canvas.drawLine(x1, y1, x2, y2, paint);




    }

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

}


Change this line

view.getProjection().toPixels(points.get(i), point);

to

point = view.getProjection().toPixels(points.get(i), null);

then it should work.

0

精彩评论

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