在Google地图中的坐标之间绘制路线

时间:2011-08-08 08:04:15

标签: android routes drawing coordinates

有人能告诉我在Google地图中坐标之间画一条线的简单方法吗?

我在谷歌找到了一些结果,但我正在寻找一些简单的东西。

1 个答案:

答案 0 :(得分:4)

使用这个简单的代码......可能适用于您的应用程序

public class GPSLine extends MapActivity {

    private List<Overlay> mapOverlays;
    private Projection projection;
    MapView mapView;

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

        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setClickable(true);

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

    }

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


    class MyOverlay extends ItemizedOverlay {

        private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
        public MyOverlay(Drawable defaultMarker) {
            super(defaultMarker);
            // TODO Auto-generated constructor stub
        }
        @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);
        }
        @Override
        protected OverlayItem createItem(int i) {
            // TODO Auto-generated method stub
            return mOverlays.get(i);
        }
        @Override
        public int size() {
            // TODO Auto-generated method stub
            return 0;
        }
    }
}