PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-toolkit/PhoneToolkitSample8/Samples/MapsSample.xaml.cs

https://bitbucket.org/jeremejevs/milk-manager
C# | 402 lines | 225 code | 71 blank | 106 comment | 26 complexity | 6933f39d9010e03b5e9c9b9d07d9ea7d MD5 | raw file
  1. // ---------------------------------------------------------------------------
  2. // <copyright file="MapsSample.xaml.cs" company="Microsoft">
  3. // (c) Copyright Microsoft Corporation.
  4. // This source is subject to the Microsoft Public License (Ms-PL).
  5. // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
  6. // All other rights reserved.
  7. // </copyright>
  8. // ---------------------------------------------------------------------------
  9. namespace PhoneToolkitSample.Samples
  10. {
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.Device.Location;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using Microsoft.Phone.Controls;
  20. using Microsoft.Phone.Maps.Controls;
  21. using Microsoft.Phone.Maps.Services;
  22. using Microsoft.Phone.Maps.Toolkit;
  23. using Microsoft.Phone.Shell;
  24. using Microsoft.Phone.Tasks;
  25. using PhoneToolkitSample.Data.Maps;
  26. using Windows.Devices.Geolocation;
  27. using Windows.Foundation;
  28. #region Enums
  29. /// <summary>
  30. /// Map Mode
  31. /// </summary>
  32. public enum MapMode
  33. {
  34. /// <summary>
  35. /// Stores are displayed in the map
  36. /// </summary>
  37. Stores,
  38. /// <summary>
  39. /// Map is showing directions using a Windows Phone Task
  40. /// </summary>
  41. Directions,
  42. /// <summary>
  43. /// Map is showing a route in the map
  44. /// </summary>
  45. Route
  46. }
  47. #endregion
  48. /// <summary>
  49. /// Maps sample page
  50. /// </summary>
  51. public partial class MapsSample : PhoneApplicationPage
  52. {
  53. /// <summary>
  54. /// Main view model used in this page
  55. /// </summary>
  56. private readonly MapsViewModel mainViewModel = new MapsViewModel();
  57. /// <summary>
  58. /// Seattle's GeoCoordinate
  59. /// </summary>
  60. private readonly GeoCoordinate seattleGeoCoordinate = new GeoCoordinate(47.60097, -122.3331);
  61. /// <summary>
  62. /// Zoom level to be used when showing the user location
  63. /// </summary>
  64. private readonly double userLocationMarkerZoomLevel = 16;
  65. /// <summary>
  66. /// Initializes a new instance of the <see cref="MapsSample"/> class
  67. /// </summary>
  68. public MapsSample()
  69. {
  70. this.InitializeComponent();
  71. this.MapExtensionsSetup(this.Map);
  72. this.DataContext = this.mainViewModel;
  73. this.HideStoresIconMenuItem = (ApplicationBarMenuItem)ApplicationBar.MenuItems[0];
  74. this.ShowStoresIconMenuItem = (ApplicationBarMenuItem)ApplicationBar.MenuItems[1];
  75. this.Loaded += this.OnPageLoaded;
  76. }
  77. /// <summary>
  78. /// Gets or sets the current Map Mode used in the page
  79. /// </summary>
  80. private MapMode Mode { get; set; }
  81. /// <summary>
  82. /// Gets or sets the route displayed in the map
  83. /// </summary>
  84. private MapRoute MapRoute { get; set; }
  85. /// <summary>
  86. /// Gets or sets the application bar menu item used to hide the stores displayed in the map
  87. /// </summary>
  88. private ApplicationBarMenuItem HideStoresIconMenuItem { get; set; }
  89. /// <summary>
  90. /// Gets or sets the application bar menu item used to show the stores displayed in the map
  91. /// </summary>
  92. private ApplicationBarMenuItem ShowStoresIconMenuItem { get; set; }
  93. #region Event Handlers
  94. /// <summary>
  95. /// Event handler to be called when the page has been loaded
  96. /// </summary>
  97. /// <param name="sender">Sender of the event</param>
  98. /// <param name="e">Event arguments</param>
  99. private void OnPageLoaded(object sender, RoutedEventArgs e)
  100. {
  101. this.Dispatcher.BeginInvoke(new Action(this.MapFlightToSeattle));
  102. this.StoresMapItemsControl.ItemsSource = this.mainViewModel.StoreList;
  103. }
  104. /// <summary>
  105. /// Event handler for the Me button. It will show the user location marker and set the view on the map
  106. /// </summary>
  107. /// <param name="sender">Sender of the event</param>
  108. /// <param name="e">Event arguments</param>
  109. private async void OnMe(object sender, EventArgs e)
  110. {
  111. await this.ShowUserLocation();
  112. this.Map.SetView(this.UserLocationMarker.GeoCoordinate, this.userLocationMarkerZoomLevel);
  113. }
  114. /// <summary>
  115. /// Event handler called when the user tap and hold in the map
  116. /// </summary>
  117. /// <param name="sender">Sender of the event</param>
  118. /// <param name="e">Event arguments</param>
  119. private async void OnMapHold(object sender, System.Windows.Input.GestureEventArgs e)
  120. {
  121. ReverseGeocodeQuery query;
  122. List<MapLocation> mapLocations;
  123. string pushpinContent;
  124. MapLocation mapLocation;
  125. query = new ReverseGeocodeQuery();
  126. query.GeoCoordinate = this.Map.ConvertViewportPointToGeoCoordinate(e.GetPosition(this.Map));
  127. mapLocations = (List<MapLocation>)await query.GetMapLocationsAsync();
  128. mapLocation = mapLocations.FirstOrDefault();
  129. if (mapLocation != null)
  130. {
  131. this.RouteDirectionsPushPin.GeoCoordinate = mapLocation.GeoCoordinate;
  132. pushpinContent = mapLocation.Information.Name;
  133. pushpinContent = string.IsNullOrEmpty(pushpinContent) ? mapLocation.Information.Description : null;
  134. pushpinContent = string.IsNullOrEmpty(pushpinContent) ? string.Format("{0} {1}", mapLocation.Information.Address.Street, mapLocation.Information.Address.City) : null;
  135. this.RouteDirectionsPushPin.Content = pushpinContent.Trim();
  136. this.RouteDirectionsPushPin.Visibility = Visibility.Visible;
  137. }
  138. }
  139. /// <summary>
  140. /// Event handler for the directions button
  141. /// </summary>
  142. /// <param name="sender">Sender of the event</param>
  143. /// <param name="e">Event arguments</param>
  144. private void OnDirections(object sender, EventArgs e)
  145. {
  146. if (this.RouteDirectionsPushPin.GeoCoordinate == null || this.RouteDirectionsPushPin.Visibility == Visibility.Collapsed)
  147. {
  148. MessageBox.Show("Tap and hold somewhere in the map to show a Pushpin. After that you can get directions to there");
  149. }
  150. else
  151. {
  152. BingMapsDirectionsTask directionsTask;
  153. directionsTask = new BingMapsDirectionsTask();
  154. directionsTask.End = new LabeledMapLocation()
  155. {
  156. Label = (string)this.RouteDirectionsPushPin.Content,
  157. Location = this.RouteDirectionsPushPin.GeoCoordinate
  158. };
  159. this.ChangeMode(MapMode.Directions);
  160. directionsTask.Show();
  161. }
  162. }
  163. /// <summary>
  164. /// Event handler for the show route button
  165. /// </summary>
  166. /// <param name="sender">Sender of the event</param>
  167. /// <param name="e">Event arguments</param>
  168. private async void OnShowRoute(object sender, EventArgs e)
  169. {
  170. if (this.RouteDirectionsPushPin.GeoCoordinate == null || this.RouteDirectionsPushPin.Visibility == Visibility.Collapsed)
  171. {
  172. MessageBox.Show("Tap and hold somewhere in the map to show a Pushpin. After that you can show route from your location to the destination");
  173. }
  174. else
  175. {
  176. RouteQuery query;
  177. List<GeoCoordinate> wayPoints;
  178. Route route;
  179. this.ShowStores(false);
  180. if (this.MapRoute != null)
  181. {
  182. this.Map.RemoveRoute(this.MapRoute);
  183. }
  184. await this.ShowUserLocation();
  185. query = new RouteQuery();
  186. wayPoints = new List<GeoCoordinate>();
  187. wayPoints.Add(this.UserLocationMarker.GeoCoordinate);
  188. wayPoints.Add(this.RouteDirectionsPushPin.GeoCoordinate);
  189. query.Waypoints = wayPoints;
  190. route = await query.GetRouteAsync();
  191. this.MapRoute = new MapRoute(route);
  192. this.Map.SetView(route.BoundingBox);
  193. this.Map.AddRoute(this.MapRoute);
  194. this.ChangeMode(MapMode.Route);
  195. }
  196. }
  197. /// <summary>
  198. /// Event handler for the Hide Stores menu item
  199. /// </summary>
  200. /// <param name="sender">Sender of the events</param>
  201. /// <param name="e">Event arguments</param>
  202. private void OnHideStores(object sender, EventArgs e)
  203. {
  204. this.ShowStores(false);
  205. }
  206. /// <summary>
  207. /// Event handler for the Show Stores menu item
  208. /// </summary>
  209. /// <param name="sender">Sender of the events</param>
  210. /// <param name="e">Event arguments</param>
  211. private void OnShowStores(object sender, EventArgs e)
  212. {
  213. this.ShowStores(true);
  214. }
  215. /// <summary>
  216. /// Toggles the visuals to show or hide the stores
  217. /// </summary>
  218. /// <param name="show">boolean whether the stores should be displayed or not</param>
  219. private void ShowStores(bool show)
  220. {
  221. this.ShowStoresIconMenuItem.IsEnabled = !show;
  222. this.HideStoresIconMenuItem.IsEnabled = show;
  223. foreach (Store store in this.mainViewModel.StoreList)
  224. {
  225. store.Visibility = show ? Visibility.Visible : Visibility.Collapsed;
  226. }
  227. if (show)
  228. {
  229. this.ChangeMode(MapMode.Stores);
  230. this.MapFlightToSeattle();
  231. }
  232. }
  233. #endregion
  234. #region Helpers
  235. /// <summary>
  236. /// Will set view in the map to an area that will display all the stores in the map
  237. /// </summary>
  238. private void MapFlightToSeattle()
  239. {
  240. LocationRectangle locationRectangle;
  241. locationRectangle = LocationRectangle.CreateBoundingRectangle(from store in this.mainViewModel.StoreList select store.GeoCoordinate);
  242. this.Map.SetView(locationRectangle, new Thickness(20, 20, 20, 20));
  243. }
  244. /// <summary>
  245. /// Show the user location in the map
  246. /// </summary>
  247. /// <returns>Task that can used to await</returns>
  248. private async Task ShowUserLocation()
  249. {
  250. Geolocator geolocator;
  251. Geoposition geoposition;
  252. this.UserLocationMarker = (UserLocationMarker)this.FindName("UserLocationMarker");
  253. geolocator = new Geolocator();
  254. geoposition = await geolocator.GetGeopositionAsync();
  255. this.UserLocationMarker.GeoCoordinate = geoposition.Coordinate.ToGeoCoordinate();
  256. this.UserLocationMarker.Visibility = System.Windows.Visibility.Visible;
  257. }
  258. /// <summary>
  259. /// Setup the map extensions objects.
  260. /// All named objects inside the map extensions will have its references properly set
  261. /// </summary>
  262. /// <param name="map">The map that uses the map extensions</param>
  263. private void MapExtensionsSetup(Map map)
  264. {
  265. ObservableCollection<DependencyObject> children = MapExtensions.GetChildren(map);
  266. var runtimeFields = this.GetType().GetRuntimeFields();
  267. foreach (DependencyObject i in children)
  268. {
  269. var info = i.GetType().GetProperty("Name");
  270. if (info != null)
  271. {
  272. string name = (string)info.GetValue(i);
  273. if (name != null)
  274. {
  275. foreach (FieldInfo j in runtimeFields)
  276. {
  277. if (j.Name == name)
  278. {
  279. j.SetValue(this, i);
  280. break;
  281. }
  282. }
  283. }
  284. }
  285. }
  286. }
  287. /// <summary>
  288. /// Get the current user location
  289. /// </summary>
  290. /// <returns>IAsyncOperation caller can await</returns>
  291. private IAsyncOperation<Geoposition> GetUserLocation()
  292. {
  293. Geolocator geolocator;
  294. geolocator = new Geolocator();
  295. return geolocator.GetGeopositionAsync();
  296. }
  297. /// <summary>
  298. /// Changes the effective map mode. Will switch visuals state it changed
  299. /// </summary>
  300. /// <param name="mode">New map mode</param>
  301. private void ChangeMode(MapMode mode)
  302. {
  303. if (this.Mode != mode)
  304. {
  305. this.Mode = mode;
  306. switch (this.Mode)
  307. {
  308. case MapMode.Stores:
  309. this.ShowStores(true);
  310. this.RouteDirectionsPushPin.Visibility = Visibility.Collapsed;
  311. if (this.MapRoute != null)
  312. {
  313. this.Map.RemoveRoute(this.MapRoute);
  314. }
  315. break;
  316. case MapMode.Route:
  317. this.ShowStores(false);
  318. break;
  319. case MapMode.Directions:
  320. this.ShowStores(false);
  321. if (this.MapRoute != null)
  322. {
  323. this.Map.RemoveRoute(this.MapRoute);
  324. }
  325. break;
  326. }
  327. }
  328. }
  329. #endregion
  330. }
  331. }