/
LocationHelper.cs
285 lines (256 loc) · 12.8 KB
/
LocationHelper.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// ---------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ---------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.Data.Xml.Dom;
using Windows.Devices.Geolocation;
using Windows.Services.Maps;
using Windows.UI.Notifications;
namespace Location
{
public static class LocationHelper
{
private static string routeFinderUnavailableMessage = "Unable to access map route finder service.";
/// <summary>
/// Gets the Geolocator singleton used by the LocationHelper.
/// </summary>
public static Geolocator Geolocator { get; } = new Geolocator();
/// <summary>
/// Gets or sets the CancellationTokenSource used to enable Geolocator.GetGeopositionAsync cancellation.
/// </summary>
private static CancellationTokenSource CancellationTokenSource { get; set; }
/// <summary>
/// Initializes the LocationHelper.
/// </summary>
static LocationHelper()
{
// TODO Replace the placeholder string below with your own Bing Maps key from https://www.bingmapsportal.com
MapService.ServiceToken = "<insert your Bing Maps key here>";
}
/// <summary>
/// Gets the current location if the geolocator is available.
/// </summary>
/// <returns>The current location.</returns>
public static async Task<LocationData> GetCurrentLocationAsync()
{
try
{
// Request permission to access the user's location.
var accessStatus = await Geolocator.RequestAccessAsync();
switch (accessStatus)
{
case GeolocationAccessStatus.Allowed:
LocationHelper.CancellationTokenSource = new CancellationTokenSource();
var token = LocationHelper.CancellationTokenSource.Token;
Geoposition position = await Geolocator.GetGeopositionAsync().AsTask(token);
return new LocationData { Position = position.Coordinate.Point.Position };
case GeolocationAccessStatus.Denied:
case GeolocationAccessStatus.Unspecified:
default:
return null;
}
}
catch (TaskCanceledException)
{
// Do nothing.
}
finally
{
LocationHelper.CancellationTokenSource = null;
}
return null;
}
/// <summary>
/// Cancels any waiting GetGeopositionAsync call.
/// </summary>
public static void CancelGetCurrentLocation()
{
if (LocationHelper.CancellationTokenSource != null)
{
LocationHelper.CancellationTokenSource.Cancel();
LocationHelper.CancellationTokenSource = null;
}
}
/// <summary>
/// Launches the Maps app and displays the route from the current location
/// to the specified location.
/// </summary>
/// <param name="location">The location to display the route to.</param>
public static async Task ShowRouteToLocationInMapsAppAsync(LocationData location, LocationData currentLocation)
{
var mapUri = new Uri("bingmaps:?trfc=1&rtp=" +
$"pos.{Math.Round(currentLocation.Position.Latitude, 6)}_{Math.Round(currentLocation.Position.Longitude, 6)}~" +
$"pos.{location.Position.Latitude}_{location.Position.Longitude}");
await Windows.System.Launcher.LaunchUriAsync(mapUri);
}
/// <summary>
/// Shows the specified text in a toast notification if notifications are enabled.
/// </summary>
/// <param name="text">The text to show.</param>
private static void ShowToast(string text)
{
var toastXml = new XmlDocument();
toastXml.LoadXml("<toast duration='short'><visual><binding template='ToastText01'>" +
$"<text id='1'>{text}</text></binding></visual></toast>");
var toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
/// <summary>
/// Registers the TrafficMonitor background task.
/// </summary>
public static void RegisterTrafficMonitor()
{
BackgroundTaskHelper.RegisterBackgroundTask(
taskEntryPoint: "TrafficMonitor.TrafficMonitor",
taskName: "TrafficMonitor",
trigger: new MaintenanceTrigger(freshnessTime: 15, oneShot: false),
condition: new SystemCondition(SystemConditionType.InternetAvailable));
}
/// <summary>
/// Unregisters the TrafficMonitor background task.
/// </summary>
public static void UnregisterTrafficMonitor()
{
BackgroundTaskHelper.UnregisterBackgroundTask("TrafficMonitor");
}
/// <summary>
/// Loads the location data from storage then raises an alert for each flagged location
/// if traffic is currently increasing the travel time by 10 minutes or more.
/// </summary>
public static async Task CheckTravelInfoForMonitoredLocationsAsync()
{
var locations = await LocationDataStore.GetLocationDataAsync();
var flaggedLocations = locations.Where(location => location.IsMonitored).ToList();
if (flaggedLocations.Count > 0)
{
var currentLocation = await LocationHelper.GetCurrentLocationAsync();
if (!await LocationHelper.TryUpdateLocationsTravelInfoAsync(flaggedLocations, currentLocation))
{
LocationHelper.ShowToast("Can't get location/traffic info.");
}
}
}
/// <summary>
/// Attempts to update the travel distance and time info for the specified locations,
/// relative to the current location, and raises an alert for each flagged location
/// if traffic is currently increasing the travel time by 10 minutes or more.
/// </summary>
/// <param name="locations">The locations to update.</param>
/// <param name="currentLocation">The current location, providing context to disambiguate locations, if needed. </param>
/// <returns>true if all the locations were successfully updated; false if a service failure occurred.</returns>
public static async Task<bool> TryUpdateLocationsTravelInfoAsync(IEnumerable<LocationData> locations, LocationData currentLocation)
{
try
{
await Task.WhenAll(locations.Select(async location =>
{
await LocationHelper.UpdateTravelInfoAsync(location, currentLocation);
int travelTimeDifference = location.CurrentTravelTime - location.CurrentTravelTimeWithoutTraffic;
if (location.IsMonitored && travelTimeDifference >= 10)
{
LocationHelper.ShowToast(
$"+{travelTimeDifference} min. to {location.Name}, total {location.CurrentTravelTime} min.");
}
}));
return true;
}
catch (Exception ex) when (ex.Message.Equals(routeFinderUnavailableMessage))
{
return false;
}
}
/// <summary>
/// Updates the travel distance and time info for the specified location, relative to the specified current location.
/// </summary>
/// <param name="location">The location to update.</param>
/// <param name="currentLocation">The current location.</param>
public static async Task UpdateTravelInfoAsync(LocationData location, LocationData currentLocation)
{
var routeResultTask = MapRouteFinder.GetDrivingRouteAsync(
currentLocation.Geopoint, location.Geopoint,
MapRouteOptimization.TimeWithTraffic, MapRouteRestrictions.None);
var routeResultWithoutTrafficTask = MapRouteFinder.GetDrivingRouteAsync(
currentLocation.Geopoint, location.Geopoint,
MapRouteOptimization.Time, MapRouteRestrictions.None);
MapRouteFinderResult routeResult = await routeResultTask;
MapRouteFinderResult routeResultWithoutTraffic = await routeResultWithoutTrafficTask;
if (routeResult.Status == MapRouteFinderStatus.Success)
{
location.FastestRoute = routeResult.Route;
location.CurrentTravelDistance = Math.Round(routeResult.Route.LengthInMeters * 0.00062137, 1); // convert to miles
location.CurrentTravelTime = (int)routeResult.Route.EstimatedDuration.TotalMinutes;
location.Timestamp = DateTimeOffset.Now;
if (routeResultWithoutTraffic.Status == MapRouteFinderStatus.Success)
{
location.CurrentTravelTimeWithoutTraffic = routeResultWithoutTraffic.Route.EstimatedDuration.Minutes;
}
else
{
// Fall back to the with-traffic value if the request fails.
location.CurrentTravelTimeWithoutTraffic = routeResult.Route.EstimatedDuration.Minutes;
}
}
else if (routeResult.Status == MapRouteFinderStatus.UnknownError ||
routeResult.Status == MapRouteFinderStatus.InvalidCredentials ||
routeResult.Status == MapRouteFinderStatus.NetworkFailure ||
routeResult.Status == MapRouteFinderStatus.NotSupported)
{
throw new Exception(routeFinderUnavailableMessage);
}
}
/// <summary>
/// Attempts to update either the address or the coordinates of the specified location
/// if the other value is missing, using the specified current location to provide
/// context for prioritizing multiple locations returned for an address.
/// </summary>
/// <param name="location">The location to update.</param>
/// <param name="currentLocation">The current location.</param>
public static async Task<bool> TryUpdateMissingLocationInfoAsync(LocationData location, LocationData currentLocation)
{
bool hasNoAddress = String.IsNullOrEmpty(location.Address);
if (hasNoAddress && location.Position.Latitude == 0 && location.Position.Longitude == 0) return true;
var results = hasNoAddress ?
await MapLocationFinder.FindLocationsAtAsync(location.Geopoint) :
await MapLocationFinder.FindLocationsAsync(location.Address, currentLocation.Geopoint);
if (results.Status == MapLocationFinderStatus.Success && results.Locations.Count > 0)
{
var result = results.Locations.First();
location.Position = result.Point.Position;
location.Address = result.Address.FormattedAddress;
if (String.IsNullOrEmpty(location.Name)) location.Name = result.Address.Town;
// Sometimes the returned address is poorly formatted. This fixes one of the issues.
if (location.Address.Trim().StartsWith(",")) location.Address = location.Address.Trim().Substring(1).Trim();
return true;
}
else
{
return false;
}
}
}
}