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

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

}

于 2010-03-30T06:59:39.327 回答
1

更改此行

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

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

那么它应该可以工作。

于 2011-06-17T12:33:32.617 回答