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