Issue
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:
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
}
}
Solution
Like you said, you need to have these coordinates stored somewhere. These can be stored in a database or a more temporary local list. The draw method must also iterate between all of the coordinates you store.
Answered By - Jack Satriano
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.