在两点之间画一条线android&谷歌地图

时间:2012-07-16 06:34:26

标签: android google-maps android-layout layout maps

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

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

我使用的代码如下:

    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

2 个答案:

答案 0 :(得分: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列表。

答案 1 :(得分:0)

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

Draw Route Path on Google Map