使用Google地图在2个点之间绘制一条线

时间:2012-10-21 13:39:03

标签: android google-maps overlay line geopoints

我的问题是我想要创建一个方法,当我调用它时给它两个GeoPoint我希望它在两点之间画一条线而我无法做到这一点。

提前致谢。

public class MyOverlays extends ItemizedOverlay<OverlayItem> {

private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
private int lastestIndex;
private Projection projection;
private Canvas canvas;
private MapView mapv;
private boolean shadow;

public MyOverlays(Context context, Drawable defaultMarker, String player) {
    super(boundCenterBottom(defaultMarker));
    this.context = context;
}

@Override
protected OverlayItem createItem(int i) {
    return mapOverlays.get(i);
}

@Override
public int size() {
    return mapOverlays.size();

}

public void setPj(Projection projection) {
    this.projection = projection;

}

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

    // Configuring the paint brush

}

测试方法理想情况下我想传递2个地理位置来在它们之间划一条线

  public void test() {
    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(4);

    GeoPoint gP1 = new GeoPoint(34159000, 73220000);// starting point
    GeoPoint gP2 = new GeoPoint(33695043, 73050000);// End point Islamabad

    GeoPoint gP4 = new GeoPoint(33695043, 73050000);// Start point Islamabad
    GeoPoint gP3 = new GeoPoint(33615043, 73050000);// End Point Rawalpindi

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

    Point p3 = new Point();
    Point p4 = new Point();
    Path path2 = new Path();
    projection.toPixels(gP2, p3);
    projection.toPixels(gP1, p4);

    path1.moveTo(p4.x, p4.y);// Moving to Abbottabad location
    path1.lineTo(p3.x, p3.y);// Path till Islamabad

    projection.toPixels(gP3, p1);
    projection.toPixels(gP4, p2);

    path2.moveTo(p2.x, p2.y);// Moving to Islamabad location
    path2.lineTo(p1.x, p1.y);// Path to Rawalpindi

    canvas.drawPath(path1, mPaint);// Actually drawing the path from
                                    // Abbottabad to Islamabad
    canvas.drawPath(path2, mPaint);// Actually drawing the path from
                                    // Islamabad to Rawalpindi
    this.populate();
}

1 个答案:

答案 0 :(得分:0)

解决了我的问题,

public void draw(Canvas canvas, MapView mapv, boolean shadow){
     super.draw(canvas, mapv, shadow);
     this.canvas=canvas;
     this.mapv=mapv;
     this.shadow=shadow;
     if(line==1){
         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(4);

         GeoPoint gP1 = new GeoPoint(34159000,73220000);//starting point Abbottabad
         GeoPoint gP2 = new GeoPoint(33695043,73050000);//End point Islamabad

         GeoPoint gP4 = new GeoPoint(33695043, 73050000);//Start point Islamabad
         GeoPoint gP3 = new GeoPoint(33615043, 73050000);//End Point Rawalpindi

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

         Point p3 = new Point();
         Point p4 = new Point();
         Path path2 = new Path();
         projection.toPixels(gP2, p3);
         projection.toPixels(gP1, p4);

        path1.moveTo(p4.x, p4.y);//Moving to Abbottabad location
         path1.lineTo(p3.x,p3.y);//Path till Islamabad

         projection.toPixels(gP3, p1);
         projection.toPixels(gP4, p2);

        path2.moveTo(p2.x, p2.y);//Moving to Islamabad location
         path2.lineTo(p1.x,p1.y);//Path to Rawalpindi

         canvas.drawPath(path1, mPaint);//Actually drawing the path from Abbottabad to Islamabad
         canvas.drawPath(path2, mPaint);//Actually drawing the path from Islamabad to Rawalpindi
     }


    //Configuring the paint brush



     }
public void test(){
    System.out.println("vim ao test");
    line=1;
    draw(canvas,mapv,shadow);
}