Drawing a line/path on Google Maps

后端 未结 13 837
执笔经年
执笔经年 2020-11-22 05:23

I\'ve been busy for a long time finding out how to draw a line between two (GPS) points on the map in HelloMapView but with no luck.

Could anyone please tell me how

13条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 05:37

    Thank you for your help. At last I could draw a line on the map. This is how I done it:

    /** Called when the activity is first created. */
    private List 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 projection=mapv.getProjection();
            projection.toPixels(gP1, p1);
            projection.toPixels(gP2, p2);
    
            path.moveTo(p2.x, p2.y);
            path.lineTo(p1.x,p1.y);
    
            canvas.drawPath(path, mPaint);
        }
    

提交回复
热议问题