在几个GPS坐标之间画线

时间:2013-01-24 20:13:36

标签: android google-maps

如何在不同的GPS坐标之间绘制线条以将它们连接在一起?

我应该提到这不是一条路线。我正在使用卫星视图,例如,该线路可以通过房屋。

1 个答案:

答案 0 :(得分:0)

如果只有两个坐标,而不是在地图上绘制一个简单的画布并在这些坐标之间绘制一条直线,那么这将是一个简单的线条,它可以在任何地方绘制,甚至在房屋上。

以下示例代码 Drawing a line/path on Google Maps

/** Called when the activity is first created. */
 private List<Overlay> mapOverlays;

 private Projection projection;  

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

linearLayout = (LinearLayout) findViewById(R.id.zoomview);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);

mapOverlays = mapView.getOverlays();        
projection = mapView.getProjection();
mapOverlays.add(new MyOverlay());        

 }

@Override
 protected boolean isRouteDisplayed() {
return false;
 }

class MyOverlay extends Overlay{

public MyOverlay(){

}   

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);
}