2012-10-10 53 views
0

我有地圖點Android的地圖路徑借鑑點十字線

@Override 
public void draw(Canvas canvas, MapView mapview, boolean shadow) { 

    if (!shadow) { 
     Projection projection = mapview.getProjection(); 

     for(int i=0; i< puntos.size()-1; i++) { 

      Point origen = new Point(); 
      Point destino = new Point(); 

      projection.toPixels(puntos.get(i).getPoint(), origen); 
      projection.toPixels(puntos.get(i+1).getPoint(), destino); 

      Paint paint = new Paint(); 

      paint.setStyle(Style.STROKE); 
      paint.setStrokeWidth(5); 
      paint.setColor(Color.GREEN); 
      paint.setAntiAlias(true); 

      canvas.drawLine(origen.x, origen.y, destino.x, destino.y, paint); 
      mapview.invalidate(); 
     } 
    } 
super.draw(canvas, mapview, shadow); 
} 

但之間畫線的代碼,當我把對地圖上點太近或指向該行跨其他行的MapView平多行到近點。所以如果我有點1,2,3,4。點1有一個劃線點2,3,4

任何想法如何解決這個??

回答

0

請嘗試下面的代碼。它也會填充一些顏色的形狀。您可以省略該功能。

 Paint mPaint = new Paint(); 
     mPaint.setDither(true); 
     mPaint.setStyle(Style.FILL_AND_STROKE); 
     mPaint.setColor(Color.RED); 
     mPaint.setAlpha(9); 
     mPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
     mPaint.setStrokeJoin(Paint.Join.ROUND); 
     mPaint.setStrokeCap(Paint.Cap.ROUND); 
     mPaint.setStrokeWidth(2); 

     Path path = new Path(); 

     Projection projection = mapView.getProjection(); 

     for(int j = 0; j < geoArrayist.size(); j++) 
     { 
      Iterator<GeoPoint> it = geoArrayist.iterator(); 
      while(it.hasNext()) 
      { 
       GeoPoint arrayListGeoPoint = it.next(); 

       Point currentScreenPoint = new Point(); 
       projection.toPixels(arrayListGeoPoint, currentScreenPoint); 

       if(j == 0) 
        path.moveTo(currentScreenPoint.x, currentScreenPoint.y); 
       else 
        path.lineTo(currentScreenPoint.x, currentScreenPoint.y); 
      }     
     } 
     canvas.drawPath(path, mPaint); 
+0

st發生,當我添加點3或4時,mapview從點3到點4從第3點到第1點繪製一條線... – Nonyck

0

更改代碼:

@Override 
public void draw(Canvas canvas, MapView mapview, boolean shadow) { 

if (!shadow) { 
    if(puntos.size() == 0) return; 

    //initialization 
    Point origen = new Point(); 
    Point destino = new Point(); 


    Paint paint = new Paint(); 

    paint.setStyle(Style.STROKE); 
    paint.setStrokeWidth(5); 
    paint.setColor(Color.GREEN); 
    paint.setAntiAlias(true); 

    //end of initialization 

    Projection projection = mapview.getProjection(); 
    projection.toPixels(puntos.get(0).getPoint(), origen); 

    for(int i=1; i< puntos.size(); i++) { 
     projection.toPixels(puntos.get(i).getPoint(), destino); 
     canvas.drawLine(origen.x, origen.y, destino.x, destino.y, paint); 
    } 
} 
super.draw(canvas, mapview, shadow); 
} 

理想情況下,你應該初始化塊內移動上面的代碼覆蓋構造器,使物體origendestinopaint全局。這樣做可以提高內存使用率,僅在覆蓋層的整個生命週期內創建每個對象的1個。

祝你好運。