OK, well your main problem is that while you implemented the Parcelable
class, you didn't implement the writeToParcel method or the constructor that
takes a Parcel. That's the key.

You need to implement the first method to save all you state information
(i.e., your member variables) to the Parcel object.
Then implement the constructor to restore your instance state from the
incoming Parcel object.

Then in your MapActivity onSavedInstanceState, iterate through all of your
Overlays and shove them into an array you can store in the Bundle.
In onRestoreInstanceState, get a list of Parcelables from the Bundle and
re-create all of your Overlays (using the constructor that takes a Parcel).

Other bit of advise: get rid of that "if (first) { /*initialze*/}" code. Set
up your MyLocationOverlay object in onCreate. If you need to have a starting
location while you wait for the first update, use getLastKnownLocation().

I hope that makes sense and helps some.

-------------------------------------------------------------------------------------------------
TreKing - Chicago transit tracking app for Android-powered devices
http://sites.google.com/site/rezmobileapps/treking


On Tue, Jan 5, 2010 at 3:32 PM, Stefan <ebay-dah...@web.de> wrote:

> Ok, i try it :)
>
> 1. I track gps points in my app and draw my route on a MapView:
>
> @Override
> public void onLocationChanged(Location loc)
> {
>        if (loc != null)
>        {
>                latitude = loc.getLatitude();
>                longitude = loc.getLongitude();
>
>                if(first)
>                {
>                        p1 = new GeoPoint((int) (loc.getLatitude()*1E6),
> (int)
> (loc.getLongitude()*1E6));
>                        mc.animateTo(p1);
>                        mc.setZoom(18);
>                        first=false;
>
>                        //act_pos = new TrackOverlay(p1, p1,1);  I NOW USE
> MYLOCATIONOVERLAY
>                        //map.getOverlays().add(act_pos);
>
>                        mlo = new MyLocationOverlay(TRACKing.this, map);
>                        map.getOverlays().add(mlo);
>                        mlo.enableMyLocation();
>                        mlo.runOnFirstFix(new Runnable() {
>                            public void run() {
>                                mc.animateTo(mlo.getMyLocation());
>                        }  });
>                }
>                else
>                {
>                        p2 = p1;
>                        p1 = new GeoPoint((int) (loc.getLatitude()*1E6),
> (int)
> (loc.getLongitude()*1E6));
>
>                        to = new TrackOverlay(p1, p2, 0);
>
>                        //act_pos.setGeoPoints(p1, p2);   I NOW USE
> MYLOCATIONOVERLAY
>                        map.getOverlays().add(to);
>                        mc.animateTo(p1);
>                        map.invalidate();
>                }
>        }
> }
>
> 2. My TrackOverlay class:
>
> public class TrackOverlay extends Overlay implements Parcelable
> {
>        private static final int TRACK = 0;
>        private GeoPoint gp1, gp2;
>        private int mode;
>
>        public TrackOverlay(GeoPoint gp1,GeoPoint gp2, int mode)
>        {
>                this.gp1 = gp1;
>                this.gp2 = gp2;
>                this.mode = mode;
>        }
>
>        public void setGeoPoints (GeoPoint gp1, GeoPoint gp2)
>        {
>                this.gp1 = gp1;
>                this.gp2 = gp2;
>        }
>
>        @Override
>        public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
> long when)
>        {
>                Projection projection = mapView.getProjection();
>                if (shadow == false)
>                {
>                        Paint paint = new Paint();
>                        paint.setAntiAlias(true);
>                        Point point = new Point();
>                        if(gp1!=null && gp2 != null)
>                        {
>                                projection.toPixels(gp1, point);
>
>                                if(mode==TRACK)
>                                {
>                                        paint.setColor(Color.BLUE);
>                                        Point point2 = new Point();
>                                        projection.toPixels(gp2, point2);
>                                        paint.setStrokeWidth(5);
>                                        paint.setAlpha(120);
>                                        canvas.drawLine(point.x, point.y,
> point2.x,point2.y, paint);
>                                }
>                                else
>                                      //atm nothing to do
>                        }
>                }
>                return super.draw(canvas, mapView, shadow, when);
>        }
>
>        @Override
>        public int describeContents() {
>                // TODO Auto-generated method stub
>                return 0;
>        }
>
>
>        @Override
>        public void writeToParcel(Parcel dest, int flags) {
>                // TODO Auto-generated method stub
>
>        }
>
>        private TrackOverlay (Parcel in)
>        {
>
>        }
>
>        public static final Parcelable.Creator<TrackOverlay> CREATOR =
>        new Parcelable.Creator<TrackOverlay>()
>        {
>                public TrackOverlay createFromParcel(Parcel in)
>                {
>                        return new TrackOverlay(in);
>                }
>                public TrackOverlay[] newArray(int size)
>                {
>                        return new TrackOverlay[size];
>                }
>    };
> }
>
> 3. If i want to describe a specific point, i start a new activity (via
> menue). and if i come back to this activity, i cant see the overlays
> any more. So how can I save my overlays??
>
> Thanks,
> Stefan
>
> PS: I hope this help you...
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com<android-developers%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to