我如何在谷歌地图Android上画一条线覆盖

我如何在谷歌地图Android上画一条线覆盖,android,overlay,line,draw,android-maps,Android,Overlay,Line,Draw,Android Maps,我正试图在我的地图项目上画一条线,但无法画出这条线。在哪里以及如何声明覆盖?我试过各种方法,但都没用。也就是说,代码只是在Eclipse中显示错误。我不是想画一条从a到B的路线,而是在我移动的时候画一条路线 // Creating a MapView public class Gpstrack extends MapActivity { private MapView map; private MapController controller; private Proje

我正试图在我的地图项目上画一条线,但无法画出这条线。在哪里以及如何声明覆盖?我试过各种方法,但都没用。也就是说,代码只是在Eclipse中显示错误。我不是想画一条从a到B的路线,而是在我移动的时候画一条路线

// Creating a MapView
public class Gpstrack extends MapActivity {
    private MapView map;
    private MapController controller;
    private Projection projection;
    ArrayList<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        initMapView();
        initMyLocation();


        TabHost.TabSpec spec;

        TabHost th = (TabHost)findViewById(R.id.tabhost);
        th.setup();

        spec = th.newTabSpec("tag1");
        spec.setContent(R.id.map_Tab);
        spec.setIndicator("Map");
        th.addTab(spec);

        spec = th.newTabSpec("tag2");
        spec.setContent(R.id.log_Tab);
        spec.setIndicator("Log");
        th.addTab(spec);

        spec = th.newTabSpec("tag3");
        spec.setContent(R.id.details_Tab);
        spec.setIndicator("Details");
        th.addTab(spec);

        spec = th.newTabSpec("tag4");
        spec.setContent(R.id.student_Tab);
        spec.setIndicator("Student Info");
        th.addTab(spec);

    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    //Map and Controls
    private void initMapView() {
        map = (MapView) findViewById(R.id.mvMain);
        controller = map.getController();
        map.setSatellite(true);
        //map.setStreetView(true);
        map.setBuiltInZoomControls(true);
    }


    //Creates an Overlay that marks current position
    private void initMyLocation() {
        final MyLocationOverlay overlay = new MyLocationOverlay(this, map);

        overlay.enableMyLocation();
        overlay.enableCompass();
        overlay.runOnFirstFix(new Runnable() {
            public void run() {
                controller.setZoom(17);
                controller.animateTo(overlay.getMyLocation());
                map.getOverlays().add(overlay);



            }

        });

    }


    //Experiment
    class MyOverlay extends Overlay {
        public void draw(Canvas canvas, MapView mapv, boolean shadow) {
            super.draw(canvas, mapv, shadow);
            Projection projection = mapv.getProjection();
            Path p = new Path();



            for (int i = 0; i < geoPointsArray.size(); i++) {
                if (i == geoPointsArray.size() -1) {
                    break;
                }
                Point from = new Point();
                Point to = new Point();
                projection.toPixels(geoPointsArray.get(i), from);
                projection.toPixels(geoPointsArray.get(i + 1), to);
                p.moveTo(from.x, from.y);
                p.lineTo(to.x, to.y);
                }

            Paint mPaint = new Paint();
            mPaint.setStyle(Style.STROKE);
            mPaint.setColor(Color.GREEN);
            mPaint.setAntiAlias(true);
            mPaint.setStrokeWidth(5);
            canvas.drawPath(p, mPaint);
            mapv.invalidate();
            super.draw(canvas, mapv, shadow);


        }
    }       
}
//创建地图视图
公共类Gpstrack扩展了MapActivity{
私人地图视图;
专用映射控制器;
私人投影;
ArrayList地理点阵列=新的ArrayList();
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initMapView();
initMyLocation();
TabHost.TabSpec;
TabHost th=(TabHost)findviewbyd(R.id.TabHost);
th.setup();
规格=新标签(“tag1”);
规格设置内容(R.id.map_选项卡);
规格设置指示器(“地图”);
添加标签(规范);
规范=新规范(“tag2”);
规格设置内容(R.id.log_选项卡);
规格设置指示器(“日志”);
添加标签(规范);
规格=新标签(“tag3”);
规格设置内容(R.id.details_选项卡);
规格设置指示器(“细节”);
添加标签(规范);
规格=第四新标签(“tag4”);
规格设置内容(R.id.student_选项卡);
规格设置指示器(“学生信息”);
添加标签(规范);
}
@凌驾
受保护的布尔值isRouteDisplayed(){
//TODO自动生成的方法存根
返回false;
}
//地图和控件
私有void initMapView(){
map=(MapView)findViewById(R.id.mvMain);
controller=map.getController();
地图.固定卫星(真);
//map.setStreetView(真);
map.setBuiltInZoomControls(真);
}
//创建标记当前位置的覆盖
私有void initMyLocation(){
最终MyLocationOverlay覆盖=新MyLocationOverlay(此,地图);
overlay.enableMyLocation();
overlay.enableCompus();
overlay.runOnFirstFix(新的Runnable(){
公开募捐{
控制器。设置缩放(17);
controller.animateTo(overlay.getMyLocation());
map.getOverlays().add(覆盖);
}
});
}
//实验
类MyOverlay扩展覆盖{
公共空白绘制(画布、MapView mapv、布尔阴影){
super.draw(画布、贴图、阴影);
Projection=mapv.getProjection();
路径p=新路径();
对于(int i=0;i
您是否尝试使用此演示来实现

希望你能从这个演示中了解到这个想法


享受。:)

在覆盖类中,可以这样画线

public class MapOverlay extends com.google.android.maps.Overlay

    {
        Canvas canvas;

        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
                long when)
        {
            // TODO Auto-generated method stub
             super.draw(canvas, mapView, shadow);
            this.canvas=canvas;
            Point screenpoint = new Point();

             mapView.getProjection().toPixels(p, screenpoint);
             Bitmap bmp = BitmapFactory.decodeResource(
                        getResources(), R.drawable.pushpin);            
                    canvas.drawBitmap(bmp, canvas.getWidth()/4, 
                            canvas.getHeight()/4, null);  

                    Paint paint = new Paint();
                    paint.setColor(Color.BLACK);
                    canvas.drawLine(canvas.getWidth()/4, canvas.getHeight()/4,
                            canvas.getWidth()/2, canvas.getHeight()/2, paint);
                    return true;

        }  


        return true;
    }    



    }
绘制线函数类似于

 public void drawLine (float startX, float startY,
 float stopX, float stopY, Paint paint) 
    Since: API Level 1 Draw a line segment with the specified start and stop x,y coordinates, using the specified paint. 

    Parameters
    startX  The x-coordinate of the start point of the line 
    startY  The y-coordinate of the start point of the line 
    paint  The paint used to draw the line  
如果你想画一条路径,你可以参照这个任务


我想你应该试试这个链接
还有这一个。我认为这应该对您有所帮助。

什么是错误?请向我们显示您的日志类别。这不是输出错误,而是说“类型列表中的方法添加(覆盖)不适用于参数(boolen)我认为应该实现位置更改侦听器。我不想做的是绘制从a到B的路线,而是在移动时绘制路线。