0

我在谷歌地图上工作,我想在两点之间画一条线我使用了本网站用户问题中的以下代码,但它对我不起作用当我在内部删除此功能时,我有一个强制关闭类应用程序工作

但我需要它,因为我必须画线

我使用的代码如下:

    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

4

2 回答 2

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 是一个地理点列表。

于 2012-07-16T06:42:22.647 回答
0

使用下面的堆栈溢出答案在谷歌地图上的两点之间绘制路线,它可能会对您有所帮助。

在谷歌地图上绘制路线路径

于 2012-07-16T07:48:35.663 回答