我在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;
}
}
答案 0 :(得分: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();
}
}
答案 1 :(得分:1)
更改此行
view.getProjection().toPixels(points.get(i), point);
到
point = view.getProjection().toPixels(points.get(i), null);
然后它应该工作。