2012-06-20 33 views
0

我想通過使用for循環的數組在點之間繪製線條,但我似乎遇到了一些問題。我創建了一個循環遍歷數組,但嘗試使用projection.toPixels(arraylist,point)的循環;它一直在拋出一個錯誤。我看到其他人使用它,他們似乎沒有得到一個錯誤,請大家幫幫忙:Android - 通過陣列在點之間繪製線條

我寫的代碼如下:

public class MyPositionOverlay extends ItemizedOverlay<OverlayItem> { 

public List<GeoPoint> geoPointsArrayList = new ArrayList<GeoPoint>(); 
public ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>(); 
Context context; 
int listSize = overlayItemList.size(); 
//Vector <overlayItemList> vectorList; 
public MyPositionOverlay(Drawable marker, Context c) { 
    super(boundCenterBottom(marker)); 
    // TODO Auto-generated constructor stub 
    populate(); 
    context = c; 
} 
private final int mRadius = 5; 
Location location; 
public Location getLocation() { 
    return location; 
} 
public void setLocation(Location location) {; 
this.location = location; 
} 

public void addItem(GeoPoint point, String title, String snippet){ 
    OverlayItem newItem = new OverlayItem(point, title, snippet); 
    overlayItemList.add(newItem); 
    populate(); 
} 
@Override 
public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
    Projection projection = mapView.getProjection(); 
    Double latitude = location.getLatitude()*1E6; 
    Double longitude = location.getLongitude()*1E6; 
    GeoPoint geoPoint; 
    geoPoint = new 
      GeoPoint(latitude.intValue(),longitude.intValue()); 
    GeoPoint prePoint = null, currentPoint = null; 
    Path linePath = new Path(); 
    Point screenCoords = new Point(); 
    Point screenCoords2 = new Point(); 
    Point lastPoint = new Point(); 
    Point linePoint = new Point(); 
    if (shadow == false) { 
     // Get the current location  
     // Convert the location to screen pixels  
     Point point = new Point(); 
     projection.toPixels(geoPoint, point); 
     RectF oval = new RectF(point.x - mRadius, point.y - mRadius, 
       point.x + mRadius, point.y + mRadius); 
     // Setup the paint 
     Paint paint = new Paint(); 
     paint.setARGB(250, 255, 255, 255); 
     paint.setAntiAlias(true); 
     paint.setFakeBoldText(true); 
     //Text set up 
     Paint textPaint = new Paint(); 
     textPaint.setColor(Color.BLACK); 
     Paint backPaint = new Paint(); 
     backPaint.setARGB(175, 50, 50, 50); 
     backPaint.setAntiAlias(true); 
     RectF backRect = new RectF(point.x + 2 + mRadius, 
       point.y - 3*mRadius, 
       point.x + 65, point.y + mRadius); 
     // Draw the marker  
     canvas.drawOval(oval, paint); 
     canvas.drawRoundRect(backRect, 5, 5, backPaint); 
     canvas.drawText("Here I Am", 
       point.x + 2*mRadius, point.y, 
       textPaint); 

     for (int j = 1; j < overlayItemList.size();j++){ 
      linePath.setLastPoint(4, j - 1); 
      linePath.lineTo(4, j); 
     } 
     linePath.close(); 
     canvas.drawPath(linePath, linePaint); 
    } // End of If statement 
    super.draw(canvas, mapView, shadow); 
} // End of draw method 
public void drawLine(MapView mapView){ 

    System.out.println("Drawing line"); 
    Path linePath = new Path(); 
    Canvas lineCanvas = new Canvas(); 
    Projection lineProjection = mapView.getProjection(); 


    //Line Settings 
    Paint linePaint = new Paint(); 
    linePaint.setStyle(Style.FILL); 
    linePaint.setStrokeWidth(2); 
    linePaint.setARGB(0, 0, 0, 255); 
    linePaint.setAntiAlias(true); 
    linePaint.setStrokeJoin(Paint.Join.ROUND); 
    linePaint.setStrokeCap(Paint.Cap.ROUND); 
    /****************************************************************************/ 

    for (int k = 0; k<overlayItemList.size(); k++){ 
     if(k == overlayItemList.size() -1){ 
      break; 
     } 
     Point from = new Point(); 
     Point to = new Point(); 

     lineProjection.toPixels(overlayItemList.get(k), from); 
     lineProjection.toPixels(overlayItemList.get(k + 1), to); 

     linePath.moveTo(from.x, from.y); 
     linePath.lineTo(to.x, to.y); 
    } 

    lineCanvas.drawPath(linePath, linePaint); 


}//End of drawLine method 

} *

+0

錯誤我得到的是:該方法toPixels(GeoPoint對象,點)在類型投影不適用於參數(OverlayItem,Point) 我使用的代碼來自http://stackoverflow.com/questions/5243146/i-want-to-draw-path-between-multiple-geopoins在Android中,但我不能看到最新錯了 – MurphyApps

+0

ih也見過這個代碼:http://stackoverflow.com/questions/3036139/android-drawing-path-as-overlay-on-mapview 這似乎是我的相同的東西,是我的arraylists? – MurphyApps

回答

1

的方法toPixels( GeoPoint,Point)不適用於參數(OverlayItem,Point)

您會收到此錯誤消息,因爲您嘗試傳遞一個對象t ype OverlayItem應用於期望GeoPoint類型的對象的方法。這不起作用,因爲OverlayItem不從GeoPoint延伸。它有一個getPoint()方法,它返回覆蓋層的GeoPoint,所以你可以使用它。

在你的代碼,你只需要更改以下兩行:

lineProjection.toPixels(overlayItemList.get(k), from); 
lineProjection.toPixels(overlayItemList.get(k + 1), to); 

這些線路應閱讀

lineProjection.toPixels(overlayItemList.get(k).getPoint(), from); 
lineProjection.toPixels(overlayItemList.get(k + 1).getPoint(), to); 
相關問題