How to save a coordinate list after app is tombstoned?

120 Views Asked by At

I have a list of coordinates saved in a map class that are manipulated to draw a route between the points, but I'm not sure how to save these points when the application is closed and then reopened.

This is how I currently save the points within the app in the OnNavigatedTo(), the points and markers aren't lost as long as I don't close it. But how do I save these coordinates after I close the app?

I'm guessing I should save them in the OnNavigatedFrom but I tried saving the coordinate list here and the markers don't show when I repopen the app.

//save the coordinates to a list
mycoord.Add(new GeoCoordinate(MyGeoPosition.Latitude, MyGeoPosition.Longitude));

if (mycoord.Count == 2)
{
  //call route method when coord list equal to two.
  GetRoute();
}
3

There are 3 best solutions below

2
DotNetRussell On BEST ANSWER

The easiest way to do this that is customizable would be to create a custom object and serialize it to the phone's storage using EZ_Iso.dll

Here's an example

//Your custom GeoCoord container
[DataContractAttribute]
public class GeoCoordContainer{
  [DataMember]
  public double lat = 0;

  [DataMember]
  public double lon = 0;

  public GeoCoordContainer(Double lat, Double lon){
    this.lat = lat;
    this.lon = lon;
  }
}


//Then in your Navigated from method
  GeoCoordContainer cont = new GeoCoordContainer(MyGeoPosition.Lattitude,MyGeoPosition.Longitued);

  //Now save it to the storage using EZ_Iso
  EZ_Iso.IsolatedStorageAccess.SaveFile("MyLocation",cont);


 //To Retrieve it from storage 
  GeoCoordContainer cont = (GeoCoordContainer)EZ_Iso.IsolatedStorageAccess.GetFile("MyLocation",typeof(GeoCoordContainer));

You can find EZ_Iso.dll free here http://anthonyrussell.info/postpage.php?name=2

2
fuzzybear On

Use Tombstone Helper http://tombstonehelper.codeplex.com/

protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
 {
this.SaveState(e);  // <- first line
 }

protected override void OnNavigatedTo(NavigationEventArgs e)
 {
this.RestoreState();  // <- second line
 }
2
Abhishek Dey On

You can create something simple like:

PhoneApplicationService.Current.State["latitude"] = MyGeoPosition.Latitude;
PhoneApplicationService.Current.State["longitude"] = MyGeoPosition.Longitude;

Later access them as:

var lat=PhoneApplicationService.Current.State["latitude"];
var lng=PhoneApplicationService.Current.State["longitude"];

Also you can try:

private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
appSettings.Add("latitude", MyGeoPosition.Latitude.ToString());