Java-叠加层仅在Android中的前2个GPS点之间绘制线条

我在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;
}
}

解决方法:

在您的代码中,draw()方法仅被调用一次,因此永远不会被调用.填充方法将重新填充列表中每次出现的所有叠加点.因此,它将再次调用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();
    }

}

上一篇:docker 存储驱之overlayFS


下一篇:透明的浮动Android活动不允许更新其背后的内容