2012-07-16 100 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

相关问题