-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainPage.xaml.cs
1137 lines (1005 loc) · 41.9 KB
/
MainPage.xaml.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Xml.Linq;
using System.Windows.Resources;
using System.Windows.Threading;
using System.Threading;
using mapapp.data;
using Microsoft.Phone.Shell;
namespace mapapp
{
public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
{
internal const string Id = "INVALID_BING_MAPS_API_APP_ID";
private readonly CredentialsProvider _credentialsProvider = new ApplicationIdCredentialsProvider(Id);
public static readonly GeoCoordinate DefaultLocation = new GeoCoordinate(47.662929, -122.115863);
private const double DefaultZoomLevel = 16.0;
private const double MaxZoomLevel = 21.0;
private const double MinZoomLevel = 10.0;
private double _zoom = DefaultZoomLevel;
private GeoCoordinate _center = DefaultLocation;
public event PropertyChangedEventHandler PropertyChanged;
GeoCoordinateWatcher watcher;
private bool firstfind = true;
private bool showMap = false;
private bool showWait = true;
List<PushpinModel> listModels = new List<PushpinModel>();
// List<PrecinctPinModel> precinctList = new List<PrecinctPinModel>();
private bool _showPushpins = false;
private bool _showDetails = false;
private bool _showPrecincts = true;
private bool _showStreets = false;
private bool _showMe = false;
private bool _showCar = false;
// NOTE: This setting is used for testing during development, and should be set to true before compiling a production build
private bool _limitVoters = true;
// NOTE: These are rough estimates of the number of degrees covered in one mile
// in the Redmond, WA area. These should be calculated based on actual latitude
// of the center of the viewport.
// private double _mileLat = 0.015;
// private double _mileLong = 0.027;
private DataViewport _dataView = new DataViewport(DefaultLocation);
private List<PushpinModel> _dataViewPins = new List<PushpinModel>();
private String _PrecinctFilter = "";
private String _StreetFilter = "";
private int _MaxVotersInView = 600; // NOTE: This is an arbitrary value that can be adjusted based on app performance with larger numbers of voter pins visible.
private bool follow = true;
Thread backThread;
ManualResetEvent wait = new ManualResetEvent(false);
// quadtree tree = new quadtree();
double zoomlevel = 16.0;
private Pushpin _me;
public Pushpin Me
{
get { return _me; }
set
{
if (_me != value)
{
_me = value;
NotifyPropertyChanged("Me");
}
}
}
private Pushpin _car;
public Pushpin Car
{
get { return _car; }
set
{
if (_car != value)
{
_car = value;
NotifyPropertyChanged("Car");
}
}
}
private GeoCoordinate _here;
public GeoCoordinate CurrentLocation
{
get { return _here; }
}
bool running = true;
// private static mapapp.data.VoterFileDataContext _voterDB;
ApplicationBar _mainAppBar = new ApplicationBar();
// Constructor
public MainPage()
{
InitializeComponent();
DataContext = this;
// _here = new GeoCoordinate();
_here = DefaultLocation;
Me = new Pushpin();
Me.Content = "Me";
Car = new Pushpin();
Car.Content = "Car";
ShowCar = false;
// Map.
Map.ZoomBarVisibility = System.Windows.Visibility.Visible;
Map.MapZoom += new EventHandler<MapZoomEventArgs>(MapZoomed);
Map.MapPan += new EventHandler<MapDragEventArgs>(MapDragged);
Map.MapResolved += Map_MapResolved;
watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); // using high accuracy
watcher.MovementThreshold = 0.1; // use MovementThreshold to ignore noise in the signal
watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
if (_mainAppBar == null)
_mainAppBar = new ApplicationBar();
_mainAppBar.Mode = ApplicationBarMode.Default;
_mainAppBar.Opacity = 1.0;
_mainAppBar.IsVisible = true;
_mainAppBar.IsMenuEnabled = true;
ApplicationBarIconButton buttonLocation = new ApplicationBarIconButton();
buttonLocation.IconUri = new Uri("/Images/appbar.location.png", UriKind.Relative);
buttonLocation.Text = "find me";
buttonLocation.Click += Center_Click;
_mainAppBar.Buttons.Add(buttonLocation);
ApplicationBarIconButton buttonSatLayer = new ApplicationBarIconButton();
buttonSatLayer.IconUri = new Uri("/Images/appbar.satlayer.png", UriKind.Relative);
buttonSatLayer.Text = "street/sat";
buttonSatLayer.Click += ChangeMapMode;
_mainAppBar.Buttons.Add(buttonSatLayer);
ApplicationBarIconButton buttonShowPushpins = new ApplicationBarIconButton();
buttonShowPushpins.IconUri = new Uri("/Images/appbar.minus.png", UriKind.Relative);
buttonShowPushpins.Text = "pushpins";
buttonShowPushpins.Click += OnTogglePushpinViewMode;
_mainAppBar.Buttons.Add(buttonShowPushpins);
ApplicationBarIconButton buttonShowStreetList = new ApplicationBarIconButton();
buttonShowStreetList.IconUri = new Uri("/Images/appbar.menu.png", UriKind.Relative);
buttonShowStreetList.Text = "street list";
buttonShowStreetList.Click += ListStreets;
_mainAppBar.Buttons.Add(buttonShowStreetList);
ApplicationBarMenuItem menuitemZoomOut = new ApplicationBarMenuItem("zoom out");
menuitemZoomOut.Click += ZoomOut;
_mainAppBar.MenuItems.Add(menuitemZoomOut);
ApplicationBarMenuItem menuitemPlaceCar = new ApplicationBarMenuItem("set car location");
menuitemPlaceCar.Click += new EventHandler(menuitemPlaceCar_Click);
_mainAppBar.MenuItems.Add(menuitemPlaceCar);
ApplicationBarMenuItem menuitemFindCar = new ApplicationBarMenuItem("show car location");
menuitemFindCar.Click += new EventHandler(menuitemFindCar_Click);
_mainAppBar.MenuItems.Add(menuitemFindCar);
if (_limitVoters == false) // NOTE: This menu item is only useful in test mode when the voter list size is small
{
ApplicationBarMenuItem menuitemCenterOnVoters = new ApplicationBarMenuItem("center on voters");
menuitemCenterOnVoters.Click += new EventHandler(menuitemCenterOnVoters_Click);
_mainAppBar.MenuItems.Add(menuitemCenterOnVoters);
}
ApplicationBarMenuItem menuitemSendChanges = new ApplicationBarMenuItem("upload/download data");
menuitemSendChanges.Click += new EventHandler(menuitemSendChanges_Click);
_mainAppBar.MenuItems.Add(menuitemSendChanges);
ApplicationBar = _mainAppBar;
watcher.Start();
CenterLocation();
_dataView.SetSize(0.0);
Zoom = Map.ZoomLevel;
zoomlevel = Map.ZoomLevel;
App.thisApp.PropertyChanged += new PropertyChangedEventHandler(thisApp_PropertyChanged);
backThread = new Thread(BackgroundThread);
if (App.thisApp._settings.DbStatus == DbState.Loaded)
{
App.Log("DB already loaded. Starting background thread...");
// LoadPrecincts();
backThread.Start();
wait.Set();
}
else
{
MessageBoxResult result = MessageBox.Show("The voters are still being loaded. Stick around, they should start showing up soon.", "Loading voters...", MessageBoxButton.OK);
}
}
void Map_MapResolved(object sender, EventArgs e)
{
App.Log(string.Format("Map resolved event: {0}", e.ToString()));
}
void menuitemFindCar_Click(object sender, EventArgs e)
{
if (null != Car)
{
if (Car.Location.IsUnknown)
{
App.Log("Dude! Where's my car?!");
Center = Me.Location;
}
else
Center = Car.Location;
}
}
void menuitemPlaceCar_Click(object sender, EventArgs e)
{
if (null == Car)
{
Car = new Pushpin();
Car.Content = "Car";
}
Car.Location = Me.Location;
ShowCar = true;
}
void menuitemCenterOnVoters_Click(object sender, EventArgs e)
{
if (listModels.Count > 0)
{
GeoCoordinate votersCenter = FindCenterOfVoters(listModels);
if (!votersCenter.IsUnknown)
Center = votersCenter;
}
}
private GeoCoordinate FindCenterOfVoters(List<PushpinModel> list)
{
GeoCoordinate locCenter = new GeoCoordinate(Center.Latitude, Center.Longitude);
// List<PushpinModel> voters = GetLocalVoters(locCenter);
double west = list.Min<PushpinModel, double>(vMin => vMin.Location.Longitude); // smallest longitude found in list (or largest absolute value)
double east = list.Max<PushpinModel, double>(vMax => vMax.Location.Longitude); // largest longitude found in list (or smallest absolute value)
double north = list.Max<PushpinModel, double>(vMax => vMax.Location.Latitude); // largest latitude found in list
double south = list.Min<PushpinModel, double>(vMin => vMin.Location.Latitude); // smallest latitude found in list
double diffLat = north - south;
double diffLong = east - west;
locCenter.Latitude = south + (diffLat/2);
locCenter.Longitude = west + (diffLong/2);
return locCenter;
}
void menuitemSendChanges_Click(object sender, EventArgs e)
{
// App.thisApp.ReportUpdates();
// this.NavigationService.Navigate(new Uri("/LiveAccessPage.xaml", UriKind.Relative));
this.NavigationService.Navigate(new Uri("/DataManagePage.xaml", UriKind.Relative));
}
void thisApp_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
App.Log(" Property changed = " + e.PropertyName);
if (e.PropertyName.Equals("IsDbLoaded") && App.thisApp._settings.DbStatus == DbState.Loaded)
{
if (backThread.ThreadState == ThreadState.Unstarted)
{
App.Log("DB is finally loaded, starting background thread...");
// LoadPrecincts();
backThread.Start();
ShowPushpins = true;
}
}
}
public void BackgroundThread()
{
// LoadPushPins();
App.Log("Started background thread...");
LocationRect _lastRect = null;
double _lastZoom = 0.0;
while (wait.WaitOne())
{
App.Log(" Wait completed at top of background thread loop.");
wait.Reset();
if (running == false)
{
App.Log(" Exiting background thread.");
wait.Close();
wait.Dispose();
return;
}
try
{
if (Precincts.Count == 0)
{
App.Log(" Invoking LoadPrecincts...");
Dispatcher.BeginInvoke(LoadPrecincts);
}
if (Streets.Count == 0)
{
App.Log(" Invoking LoadStreets...");
Dispatcher.BeginInvoke(LoadStreets);
}
LocationRect lr = Map.BoundingRectangle;
if (lr.Equals(_lastRect)) // NOTE: the rect can only be equal if both center and zoom level are unchanged.
{
App.Log(" We haven't moved, continuing...");
continue;
}
bool bfullcontent = false;
if (_showDetails)
{
bfullcontent = true;
}
App.Log(" Made it through prelims - still here.");
IEnumerable<PushpinModel> selectedVoters = listModels.AsEnumerable();
if (true) // (!_dataView.IsWithinView(lr)) NOTE: Quick hack for TechRoanoke conference
{
App.Log(" Calling GetVoterList...");
selectedVoters = GetVoterList();
App.Log(String.Format(" {0} voters are in current voter list.", selectedVoters.Count()));
App.Log(" GetVoterList call completed.");
}
else
{
App.Log(" Still within current dataview, skipping call to GetLocalVoters.");
if (lr.Center == _lastRect.Center && zoomlevel == _lastZoom)
{
App.Log(" Still in same location. continuing...");
continue;
}
}
_lastRect = lr;
_lastZoom = zoomlevel;
// Now filter on precinct if set
if (_PrecinctFilter != "")
{
// _dataViewPins = from <PushpinModel> voter in _dataViewPins where voter.
selectedVoters = from PushpinModel aVoter in selectedVoters where aVoter.Precinct == _PrecinctFilter select aVoter;
App.Log(String.Format(" {0} voters are in {1} precinct.", selectedVoters.Count(), _PrecinctFilter));
}
// Now filter on street if selected
if (_StreetFilter != "")
{
selectedVoters = from PushpinModel aVoter in selectedVoters where aVoter.Street == _StreetFilter select aVoter;
App.Log(String.Format(" {0} voters are in {1} precinct and live on {2}.", selectedVoters.Count(), _PrecinctFilter, _StreetFilter));
}
// If resulting list is still more than preset limit (do 300 for now) then filter to nearest 300
if (selectedVoters.Count() > _MaxVotersInView)
{
_dataViewPins = GetViewVoterList(lr.Center, selectedVoters);
}
else
_dataViewPins = selectedVoters.ToList();
List<PushpinModel> l = new List<PushpinModel>(_dataViewPins.AsEnumerable());
int total = 0;
if (wait.WaitOne(0))
{
App.Log(" Wait was not signaled, continuing.");
continue;
}
App.Log(" Adding filtered pushpins to listModel.");
listModels.Clear();
foreach (PushpinModel p in l)
{
total++;
if (bfullcontent)
{
p.Content = p.FullName + "\r" + p.VoterFile.Address + " " + p.VoterFile.Address2;
}
else
{
p.Content = p.VoterFile.LastName;
}
if (p.Location.IsUnknown || p.Location.Latitude < 25.3 || p.Location.Longitude > -67.0)
{
// This pin doesn't have valid location info - Set the location for the pushpin to the center of the street or precinct
StreetPinModel _street = (from StreetPinModel _st in Streets where _st.Content == p.Street select _st).FirstOrDefault();
if (_street != null)
{
p.Location = _street.Center;
App.Log(string.Format("Voter at {0} lacks location, setting to center of {1} street.", p.Address, _street.Content));
}
}
listModels.Add(p);
if (wait.WaitOne(0))
{
break;
}
}
}
catch (ThreadAbortException)
{
running = false;
wait.Close();
wait.Dispose();
return;
}
catch (Exception ex)
{
App.Log("Something went wrong in the background thread. " + ex.ToString());
}
if (wait.WaitOne(0))
{
App.Log(" Wait was not signaled, continuing (4).");
continue;
}
App.Log(" Invoking UpdatePins...");
Dispatcher.BeginInvoke(UpdatePins);
}
running = false;
wait.Close();
wait.Dispose();
return;
}
public void UpdatePins()
{
Pushpins.Clear();
foreach (PushpinModel p in listModels)
{
Pushpins.Add(p);
}
NotifyPropertyChanged("Pushpins");
// Make sure we still have the right ApplicationBar
if (ApplicationBar != _mainAppBar)
ApplicationBar = _mainAppBar;
}
public void MapDragged(object o, MapDragEventArgs e)
{
// TODO: We need to reload the listModels again
zoomlevel = Map.ZoomLevel;
follow = false;
if (App.thisApp._settings.VoterCount > 200) // This supports location-based filtering on large voter sets
{
wait.Set();
}
}
public void MapZoomed(object o, MapZoomEventArgs e)
{
// we need to reload the listModels again
zoomlevel = Map.ZoomLevel;
App.Log("Map zoomed to level " + zoomlevel.ToString());
bool _visiblePinsChanged = false;
// When zoomed out to zoomlevel 15 and smaller show only the precinct pins
if (zoomlevel < 15)
{
if (!ShowPrecincts || ShowStreets || ShowPushpins || _PrecinctFilter != "")
{
_PrecinctFilter = "";
_StreetFilter = "";
ShowPrecincts = true;
ShowStreets = false;
ShowPushpins = false;
_visiblePinsChanged = true;
_showDetails = false;
}
}
// When zoomed between 15 and 16 show streets and precincts (TODO: Verify we want precincts to still be shown)
else if (zoomlevel >= 15 && zoomlevel <= 16.2)
{
if (ShowPushpins) // We are zooming out - just show the streets
{
}
if (!ShowStreets || ShowPrecincts || ShowPushpins || _StreetFilter != "")
{
_StreetFilter = "";
ShowPrecincts = false; // NOTE: Set this to false if precincts are in the way in street view
ShowStreets = true;
ShowPushpins = false;
_visiblePinsChanged = true;
_showDetails = false;
}
}
// When zoomed in to 16 or closer just show the individual voter pins, hide street and precinct pins
// TODO: We need a new pin type to represent an aggregate view of multi-family locations where
// multiple voters map to the same geographic location.
else
{
if (!ShowPushpins || ShowStreets || ShowPrecincts)
{
// NOTE: We are purposely not clearing the street or precinct filters when zooming in
ShowPushpins = true;
ShowStreets = false;
ShowPrecincts = false;
_visiblePinsChanged = true;
}
if (zoomlevel >= 18.0) // Needed to support changing pushpin content to expanded view
{
if (!_showDetails)
{
_showDetails = true;
_visiblePinsChanged = true;
}
}
else
{
if (_showDetails)
{
_showDetails = false;
_visiblePinsChanged = true;
}
}
}
if (_visiblePinsChanged)
wait.Set();
}
int SortDistance(PushpinModel p1, PushpinModel p2)
{
if (p1.dist > p2.dist)
{
return 1;
}
else if (p1.dist < p2.dist)
{
return -1;
}
return 0;
}
public CredentialsProvider CredentialsProvider
{
get { return _credentialsProvider; }
}
private List<PushpinModel> GetVoterList()
{
VoterFileDataContext _voterDB = new mapapp.data.VoterFileDataContext(string.Format(mapapp.data.VoterFileDataContext.DBConnectionString, App.thisApp._settings.DbFileName));
System.Diagnostics.Debug.Assert(_voterDB.DatabaseExists());
List<PushpinModel> _list = new List<PushpinModel>();
if (!(App.thisApp._settings.DbStatus == DbState.Loaded))
{
App.Log("Database not ready to load voters yet.");
return _list;
}
IEnumerable<VoterFileEntry> data = from VoterFileEntry voter in _voterDB.AllVoters select voter;
PushpinModel _pin;
int _counter = 0;
foreach (VoterFileEntry _v in data)
{
_pin = new PushpinModel(_v);
if (0.0 == _pin.Location.Latitude || 0.0 == _pin.Location.Longitude)
{
App.Log("Skipping voter with no location:" + _pin.Address);
continue;
}
_counter++;
if (!_pin.Location.IsUnknown)
{
_pin.Visibility = Visibility.Visible;
_list.Add(_pin);
}
else
{
System.Diagnostics.Debug.WriteLine("Error: A pin doesn't have a location: " + _pin.Content);
// This pin doesn't have valid location info - Set the location for the pushpin to the center of the street or precinct
StreetPinModel _street = (from StreetPinModel _st in Streets where _st.Content == _pin.Street select _st).FirstOrDefault();
if (_street != null)
{
_pin.Location = _street.Center;
App.Log(string.Format("Voter at {0} lacks location, setting to center of {1} street.", _pin.Address, _street.Content));
_list.Add(_pin);
}
}
}
if (_list.Count == 0 || _counter == 0)
{
App.Log("There are no voters around here!");
}
else
System.Diagnostics.Debug.WriteLine("Found {0} voters near here.", _counter);
_voterDB.Dispose();
return _list;
}
private List<PushpinModel> GetViewVoterList(GeoCoordinate center, IEnumerable<PushpinModel> voterList)
{
List<PushpinModel> _list = new List<PushpinModel>();
_dataView.SetCenterLocation(center);
if (voterList.Count() > _MaxVotersInView)
{
_dataView.SetSize(2.0);
_list = (from PushpinModel pin in voterList
where pin.Location.Latitude <= _dataView.North && pin.Location.Latitude >= _dataView.South &&
pin.Location.Longitude >= _dataView.West && pin.Location.Longitude <= _dataView.East
select pin).ToList();
}
else
{
_dataView.West = voterList.Min<PushpinModel, double>(vMin => vMin.Location.Longitude); // smallest longitude found in list (or largest absolute value)
_dataView.East = voterList.Max<PushpinModel, double>(vMax => vMax.Location.Longitude); // largest longitude found in list (or smallest absolute value)
_dataView.North = voterList.Max<PushpinModel, double>(vMax => vMax.Location.Latitude); // largest latitude found in list
_dataView.South = voterList.Min<PushpinModel, double>(vMin => vMin.Location.Latitude); // smallest latitude found in list
_list = voterList.OrderBy(voter => voter.Street).ToList();
}
return _list;
}
private void ChangeMapMode(object o, EventArgs e)
{
if (Map.Mode is AerialMode)
{
Map.Mode = new RoadMode();
}
else
{
Map.Mode = new AerialMode(true);
}
}
private void Follow(object o, EventArgs e)
{
follow = true;
}
public double Zoom
{
get { return _zoom; }
set
{
var coercedZoom = Math.Max(MinZoomLevel, Math.Min(MaxZoomLevel, value));
if (_zoom != coercedZoom)
{
_zoom = value;
NotifyPropertyChanged("Zoom");
}
}
}
public GeoCoordinate Center
{
get { return _center; }
set
{
if (_center != value)
{
_center = value;
NotifyPropertyChanged("Center");
}
}
}
private void CenterLocation()
{
if (null != Me)
{
if (Me.Location.IsUnknown)
{
App.Log("I don't know where I am!");
Center = DefaultLocation;
}
else
Center = Me.Location;
}
}
private void CreateNewPushpin(GeoCoordinate location)
{
PushpinModel p = new PushpinModel();
p.Location = location;
Pushpins.Add(p);
}
private readonly ObservableCollection<PushpinModel> _pushpins = new ObservableCollection<PushpinModel>
{
};
public ObservableCollection<PushpinModel> Pushpins
{
get { return _pushpins; }
}
public bool ShowPushpins
{
get { return _showPushpins; }
set
{
if (_showPushpins != value)
{
_showPushpins = value;
NotifyPropertyChanged("ShowPushpins");
}
}
}
private readonly ObservableCollection<StreetPinModel> _streetPins = new ObservableCollection<StreetPinModel> {};
public ObservableCollection<StreetPinModel> Streets
{
get { return _streetPins; }
}
public bool ShowStreets
{
get { return _showStreets; }
set
{
if (_showStreets != value)
{
_showStreets = value;
NotifyPropertyChanged("ShowStreets");
}
}
}
private void LoadStreets()
{
VoterFileDataContext _voterDB = new mapapp.data.VoterFileDataContext(string.Format(mapapp.data.VoterFileDataContext.DBConnectionString, App.thisApp._settings.DbFileName));
System.Diagnostics.Debug.Assert(_voterDB.DatabaseExists());
if (!(App.thisApp._settings.DbStatus == DbState.Loaded))
{
App.Log("Database not ready to load streets yet.");
return;
}
IEnumerable<StreetTableEntry> streets = (from street in _voterDB.Streets select street);
foreach (StreetTableEntry street in streets)
{
App.Log(" Adding street: " + street.Name);
StreetPinModel pPin = new StreetPinModel(street);
Streets.Add(pPin);
}
_voterDB.Dispose();
}
private readonly ObservableCollection<PrecinctPinModel> _precinctPins = new ObservableCollection<PrecinctPinModel> {};
public ObservableCollection<PrecinctPinModel> Precincts
{
get { return _precinctPins; }
}
public bool ShowPrecincts
{
get { return _showPrecincts; }
set
{
if (_showPrecincts != value)
{
_showPrecincts = value;
NotifyPropertyChanged("ShowPrecincts");
}
}
}
private void LoadPrecincts()
{
VoterFileDataContext _voterDB = new mapapp.data.VoterFileDataContext(string.Format(mapapp.data.VoterFileDataContext.DBConnectionString, App.thisApp._settings.DbFileName));
System.Diagnostics.Debug.Assert(_voterDB.DatabaseExists());
if (!(App.thisApp._settings.DbStatus == DbState.Loaded))
{
App.Log("Database not ready to load precincts yet.");
return;
}
IEnumerable<PrecinctTableEntry> precincts = (from precinct in _voterDB.Precincts select precinct);
foreach (PrecinctTableEntry precinct in precincts)
{
App.Log(" Adding precinct: " + precinct.Name);
PrecinctPinModel pPin = new PrecinctPinModel(precinct);
Precincts.Add(pPin);
}
_voterDB.Dispose();
}
public bool ShowMe
{
get { return _showMe; }
set
{
_showMe = value;
NotifyPropertyChanged("ShowMe");
}
}
public bool ShowCar
{
get { return _showCar; }
set
{
if (_showCar != value)
{
_showCar = value;
NotifyPropertyChanged("ShowCar");
}
}
}
public void ZoomOut(object sender, EventArgs e)
{
if (Zoom > MinZoomLevel + 1)
Zoom = Zoom - 1;
else
Zoom = MinZoomLevel;
MapZoomed(sender, e as MapZoomEventArgs);
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
if (CheckAccess())
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
else
{
Dispatcher.BeginInvoke(new Action<string>(NotifyPropertyChanged), propertyName);
}
}
}
private void Center_Click(object sender, EventArgs e)
{
CenterLocation();
follow = true;
NotifyPropertyChanged("Pushpins");
}
// Event handler for the GeoCoordinateWatcher.StatusChanged event.
void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
switch (e.Status)
{
case GeoPositionStatus.Disabled:
// The Location Service is disabled or unsupported.
// Check to see whether the user has disabled the Location Service.
if (watcher.Permission == GeoPositionPermission.Denied)
{
watcher.Stop();
}
else
{
watcher.Stop();
}
break;
case GeoPositionStatus.Initializing:
break;
case GeoPositionStatus.NoData:
watcher.Stop();
break;
case GeoPositionStatus.Ready:
{
ShowMe = true;
}
break;
}
wait.Set();
}
void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
{
if (null == Me)
{
App.Log("I don't exist.");
return;
}
if (Me.Location.IsUnknown)
App.Log("The map moved, but I don't know where I am!");
if (System.Diagnostics.Debugger.IsAttached)
{
if (CurrentLocation != null && CurrentLocation.IsUnknown)
{
_here = DefaultLocation;
Me.Location = DefaultLocation;
}
else
{
_here = e.Position.Location;
Me.Location = e.Position.Location;
}
}
else
{
_here = e.Position.Location;
Me.Location = e.Position.Location;
}
if (!Me.Location.IsUnknown)
{
ShowMe = true;
}
if (firstfind || follow)
{
App.Log(" Centering for first time, or following location");
CenterLocation();
}
if (firstfind)
{
Zoom = DefaultZoomLevel;
zoomlevel = Zoom;
firstfind = false;
Map.Visibility = System.Windows.Visibility.Visible;
waitBar.Visibility = System.Windows.Visibility.Collapsed;
}
App.Log(string.Format(" Position changed: Zoom={0}, zoomLevel={1}, Map.TargetZoom={2}", Zoom, zoomlevel, Map.TargetZoomLevel));
NotifyPropertyChanged("Pushpins");
NotifyPropertyChanged("Me");
NotifyPropertyChanged("CurrentLocation");
App.Log(" Setting thread wait from watcher_PositionChanged");
wait.Set();
}
private void ListStreets(object sender, EventArgs e)
{
App.VotersViewModel.VoterList.Clear();
foreach (PushpinModel p in listModels)
{
if (p.Content.Equals("Me"))
continue;
if ((p.Street != null) && (!App.VotersViewModel.StreetList.Contains(p.Street)))
{
App.VotersViewModel.StreetList.Add(p.Street);
}
App.VotersViewModel.VoterList.Add(p);
}
this.NavigationService.Navigate(new Uri("/HouseListPage.xaml", UriKind.Relative));
}
private void PhoneApplicationPage_BackKeyPress(object sender, CancelEventArgs e)
{
if (watcher != null)
watcher.Stop();
if (backThread != null && backThread.ThreadState != ThreadState.Stopped)
{
running = false;
wait.Set();
}
if (this.NavigationService.CanGoBack)
this.NavigationService.GoBack();
}
private void OnTogglePushpinViewMode(object sender, EventArgs e)