Saturday 27 June 2015

Drawing route between coordinates in Google map at Android os

use this simple code...may be work in your application

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

No comments:

Post a Comment