Android中Overlay只在第一二个GPS点之间绘制线条

3

我在使用Android中的ItemizedOverlay时遇到了一个不寻常的错误。

我正在创建一个GPS跟踪设备,用于绘制在数据库中存储的航点之间的路线。

当我在Eclipse模拟器中提供前两组经度和纬度点时,它按照我想要的方式绘制了一条红线,但如果我发送另一个GPS点,则会动画到该点,但不会从上一个点绘制一条线。


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.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;
}
}

你能调试一下canvas.drawLine(lastPoint.x, lastPoint.y, point.x, point.y, linePaint)这部分吗?它被调用了多少次?使用了哪些参数? - Fortega
我该如何使用Logcat来特别调试它? - jcrowson
嘿,LodSnoutimus,你可否发布一下你的工作类?我很好奇你是如何让它工作的。谢谢。 - tgai
2个回答

2

在你的代码中,draw()方法只被调用一次,然后就再也没有被调用了。populate方法将重新填充列表中所有覆盖点,因此每次都会再次调用draw方法,从而为你绘制一条线。

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();
    }

}


谢谢回复,Rahul。我已经在addPoint方法中添加了populate()方法,但似乎没有起作用。它仍然没有在第三个点和最后一个点之间画出一条线。 - jcrowson
Rahul,再次感谢你提供的更新代码,非常有帮助!但是,有一件事我不太明白,就是这行代码:mapView.getProjection().toPixels(prePoint, screenCoords);由于prePoint和screenCoords为空,所以我收到了一个nullpointerexception。我需要在我的MapView类中添加或更改任何代码吗?我可以提供代码。谢谢。 - jcrowson
请展示你的代码,我会告诉你哪里出了错,否则我会发送那段代码的示例,它将告诉你如何绘制这些线。 - Rahul Patel
Rahul,谢谢你的帮助。我现在已经成功连接了所有的点! :) - jcrowson
我猜其他人(包括我)会很高兴,如果你告诉他们你是如何连接所有点的,谢谢! - myell0w

1

更改此行

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

to

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

那么它应该可以工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接