日期:2014-05-16  浏览次数:20981 次

Windows Phone 运动轨迹记录工具

轨迹记录工具


带有 GPS 记录功能的运动手表价格不菲,于是我自己写了一个简单的运动轨迹记录工具。核心代码如下,只有不到 200 行。

工具记录的数据可以使用 Isolated Storage Explorer Tool 导出,进行进一步的分析、处理和转换。

下载链接 http://pan.baidu.com/share/link?shareid=375619&uk=2114195841

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Device.Location;
using System.IO;
using System.IO.IsolatedStorage;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace Tracker
{
    public partial class MainPage : PhoneApplicationPage
    {
        bool tracking;
        string logName;
        GeoCoordinateWatcher watcher;
        List<GeoCoordinate> locations;
        List<long> timestamps;
        Object lockObject;

        public MainPage()
        {
            tracking = false;
            lockObject = new Object();
            InitializeComponent();
            PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;
            updateLogName();
            status.Text = "GPS Tracker by coolypf";
            watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
            watcher.MovementThreshold = 0.0;
            watcher.PositionChanged += onMove;
        }

        void onMove(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            if (!tracking || watcher.Status != GeoPositionStatus.Ready)
                return;
            lock (lockObject)
            {
                locations.Add(e.Position.Location);
                timestamps.Add(e.Position.Timestamp.UtcTicks);
            }
        }

        void updateLogName()
        {
            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
            DateTime now = DateTime.Now;
            String prefix = now.Year + "-" + now.Month + "-" + now.Day + "-";
            String filename;
            int idx = 1;
            while (true)
            {
                filename = prefix + idx + ".txt";
                if (!isf.FileExists(filename))
                    break;
                idx++;
            }
            name.Text = filename;
        }

        void writeLog()
        {
            lock (lockObject)
            {
                if (locations.Count < 2)
                    return;
                IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
                StreamWriter w = new StreamWriter(isf.OpenFile(logName, FileMode.Create, FileAccess.Write));
                for (int i = 0; i < locations.Count; ++i)
                {
                    w.Write(timestamps[i]);
                    w.Write(" ");
                    GeoCoordinate g = locations[i];
                    w.Write(g.Latitude);
                    w.Write(" ");
                    w.Write(g.Longitude);
                    w.Write(" ");
                    w.Write(g.Altitude);
                    w.Write(" ");
                    w.Write(g.Speed);
                    w.WriteLine();
                }
                w.Close();
            }
        }

        void onControl(object sender, RoutedEventArgs e)
        {
            control.IsEnabled = false;
            if (tracking)
            {
                tracking = false;
                lock (lockObject)
                {
                    if (locations.Count > 0)
                    {
                        locations.Add(watcher.Position.Location);
                        timestamps.Add(watcher.Position.Timestamp.UtcTicks);
                    }
                }
                watcher.Stop();
                writeLog();
                status.Text = getStatus();
                locations = null;
                timestamps = null;
                control.Conten