2012-04-17 126 views
4

現在下面是我的代碼,在地圖geopoints之間繪製路徑。這工作非常好。我試圖實現的不是畫一條線,而是用iPhone中的點(。)來顯示這條路徑。我希望它像這個gp1 ......... gp2,而不是畫在像gp1_的單個直線上。繪製點劃線(....)線索路徑,而不是一個線路(________)

我已經嘗試了幾乎所有這樣做的選擇,但仍然沒有這方面的成功,任何一個能幫助我解決這個?

private void drawPath(List geoPoints, int color) { 
    List overlays = objMapView.getOverlays(); 
    int loopcount = geoPoints.size() - 1; 
    for (int i = 0; i < loopcount; i++) { 
     GeoPoint p1 = (GeoPoint) geoPoints.get(i); 
     GeoPoint p2 = (GeoPoint) geoPoints.get(i + 1); 
     MyPathOverLay p = null; 
     /**** Marking the start and end of the trailpath ****/ 
     if (i == 0) { 
      Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),R.drawable.greenflag); 
      p = new MyPathOverLay(p1, p2, 0xFFFF0000, true, false, bmp); 
     } else if (i == loopcount - 1) { 
      Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),R.drawable.redflag); 
      p = new MyPathOverLay(p1, p2, 0xFFFF0000, false, true, bmp); 
     } else { 
      p = new MyPathOverLay(p1, p2, 0xFFFF0000, false, false, null); 
     } 
     overlays.add(p); 
    } 
} 



public class MyPathOverLay extends Overlay { 

private GeoPoint gp1; 
private GeoPoint gp2; 
private int color; 
Boolean isFirstPOI; 
Boolean isLastPOI; 
AudioMap audioMap; 
Bitmap bmp; 

public MyPathOverLay(GeoPoint gp1, GeoPoint gp2, int color,Boolean first, Boolean last,Bitmap bitMap) { 
    this.gp1 = gp1; 
    this.gp2 = gp2; 
    this.color = color; 
    this.isFirstPOI= first; 
    this.isLastPOI = last; 
    this.bmp = bitMap; 
} 

@Override 
public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
    Projection projection = mapView.getProjection(); 
    Paint paint = new Paint(); 
    Point point = new Point(); 
    projection.toPixels(gp1, point); 
    paint.setColor(color); 
    Point point2 = new Point(); 
    projection.toPixels(gp2, point2); 
    paint.setStrokeWidth(5); 
    paint.setAlpha(120); 
    canvas.drawLine(point.x, point.y, point2.x, point2.y, paint); 
    //---translate the GeoPoint to screen pixels--- 
    Point screenPts = new Point(); 
    mapView.getProjection().toPixels(gp1, screenPts); 
    //---translate the GeoPoint to screen pixels--- 
    Point screenPts1 = new Point(); 
    mapView.getProjection().toPixels(gp2, screenPts1); 
    if(isFirstPOI == true){ 
      canvas.drawBitmap(bmp,screenPts.x-20,screenPts.y-40, null); 
    } 
    else if(isLastPOI == true) { 
      canvas.drawBitmap(bmp,screenPts1.x-20,screenPts1.y-35, null); 
    } 
    super.draw(canvas, mapView, shadow); 
} 

}

+1

http://stackoverflow.com/questions/6167236/canvas-drawlines-displays-disjointed-segments – MKJParekh 2012-04-17 05:28:37

+0

這個任務是離子使用舊的谷歌地圖api(在寫這篇評論的時刻,目前的是v2)? – DNax 2015-06-23 20:57:00

回答

10

多虧了這傢伙,我提到這個例子中,它是爲我工作。

How do I make a dotted/dashed line in Android?

只需要添加這兩條線,

paint.setPathEffect(new DashPathEffect(new float[] {10,10}, 5)); 
    canvas.drawLine(point.x, point.y, point2.x, point2.y, paint); 

後我設置,

paint.setAlpha(120); 

@Frankenstein,謝謝

+0

您WEL-加油:) – MKJParekh 2012-04-17 05:53:04

+1

您還可以接受你自己的答案:),還在乎把你的代碼,如果你覺得有必要...乾杯 – MKJParekh 2012-04-17 05:54:37

+1

@Frankenstein,我能接受我自己的答案後第2天只:(不太大的變化我做的代碼,我剛添加那些2線fgPaintSel.setStyle(Style.STROKE); fgPaintSel.setPathEffect(新DashPathEffect(新浮[] {10,20},0));之後我設定爲阿爾法油漆 – tejas 2012-04-17 06:18:45