I have a program where I want to draw a line between two points. I then want another line to draw between another two points. When I draw one line it removes the previous line. I have tried making both lines different Overlays but it still removes the old line and puts in the new line. Do I prevent this from happing and how do I make all the Overlays stay? Any help would be greatly appreciated.
The Overlay class in inside the Activity class.
This is in onKeyDown()
case KeyEvent.KEYCODE_8:
twoPoints.add(a);
twoPoints.add(b);
MapOverlay newOverlay = new MapOverlay();
listOfOverlays = mapView.getOverlays();
listOfOverlays.add(newOverlay);
mapView.postInvalidate();
System.out.println("Test overlays 1 Point");
break;
case KeyEvent.KEYCODE_9:
twoPoints.remove(1);
twoPoints.add(c);
MapOverlay newOverlay1 = new MapOverlay();
listOfOverlays.add(newOverlay1);
mapView.postInvalidate();
System.out.println("Test Overlays 2 Point");
break;
And this is the Overlay class:
public class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
super.draw(canvas,mapView,shadow);
//-- Create new paint object --
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(2);
//System.out.println("Point 1");
//System.out.println("Point 2");
for(int i =0; i < twoPoints.size()-1;i++)
{
Point screenPt1 = new Point();
mapView.getProjection().toPixels(twoPoints.get(i), screenPt1);
Point screenPt2 = new Point();
mapView.getProjection().toPixels(twoPoints.get(i+1), screenPt2);
canvas.drawLine(screenPt1.x, screenPt1.y, screenPt2.x, screenPt2.y, mPaint);
System.out.println("Point 3");
}
//mapView.invalidate();
}
}
It has been really confusing me for a while now.
Eyespyus was on the right track the problem was that I had the Overlay class in the activity and was changing the array that both used. Moving the class outside the activity fixed the problem.
This was a really silly mistake and I thank everyone for taking the time to look at this, I am very new to Android.