3

我正在尝试在我的地图项目上画一条线,但无法画线。我在哪里以及如何声明覆盖?我尝试了各种方法,但无法使其正常工作。即,代码只是在 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);


        }
    }       
}
4

3 回答 3

1

您是否尝试使用此演示来实现Google Map Overlay

希望您对这个演示有所了解。

享受。:)

于 2012-04-26T04:34:52.343 回答
0

我认为你应该试试这个链接 在谷歌地图上画一条线/路径 和这个J2ME/Android/BlackBerry - 行车路线,两个位置之间的路线。我认为这应该对你有所帮助。

于 2012-04-26T06:50:54.930 回答
0

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

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  

如果你想画一条路径,你可以参考这个问题

J2ME/Android/BlackBerry - 行车路线,两个地点之间的路线

于 2012-04-26T04:45:24.147 回答