• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Draw a route using current location from device on map using GPS Provider

ansol90

Lurker
I'm working on a project to display a route in a MapView based on current location from GPS Provider. I'm able to draw the path between two points but the problem starts when the location changes to a new point causing the draw path to erase. Basically my Location Listener saves the current Geopoint and sets the new Geopoint location and after that it starts the overlay to draw a path based on those two points. I think an option could be saving the coordinates to a database and then pulling the information from there to draw the path.

Here is my code so far:

Code:
private class MyLocationListener implements LocationListener {

   public void onLocationChanged(Location location) {   

     Toast.makeText(getBaseContext(), "Tracking device..",
                            Toast.LENGTH_SHORT).show();

            gp2 = new GeoPoint(
                    gp1.getLatitudeE6(),
                    gp1.getLongitudeE6());

            gp1 = new GeoPoint(
                    (int) (location.getLatitude() * 1000000),
                    (int) (location.getLongitude() * 1000000));


    myOverlay = new MapOverlay();

    mapOverlays_route.add(myOverlay);
    myMapView.invalidate();
}
}


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

        public MapOverlay(){


        }


        public void draw(Canvas canvas, MapView mapv, boolean shadow){
         super.draw(canvas, mapv, shadow);

        //Configuring the paint brush

        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(4);




         Point p1 = new Point();
         Point p2 = new Point();
         Path path1 = new Path();


         Path path2 = new Path();
         projection.toPixels(gp1, p1);
         projection.toPixels(gp2, p2);

         path1.moveTo(p2.x, p2.y);//current location
         path1.lineTo(p1.x,p1.y);//new location


         canvas.drawPath(path1, mPaint);//drawing the path

         }

        }
 
Hello, please place your code inside of [code] and [/code] tags. This will retain the proper format, making it easier to read.

Thanks. =)
 
Back
Top Bottom