예제 #1
0
파일: GpxIOFactory.cs 프로젝트: macias/Gpx
        public static IDisposable CreateWriter(Stream stream, out IGpxWriter writer)
        {
            var result = new GpxWriter(stream);

            writer = result;
            return(result);
        }
예제 #2
0
        public int SaveFile(GeoPosition<GeoCoordinate> Position = null)
        {
            if (Position == null)
            {
                GeoCoordinateWatcher watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
                watcher.Start();
                Position = watcher.Position;
            }

            var fileName = CurrentFileName();

            var tempFileName = "record.gpx.temp";
            var count = 0;
            bool firstRun = true;
            IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication();
            if (isStore.FileExists(tempFileName))
            {
                isStore.DeleteFile(tempFileName);
            }
            if (isStore.FileExists(fileName))
            {
                firstRun = false;
                isStore.MoveFile(fileName, tempFileName);
            }

            using (IsolatedStorageFileStream input = new IsolatedStorageFileStream(tempFileName, System.IO.FileMode.OpenOrCreate, FileAccess.Read, isStore))
            using (IsolatedStorageFileStream output = new IsolatedStorageFileStream(fileName, System.IO.FileMode.OpenOrCreate, FileAccess.Write, isStore))
            using (GpxWriter writer = new GpxWriter(output))
            {
                GpxWayPoint last = null;
                if (!firstRun)
                {
                    using (GpxReader reader = new GpxReader(input))
                    {
                        while (reader.Read())
                        {
                            switch (reader.ObjectType)
                            {
                                case GpxObjectType.WayPoint:
                                    count++;
                                    writer.WriteWayPoint(reader.WayPoint);
                                    last = reader.WayPoint;
                                    break;
                            }
                        }
                    }
                }

                IsolatedStorageSettings.ApplicationSettings["LastLocation"] = last;
                IsolatedStorageSettings.ApplicationSettings.Save();

                if (double.IsNaN(Position.Location.Latitude) || double.IsNaN(Position.Location.Longitude))
                {
                    return count;
                }
                if (last == null || last.Time.ToString() != Position.Timestamp.UtcDateTime.ToString())
                {
                    writer.WriteWayPoint(new GpxWayPoint
                    {
                        Latitude = Position.Location.Latitude,
                        Longitude = Position.Location.Longitude,
                        Elevation = Position.Location.Altitude,
                        Time = Position.Timestamp.UtcDateTime,
                    });
                    count++;
                }
            }
            return count;
        }