2012-07-16 27 views
0

我在谷歌地圖上工作,我想在兩點之間畫一條線我在這個網站的用戶問題中使用了下面的代碼,但它並沒有與我合作我有一個強制關閉的,當我在內部類的應用程序的工作在兩點之間劃一條線android&google地圖

刪除此功能,但我需要它,因爲我有畫線,

我使用的代碼如下:

class MyOverlay extends com.google.android.maps.Overlay { 
    GeoPoint [] geoPointsArray ; 
// constructor 
public MyOverlay(){ 

    } 

@Override 
public void draw(Canvas canvas, MapView mapv, boolean shadow){ 
    super.draw(canvas, mapv, shadow); 

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

    GeoPoint gP1 = new GeoPoint(19240000,-99120000); 
    GeoPoint gP2 = new GeoPoint(37423157, -122085008); 

    Point p1 = new Point(); 
    Point p2 = new Point(); 
    Path path = new Path(); 

    projection.toPixels(gP1, p1); 
    projection.toPixels(gP2, p2); 

    path.moveTo(p2.x, p2.y); 
    path.lineTo(p1.x,p1.y); 

    canvas.drawPath(path, mPaint); 
} 

} //結束內部類

我真的需要幫助,當我剛加入這個事情我得到了一個強制關閉:S

+0

請看我的回答,如果您有任何疑問,請告訴我。 – 2012-07-16 07:49:20

回答

1

嘗試使用此方法的一個或多個點添加到您的覆蓋,並用紅色填充。

public void draw(Canvas canvas, MapView mapView, boolean shadow) 
    { 
     super.draw(canvas, mapView, shadow); 

     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); 
      }     
     } 
     // old_geopoint = new_geopoint; 
     canvas.drawPath(path, mPaint); 
    } 

geoArrayList是一個Geopoints列表。

+0

我喜歡你的解決方案,但仍然有一個問題,當我添加此功能的應用程序停止工作,我得到一個黑屏在我的手機...它令人討厭,因爲我90%確定代碼是正確的 – user1413188 2012-07-16 06:58:38

+0

請務必在設備上進行測試..並確保您的應用程序中包含正確的api密鑰。最好的辦法是爲你的mapView重新生成API密鑰並使用新密鑰。有時候會發生這種情況,它也發生在我身上,只是輸入一個新的API密鑰就可以使用。我不知道你到底在哪裏錯了..但這可能是其中一個原因 – pixelscreen 2012-07-16 08:33:58

0

在Google地圖上兩點之間繪製路線時使用下面的堆棧溢出答案,它可以幫助你。

Draw Route Path on Google Map

+0

我使用了你的解決方案,並且在下面這行代碼中toPixel不工作(int i = 0; i < mPoints.size(); i ++){Point_Point = new Point(); mv.getProjection()。toPixels(mPoints.get(i),point); x2 = point.x; y2 = point.y; – user1413188 2012-07-16 07:57:50

+0

@ user1413188發佈您的錯誤logcat – 2012-07-16 08:35:39

相關問題