PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Crimezone/Crimezone/MainPage.xaml.cs

https://github.com/muhamadnurawaludin/Crimezone
C# | 280 lines | 216 code | 46 blank | 18 comment | 24 complexity | a6e03d8ec3bbe6aee3efa0a6a1bb3714 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Navigation;
  8. using Microsoft.Phone.Controls;
  9. using Microsoft.Phone.Shell;
  10. using Microsoft.Devices;
  11. using System.IO;
  12. using System.IO.IsolatedStorage;
  13. using Microsoft.Xna.Framework.Media;
  14. using System.Windows.Media;
  15. using Microsoft.Phone.Tasks;
  16. using System.Windows.Media.Imaging;
  17. using Newtonsoft.Json.Linq;
  18. using Crimezone.ViewModel;
  19. using Microsoft.Phone.Maps.Services;
  20. using System.Windows.Shapes;
  21. using Windows.Devices.Geolocation;
  22. using Microsoft.Phone.Maps.Controls;
  23. using System.Device.Location;
  24. using System.Windows.Input;
  25. using Crimezone.Resources;
  26. using MapPushpinSample;
  27. namespace Crimezone
  28. {
  29. public partial class MainPage : PhoneApplicationPage
  30. {
  31. private bool _isDirectionsShown = false;
  32. private GeoCoordinate MyCoordinate = null;
  33. private ProgressIndicator ProgressIndicator = null;
  34. private double _accuracy = 0.0;
  35. private List<GeoCoordinate> MyCoordinates = new List<GeoCoordinate>();
  36. private Route MyRoute = null;
  37. private ReverseGeocodeQuery MyReverseGeocodeQuery = null;
  38. public MainPage()
  39. {
  40. InitializeComponent();
  41. // LoadURLkegiatan();
  42. DataContext = new LaporanKejahatanViewModel();
  43. }
  44. protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
  45. {
  46. GetCurrentCoordinate();
  47. DrawMapMarkers();
  48. }
  49. private void ShowProgressIndicator(String msg)
  50. {
  51. if (ProgressIndicator == null)
  52. {
  53. ProgressIndicator = new ProgressIndicator();
  54. ProgressIndicator.IsIndeterminate = true;
  55. }
  56. ProgressIndicator.Text = msg;
  57. ProgressIndicator.IsVisible = true;
  58. SystemTray.SetProgressIndicator(this, ProgressIndicator);
  59. }
  60. private async void GetCurrentCoordinate()
  61. {
  62. ShowProgressIndicator("getting location...");
  63. Geolocator geolocator = new Geolocator();
  64. geolocator.DesiredAccuracy = PositionAccuracy.High;
  65. try
  66. {
  67. //Disini binding koordinat dari database
  68. //Ini koordinat kita
  69. Geoposition currentPosition = await geolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(10));
  70. _accuracy = currentPosition.Coordinate.Accuracy;
  71. Dispatcher.BeginInvoke(() =>
  72. {
  73. MyCoordinate = new GeoCoordinate(currentPosition.Coordinate.Latitude, currentPosition.Coordinate.Longitude);
  74. DrawMapMarkers();
  75. MyMap.SetView(MyCoordinate, 10, MapAnimationKind.Parabolic);
  76. });
  77. }
  78. catch (Exception)
  79. {
  80. // Couldn't get current location - location might be disabled in settings
  81. MessageBox.Show("Current location cannot be obtained. Check that location service is turned on in phone settings.", "MAP EXPLORER", MessageBoxButton.OK);
  82. }
  83. HideProgressIndicator();
  84. }
  85. private void HideProgressIndicator()
  86. {
  87. ProgressIndicator.IsVisible = false;
  88. SystemTray.SetProgressIndicator(this, ProgressIndicator);
  89. }
  90. private void DrawMapMarker(GeoCoordinate coordinate, Color color, MapLayer mapLayer)
  91. {
  92. //Map marker kejahatan
  93. //Image img = new Image();
  94. // Create a map marker
  95. // Image img = new Image();
  96. Polygon polygon = new Polygon();
  97. polygon.Points.Add(new Point(0, 0));
  98. polygon.Points.Add(new Point(0, 75));
  99. polygon.Points.Add(new Point(25, 0));
  100. polygon.Fill = new SolidColorBrush(color);
  101. // Enable marker to be tapped for location information
  102. polygon.Tag = new GeoCoordinate(coordinate.Latitude, coordinate.Longitude);
  103. polygon.MouseLeftButtonUp += new MouseButtonEventHandler(Marker_Click);
  104. //img.MouseLeftButtonUp += new MouseButtonEventHandler(Marker_Click);
  105. // Create a MapOverlay and add marker.
  106. MapOverlay overlay = new MapOverlay();
  107. overlay.Content = polygon;
  108. overlay.GeoCoordinate = new GeoCoordinate(coordinate.Latitude, coordinate.Longitude);
  109. overlay.PositionOrigin = new Point(0.0, 1.0);
  110. mapLayer.Add(overlay);
  111. }
  112. private void Marker_Click(object sender, MouseButtonEventArgs e)
  113. {
  114. Polygon p = (Polygon)sender;
  115. GeoCoordinate geoCoordinate = (GeoCoordinate)p.Tag;
  116. if (MyReverseGeocodeQuery == null || !MyReverseGeocodeQuery.IsBusy)
  117. {
  118. MyReverseGeocodeQuery = new ReverseGeocodeQuery();
  119. MyReverseGeocodeQuery.GeoCoordinate = new GeoCoordinate(geoCoordinate.Latitude, geoCoordinate.Longitude);
  120. MyReverseGeocodeQuery.QueryCompleted += ReverseGeocodeQuery_QueryCompleted;
  121. MyReverseGeocodeQuery.QueryAsync();
  122. }
  123. }
  124. private void ReverseGeocodeQuery_QueryCompleted(object sender, QueryCompletedEventArgs<IList<MapLocation>> e)
  125. {
  126. if (e.Error == null)
  127. {
  128. if (e.Result.Count > 0)
  129. {
  130. MapAddress address = e.Result[0].Information.Address;
  131. String msgBoxText = "";
  132. if (address.Street.Length > 0)
  133. {
  134. msgBoxText += "\n" + address.Street;
  135. if (address.HouseNumber.Length > 0) msgBoxText += " " + address.HouseNumber;
  136. }
  137. if (address.PostalCode.Length > 0) msgBoxText += "\n" + address.PostalCode;
  138. if (address.City.Length > 0) msgBoxText += "\n" + address.City;
  139. if (address.Country.Length > 0) msgBoxText += "\n" + address.Country;
  140. MessageBox.Show(msgBoxText, AppResources.ApplicationTitle, MessageBoxButton.OK);
  141. }
  142. else
  143. {
  144. MessageBox.Show("Location information not found.", AppResources.ApplicationTitle, MessageBoxButton.OK);
  145. }
  146. MyReverseGeocodeQuery.Dispose();
  147. }
  148. }
  149. private void DrawMapMarkers()
  150. {
  151. MyMap.Layers.Clear();
  152. MapLayer mapLayer = new MapLayer();
  153. // Draw marker for current position
  154. if (MyCoordinate != null)
  155. {
  156. DrawAccuracyRadius(mapLayer);
  157. DrawMapMarker(MyCoordinate, Colors.Red, mapLayer);
  158. //DrawMapMarker(koordinatkejahatan);
  159. }
  160. // Draw markers for location(s) / destination(s)
  161. for (int i = 0; i < MyCoordinates.Count; i++)
  162. {
  163. DrawMapMarker(MyCoordinates[i], Colors.Blue, mapLayer);
  164. }
  165. // Draw markers for possible waypoints when directions are shown.
  166. // Start and end points are already drawn with different colors.
  167. if (_isDirectionsShown && MyRoute.LengthInMeters > 0)
  168. {
  169. for (int i = 1; i < MyRoute.Legs[0].Maneuvers.Count - 1; i++)
  170. {
  171. DrawMapMarker(MyRoute.Legs[0].Maneuvers[i].StartGeoCoordinate, Colors.Purple, mapLayer);
  172. }
  173. }
  174. MyMap.Layers.Add(mapLayer);
  175. }
  176. private void DrawAccuracyRadius(MapLayer mapLayer)
  177. {
  178. // The ground resolution (in meters per pixel) varies depending on the level of detail
  179. // and the latitude at which it’s measured. It can be calculated as follows:
  180. double metersPerPixels = (Math.Cos(MyCoordinate.Latitude * Math.PI / 180) * 2 * Math.PI * 6378137) / (256 * Math.Pow(2, MyMap.ZoomLevel));
  181. double radius = _accuracy / metersPerPixels;
  182. Ellipse ellipse = new Ellipse();
  183. ellipse.Width = radius * 2;
  184. ellipse.Height = radius * 2;
  185. ellipse.Fill = new SolidColorBrush(Color.FromArgb(75, 200, 0, 0));
  186. MapOverlay overlay = new MapOverlay();
  187. overlay.Content = ellipse;
  188. overlay.GeoCoordinate = new GeoCoordinate(MyCoordinate.Latitude, MyCoordinate.Longitude);
  189. overlay.PositionOrigin = new Point(0.5, 0.5);
  190. mapLayer.Add(overlay);
  191. }
  192. private void ZoomLevelChanged(object sender, MapZoomLevelChangedEventArgs e)
  193. {
  194. DrawMapMarkers();
  195. }
  196. private void MyMap_Loaded(object sender, RoutedEventArgs e)
  197. {
  198. Microsoft.Phone.Maps.MapsSettings.ApplicationContext.ApplicationId = "__ApplicationID__";
  199. Microsoft.Phone.Maps.MapsSettings.ApplicationContext.AuthenticationToken = "__AuthenticationToken__";
  200. }
  201. private void CrimezoneNavigation(object sender, System.Windows.Input.GestureEventArgs e)
  202. {
  203. Grid tapping = (Grid)sender;
  204. if (tapping.Name.Equals("Timeline_Tap"))
  205. {
  206. Pivot_Control.SelectedIndex = 0;
  207. }
  208. else if (tapping.Name.Equals("Map_Tap"))
  209. {
  210. Pivot_Control.SelectedIndex = 1;
  211. }
  212. else if (tapping.Name.Equals("Polrestabes_Tap"))
  213. {
  214. Pivot_Control.SelectedIndex = 2;
  215. }
  216. }
  217. private void PostingLaporan_Click(object sender, EventArgs e)
  218. {
  219. NavigationService.Navigate(new Uri("/PostingLaporan.xaml", UriKind.Relative));
  220. }
  221. private void DetailKegiatanPolrestabes_Click(object sender, RoutedEventArgs e)
  222. {
  223. NavigationService.Navigate(new Uri("/DetailKegiatanPolrestabes.xaml", UriKind.Relative));
  224. }
  225. public void Method()
  226. {
  227. throw new System.NotImplementedException();
  228. }
  229. private void DetailPosting_Tap(object sender, System.Windows.Input.GestureEventArgs e)
  230. {
  231. NavigationService.Navigate(new Uri("/DetailPosting.xaml", UriKind.Relative));
  232. }
  233. }
  234. }