/
Location.cs
124 lines (102 loc) · 4.37 KB
/
Location.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Net;
using System.Windows;
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;
using System.Threading;
namespace ApexLumia
{
public class Location
{
#region "Location Properties (Private & Public)"
private bool _status;
public bool status { get { return _status; } }
private string _latitude;
public string latitude { get { return _latitude; } }
private string _longitude;
public string longitude { get { return _longitude; } }
private string _speed;
public string speed { get { return _speed; } }
private string _heading;
public string heading { get { return _heading; } }
private string _altitude;
public string altitude { get { return _altitude; } }
private string _horizontalaccuracy;
public string horizontalaccuracy { get { return _horizontalaccuracy; } }
private string _verticalaccuracy;
public string verticalaccuracy { get { return _verticalaccuracy; } }
private GeoCoordinate _currentlocation;
public GeoCoordinate currentlocation { get { return _currentlocation; } }
#endregion
GeoCoordinateWatcher watcher;
/// <summary>
/// Constructor: Creates the GeoCoordinateWatcher object, sets the Positioning accuracy, movement threshold and Event Handlers.
/// </summary>
public Location()
{
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); // High accuracy - use GPS.
watcher.MovementThreshold = 1; // Will trigger whenever the phone has moved at least 1 metre.
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcherStatusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcherPositionChanged);
}
/// <summary>
/// Starts the location service in a new thread.
/// </summary>
public void start(){
new Thread(startLocationService).Start();
}
/// <summary>
/// Stops the location service.
/// </summary>
public void stop(){
watcher.Stop();
_status = false;
}
/// <summary>
/// Tries to start the location service.
/// </summary>
void startLocationService(){
watcher.TryStart(true, TimeSpan.FromMilliseconds(60000));
}
/// <summary>
/// Event Handler: Handles status change of location service.
/// </summary>
void watcherStatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
_status = false;
MessageBox.Show("Location Service is disabled or not functioning - fix it!");
break;
case GeoPositionStatus.Initializing:
_status = false;
break;
case GeoPositionStatus.NoData:
_status = false;
break;
case GeoPositionStatus.Ready:
_status = true;
break;
}
}
/// <summary>
/// Event Handler: Called whenever the position changes and updates the class properties to the latest data.
/// </summary>
void watcherPositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e){
if (e.Position.Location.IsUnknown) { _status = false; return; }
_latitude = e.Position.Location.Latitude.ToString("0.0000");
_longitude = e.Position.Location.Longitude.ToString("0.0000");
_speed = e.Position.Location.Speed.ToString("0.0");
if (_speed == "NaN") { _speed = "0.0"; }
_heading = e.Position.Location.Course.ToString("0.0");
if (_heading == "NaN") { _heading = "0.0"; }
_altitude = e.Position.Location.Altitude.ToString("0.0");
if (_altitude == "NaN") { _altitude = "0.0"; }
_horizontalaccuracy = e.Position.Location.HorizontalAccuracy.ToString();
_verticalaccuracy = e.Position.Location.VerticalAccuracy.ToString();
_currentlocation = e.Position.Location;
_status = true;
}
}
}