2011-05-06 71 views
5

我正在研究一個應用程序,我需要在Google地圖上繪製路徑,因爲位置變化會顯示用戶的位置。我曾嘗試使用MyLocationOverlay類,以爲我可以重寫繪製位置的方法,但在確定要覆蓋的方法方面我沒有成功。此外,每次繪製位置時,MyLocationOverlay都會繪製一張新地圖。我目前正在使用ItemizedOverlay,並且每當位置發生變化時,都會在列表中添加一個點。這是有效的,當我走路時我得到一條虛線的路徑,但我真的很喜歡一條堅實的路徑。有什麼建議嗎?在Android版Google地圖上跟隨您的位置繪製路徑

我也看到了這個post,但我不能得到它的工作。你是否需要覆蓋層才能在地圖上顯示它?

回答

3

我認爲最簡單的方法是將覆蓋類繼承,然後重寫draw方法。繪製方法非常開放,繪製路徑不應太難。一個例子是這個樣子:然後

public class PathOverlay extends Overlay{ 

    private List<GeoPoint> gpoints; 

    public PathOverlay(List<GeoPoint> gpoints){ 
     this.gpoints = gpoints; 
    } 

    @Override 
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
    { 
     List<Point> mpoints = new ArrayList<Point>(); 

     // Convert to a point that can be drawn on the map. 
     for(GeoPoint g : gpoints){ 
      Point tpoint = new Point(); 
      mapView.getProjection().toPixels(g, tpoint); 
      mpoints.add(tpoint); 
     } 

     Path path = new Path(); 

     // Create a path from the points 
     path.moveTo(mpoints.get(0).x, mpoints.get(0).y); 
     for(Point p : mpoints){ 
      path.lineTo(p.x, p.y); 
     } 

     Paint paint = new Paint(); 
     paint.setARGB(255, 255, 0, 0); 
     paint.setStyle(Style.STROKE); 
     // Draw to the map 
     canvas.drawPath(path,paint); 

     return true; 

    } 
} 

這個類的對象可以被添加到通過調用MapView.getOverlays返回的列表()添加到地圖中。

相關問題