Search code examples
c#windows-phone-8isolatedstorage

How to save a list of coordinates to isolated storage?


I have a GeoCoordinate list that I want to save to storage when the application is closed, but I'm not sure how to save it to storage.

I tried saving the list using a helper class found here problem Storing a list of Objects in Isolated Storage but I think my syntax may be wrong in saving it as I'm new to using lists.This is how I tried to save the list. Can anyone point me in the right direction with saving the lit?

mycoord = Isolated_Storage_Helper.IsoStoreHelper
                                 .SaveList<mycoord>("Storage_Folder/", "Storage");

It gives me an error stating that mycoord is a field but is used as a type

mycoord is a list of coordinates created at a global level:

List<GeoCoordinate> mycoord = new List<GeoCoordinate>();

And populated in the OnNavigatedTo method:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (NavigationContext.QueryString.ContainsKey("GeoLat") &&  
        NavigationContext.QueryString.ContainsKey("GeoLong") &&  
        NavigationContext.QueryString.ContainsKey("pName"))
    {
        if (mycoord.Count >= 2)
        {
            //do something,draw route between points
            return;
        }
        else
        {
            var latitude = Convert.ToDouble(NavigationContext.QueryString["GeoLat"]);
            var longtitude = Convert.ToDouble(NavigationContext.QueryString["GeoLong"]);
            var MyGeoPosition = new GeoCoordinate(latitude, longtitude);
            var pushPinName = NavigationContext.QueryString["pName"];
            DrawPushPin(MyGeoPosition, pushPinName);
            mycoord.Add(MyGeoPosition);
        }
    }
    base.OnNavigatedTo(e);
}

Solution

  • First create the coordinate class:

    [DataContract]
    class coord{
        [DataMember]
        public double lat{get;set;}
        [DataMember]
        public double lon{get;set;}
    }
    

    Now if you want to save a List<coord> You may do:

    DataContractSerializer ser = new DataContractSerializer(typeof(List<coord>));
    ser.WriteObject(any_file_stream, instance_of_List<coord>);
    

    But if you want to save just a couple of coordinates, not a list IsolatedStorageSetting would be a better option.