2010-03-31 52 views
1

我正在構建將GPS位置存儲在SQLite數據庫中的應用程序,然後通過在點之間繪製紅線將數據輸出到MapView上。在Android中使用ItemizedOverlay添加可見的「標記」以表示使用ItemizedOverlay的MapView地理位置

我希望能夠爲這些點中的每一個以及紅線顯示圖形標記(圖像)。我的代碼如下:

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) { 
    populate(); 
    points.addElement(point); 

} 

//public void setProjection(Projection projection) { 
// this.projection = projection; 
// } 

public void draw(Canvas canvas, MapView view, boolean shadow) { 
    populate(); 
    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; 
} 
} 

什麼是實現爲每個GeoPoint添加標記最簡單的方法?

回答

2

看看http://www.vtgroup.com/index.html#MapLocation,看看它是否回答你的問題。

+0

你好,傑伊,這正是我正在尋找的,canvas.drawBitmap方法。謝謝! – jcrowson 2010-03-31 18:48:06

+0

傑伊,我似乎收到一個錯誤的行canvas.drawBitmap(的BitmapFactory.decodeResource(getResources(),R.drawable.arrow) 「方法getResources()是未定義的類型MyOverlay」 – jcrowson 2010-03-31 19:25:06

+1

你需要一個用於調用getResources()的上下文對象與每次繪製時訪問資源和創建新繪圖對象相反,應該在地圖活動中創建繪圖並將其傳遞到疊加層或將地圖活動的上下文傳遞到疊加層,在繪圖方法中,最好避免創建新對象和執行邏輯,因爲它非常慢。根據用例,我還建議在其他地方收集您的投影好, – 2010-03-31 19:59:40

相關問題