123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using Comal.Classes;
- using Comal.Stores;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using Syncfusion.UI.Xaml.Maps;
- namespace PRSDesktop
- {
- public class MapMarker
- {
- public Guid ID { get; set; }
- public string Latitude { get; set; }
- public string Longitude { get; set; }
- public string Code { get; set; }
- public string Description { get; set; }
- public DateTime Updated { get; set; }
- public string UpdatedBy { get; set; }
- public string DeviceID { get; set; }
- public Brush Background { get; internal set; }
- }
- public class ViewModel
- {
- private readonly List<MapMarker> _markers = new();
- private MapMarker _selected;
- public ViewModel()
- {
- Markers = new ObservableCollection<MapMarker>();
- }
- public ObservableCollection<MapMarker> Markers { get; }
- public MapMarker Selected
- {
- get => _selected;
- set
- {
- _selected = value;
- Refresh();
- }
- }
- public void Add(MapMarker marker, bool refresh = false)
- {
- _markers.Add(marker);
- if (refresh)
- Refresh();
- }
- public void Clear()
- {
- _selected = null;
- _markers.Clear();
- Refresh();
- }
- public void Refresh()
- {
- Markers.Clear();
- foreach (var marker in _markers)
- {
- marker.Background = new SolidColorBrush(marker == _selected ? Colors.Yellow : Colors.Orange);
- if (marker != _selected)
- Markers.Add(marker);
- }
- if (_selected != null)
- Markers.Add(_selected);
- }
- }
- public class ImageryLayerExt : ImageryLayer
- {
- protected override string GetUri(int X, int Y, int Scale)
- {
- var link = "http://mt1.google.com/vt/lyrs=y&x=" + X + "&y=" + Y + "&z=" + Scale;
- return link;
- }
- }
- public class GPSHistory
- {
- public DateTime Date { get; set; }
- public string Description { get; set; }
- public double Latitude { get; set; }
- public double Longitude { get; set; }
- }
- /// <summary>
- /// Interaction logic for MapsPanel.xaml
- /// </summary>
- public partial class MapsPanel : UserControl, IPanel<GPSTracker>
- {
- private LiveMapsSettings _settings;
- private bool bFirst = true;
- private readonly ViewModel viewmodel = new();
- private readonly int ZOOMEDIN = 16;
- public MapsPanel()
- {
- InitializeComponent();
- DataContext = viewmodel;
- //ausgov.Uri = "c:\\development\\maps\\MB_2011_WA.shp";
- }
- public bool IsReady { get; set; }
- public void CreateToolbarButtons(IPanelHost host)
- {
- }
- public string SectionName => "Maps";
- public DataModel DataModel(Selection selection)
- {
- return new MapsDataModel();
- }
- public void Heartbeat(TimeSpan time)
- {
- }
- public void Refresh()
- {
- var type = ViewType.SelectedItem as string;
- if (!string.IsNullOrWhiteSpace(type))
- {
- var grpid = ((KeyValuePair<Guid, string>)ViewGroup.SelectedItem).Key;
- if (type.Equals("Equipment"))
- {
- var filter = new Filter<Equipment>(x => x.TrackerLink.Location.Timestamp).IsNotEqualTo(DateTime.MinValue);
- if (!Security.IsAllowed<CanViewPrivateEquipment>())
- filter = filter.And(x => x.Private).IsEqualTo(false);
- if (grpid != Guid.Empty)
- filter = filter.And(x => x.GroupLink.ID).IsEqualTo(grpid);
- LoadMarkers(
- filter,
- x => x.ID,
- x => x.Code,
- x => x.Description,
- x => x.TrackerLink.DeviceID,
- x => x.TrackerLink.Location.Latitude,
- x => x.TrackerLink.Location.Longitude,
- x => x.TrackerLink.Description,
- x => x.TrackerLink.Location.Timestamp
- );
- }
- else if (type.Equals("Jobs"))
- {
- var filter = new Filter<Job>(x => x.SiteAddress.Location.Timestamp).IsNotEqualTo(DateTime.MinValue);
- if (grpid != Guid.Empty)
- filter = filter.And(x => x.JobStatus.ID).IsEqualTo(grpid);
- LoadMarkers(
- filter,
- x => x.ID,
- x => x.JobNumber,
- x => x.Name,
- null,
- x => x.SiteAddress.Location.Latitude,
- x => x.SiteAddress.Location.Longitude,
- x => x.LastUpdateBy,
- x => x.SiteAddress.Location.Timestamp
- );
- }
- else if (type.Equals("TimeSheets"))
- {
- if (grpid == Guid.Empty) // Starts
- LoadMarkers(
- new Filter<TimeSheet>(x => x.Date).IsEqualTo(DateTime.Today).And(x => x.StartLocation.Timestamp)
- .IsNotEqualTo(new TimeSpan(0)),
- x => x.EmployeeLink.ID,
- x => x.EmployeeLink.Code,
- x => x.EmployeeLink.Name,
- null,
- x => x.StartLocation.Latitude,
- x => x.StartLocation.Longitude,
- x => x.SoftwareVersion,
- x => x.StartLocation.Timestamp
- );
- else if (grpid == CoreUtils.FullGuid) // Finishes
- LoadMarkers(
- new Filter<TimeSheet>(x => x.Date).IsEqualTo(DateTime.Today).And(x => x.Finish).IsNotEqualTo(new TimeSpan(0))
- .And(x => x.FinishLocation.Timestamp).IsNotEqualTo(new TimeSpan(0)),
- x => x.EmployeeLink.ID,
- x => x.EmployeeLink.Code,
- x => x.EmployeeLink.Name,
- null,
- x => x.FinishLocation.Latitude,
- x => x.FinishLocation.Longitude,
- x => x.SoftwareVersion,
- x => x.FinishLocation.Timestamp
- );
- else // Open
- LoadMarkers(
- new Filter<TimeSheet>(x => x.Date).IsEqualTo(DateTime.Today).And(x => x.Finish).IsEqualTo(new TimeSpan(0))
- .And(x => x.StartLocation.Timestamp).IsNotEqualTo(new TimeSpan(0)),
- x => x.EmployeeLink.ID,
- x => x.EmployeeLink.Code,
- x => x.EmployeeLink.Name,
- null,
- x => x.StartLocation.Latitude,
- x => x.StartLocation.Longitude,
- x => x.SoftwareVersion,
- x => x.StartLocation.Timestamp
- );
- }
- else if (type.Equals("Trackers"))
- {
- var filter = new Filter<GPSTracker>(x => x.Location.Timestamp).IsNotEqualTo(DateTime.MinValue);
- if (grpid != Guid.Empty)
- filter = filter.And(x => x.Type.ID).IsEqualTo(grpid);
- LoadMarkers(
- filter,
- x => x.ID,
- x => x.DeviceID,
- x => x.Description,
- x => x.DeviceID,
- x => x.Location.Latitude,
- x => x.Location.Longitude,
- x => x.LastUpdateBy,
- x => x.Location.Timestamp
- );
- }
- }
- }
- public Dictionary<string, object[]> Selected()
- {
- return new Dictionary<string, object[]>();
- }
- public void Setup()
- {
- _settings = new UserConfiguration<LiveMapsSettings>().Load();
- if (ClientFactory.IsSupported<Equipment>())
- ViewType.Items.Add("Equipment");
- if (ClientFactory.IsSupported<Job>())
- ViewType.Items.Add("Jobs");
- if (ClientFactory.IsSupported<TimeSheet>())
- ViewType.Items.Add("TimeSheets");
- if (ClientFactory.IsSupported<GPSTracker>())
- ViewType.Items.Add("Trackers");
- ViewType.SelectedIndex = ViewType.Items.Count > _settings.ViewType ? _settings.ViewType : -1;
- ViewType.SelectionChanged += ViewTypeSelectionChanged;
- var groups = ViewType.SelectedIndex > -1 ? LoadGroups(ViewType.SelectedItem as string) : LoadGroups("");
- ViewGroup.ItemsSource = groups;
- ViewGroup.SelectedIndex = ViewGroup.Items.Count > _settings.ViewGroup ? _settings.ViewGroup : -1;
- ViewGroup.SelectionChanged += ViewGroupSelectionChanged;
- }
- public void Shutdown()
- {
- }
- public event DataModelUpdateEvent OnUpdateDataModel;
- public Dictionary<Type, CoreTable> DataEnvironment()
- {
- return new Dictionary<Type, CoreTable>();
- }
- private void LoadGroups<TGroup>(string all, Dictionary<Guid, string> results, Expression<Func<TGroup, string>> description)
- where TGroup : Entity, IRemotable, IPersistent, new()
- {
- results.Clear();
- results.Add(Guid.Empty, all);
- var groups = new Client<TGroup>().Query(
- LookupFactory.DefineFilter<TGroup>(),
- LookupFactory.DefineColumns<TGroup>(),
- LookupFactory.DefineSort<TGroup>()
- );
- foreach (var row in groups.Rows)
- results[row.Get<TGroup, Guid>(x => x.ID)] = row.Get(description);
- }
- private void ViewTypeSelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- var sel = e.AddedItems.Count > 0 ? e.AddedItems[0] as string : null;
- if (string.IsNullOrWhiteSpace(sel))
- return;
- var groups = LoadGroups(sel);
- ViewGroup.SelectionChanged -= ViewGroupSelectionChanged;
- ViewGroup.ItemsSource = groups;
- ViewGroup.SelectionChanged += ViewGroupSelectionChanged;
- ViewGroup.SelectedIndex = groups.Any() ? 0 : -1;
- }
- private Dictionary<Guid, string> LoadGroups(string sel)
- {
- var result = new Dictionary<Guid, string>();
- if (sel.Equals("Equipment"))
- {
- LoadGroups<EquipmentGroup>("All Equipment", result, x => x.Description);
- }
- else if (sel.Equals("Jobs"))
- {
- LoadGroups<JobStatus>("All Jobs", result, x => x.Description);
- }
- else if (sel.Equals("TimeSheets"))
- {
- result[Guid.NewGuid()] = "Open TimeSheets";
- result[Guid.Empty] = "TimeSheet Starts";
- result[CoreUtils.FullGuid] = "TimeSheet Finishes";
- }
- else if (sel.Equals("Trackers"))
- {
- LoadGroups<GPSTrackerType>("All Trackers", result, x => x.Description);
- }
- return result;
- }
- private void ViewGroupSelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- _settings.ViewType = ViewType.SelectedIndex;
- _settings.ViewGroup = ViewGroup.SelectedIndex;
- new UserConfiguration<LiveMapsSettings>().Save(_settings);
- Refresh();
- }
- private void LoadDayMarkers()
- {
- viewmodel.Markers.Clear();
- }
- private void LoadMarkers<T>(Filter<T> filter, Expression<Func<T, object>> guid, Expression<Func<T, object>> code,
- Expression<Func<T, object>> description, Expression<Func<T, object>> deviceid, Expression<Func<T, object>> latitude,
- Expression<Func<T, object>> longitude, Expression<Func<T, object>> updatedby, Expression<Func<T, object>> timestamp, bool first = true)
- where T : Entity, IRemotable, IPersistent, new()
- {
- viewmodel.Clear();
- var columns = new Columns<T>(guid, code, description, latitude, longitude, timestamp, updatedby);
- if (deviceid != null && !columns.ColumnNames().Contains(CoreUtils.GetFullPropertyName(deviceid, ".")))
- columns.Add(deviceid);
- new Client<T>().Query(
- filter,
- columns,
- null,
- (table, error) =>
- {
- Dispatcher.Invoke(() =>
- {
- viewmodel.Markers.Clear();
- if (error != null)
- {
- MessageBox.Show(error.Message);
- }
- else
- {
- var values = new Dictionary<Guid, DbMarker>();
- foreach (var row in table.Rows)
- {
- var id = (Guid)row.Get(guid);
- var time = (DateTime)row.Get(timestamp);
- if (!values.ContainsKey(id) || (first ? values[id].TimeStamp < time : values[id].TimeStamp > time))
- values[id] = new DbMarker(
- code != null ? (string)row.Get(code) : "",
- (string)row.Get(description),
- time,
- (double)row.Get(latitude),
- (double)row.Get(longitude),
- (string)row.Get(updatedby),
- deviceid != null ? (string)row.Get(deviceid) : ""
- );
- }
- foreach (var key in values.Keys)
- viewmodel.Add(
- new MapMarker
- {
- ID = key,
- Code = values[key].Code,
- Latitude = string.Format("{0:F6}", values[key].Latitude),
- Longitude = string.Format("{0:F6}", values[key].Longitude),
- Description = values[key].Description,
- Updated = values[key].TimeStamp,
- UpdatedBy = values[key].UpdatedBy,
- DeviceID = values[key].DeviceID
- }
- );
- viewmodel.Refresh();
- }
- //if (bFirst)
- ResetZoom();
- bFirst = false;
- LoadMarkerList();
- });
- }
- );
- }
- private void ResetZoom()
- {
- var nwLon = double.MaxValue;
- var nwLat = double.MinValue;
- var seLon = double.MinValue;
- var seLat = double.MaxValue;
- foreach (var marker in viewmodel.Markers)
- {
- var lat = double.Parse(marker.Latitude);
- var lon = double.Parse(marker.Longitude);
- if (lat != 0.0F && lon != 0.00)
- {
- nwLat = lat > nwLat ? lat : nwLat;
- seLat = lat < seLat ? lat : seLat;
- nwLon = lon < nwLon ? lon : nwLon;
- seLon = lon > seLon ? lon : seLon;
- }
- }
- var layer = Map.Layers[0] as ImageryLayer;
- var cLat = (nwLat + seLat) / 2.0F;
- var cLon = (nwLon + seLon) / 2.0F;
- layer.Center = new Point(cLat, cLon);
- layer.DistanceType = DistanceType.KiloMeter;
- var c = new Location { Latitude = cLat, Longitude = cLon };
- var nw = new Location { Latitude = nwLat, Longitude = nwLon };
- layer.Radius = c.DistanceTo(nw, UnitOfLength.Kilometers) / 2.0F;
- }
- private void ZoomIn_Click(object sender, RoutedEventArgs e)
- {
- CenterMap();
- if (Map.ZoomLevel < 20)
- {
- Map.ZoomLevel++;
- LoadMarkerList();
- }
- }
- private void ZoomOut_Click(object sender, RoutedEventArgs e)
- {
- CenterMap();
- if (Map.ZoomLevel > 1)
- {
- Map.ZoomLevel--;
- LoadMarkerList();
- }
- }
- private void Map_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
- {
- //CenterMap();
- if (e.Delta > 0)
- {
- if (Zoom.Value < 20)
- Zoom.Value++;
- e.Handled = true;
- }
- else if (e.Delta < 0)
- {
- if (Zoom.Value > 1)
- Zoom.Value--;
- e.Handled = true;
- }
- }
- private void CenterMap()
- {
- var layer = Map.Layers[0] as ImageryLayer;
- var center = layer.GetLatLonFromPoint(new Point(Map.ActualWidth / 2.0F, Map.ActualHeight / 2.0F));
- layer.Center = new Point(center.Y, center.X);
- }
- private void Map_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
- {
- var layer = Map.Layers[0] as ImageryLayer;
- var center = layer.GetLatLonFromPoint(e.GetPosition(Map));
- layer.Center = new Point(center.Y, center.X);
- if (Map.ZoomLevel < 20)
- {
- Map.ZoomLevel++;
- LoadMarkerList();
- }
- e.Handled = true;
- }
- private void Image_PreviewMouseDown(object sender, MouseButtonEventArgs e)
- {
- var border = sender as Border;
- //Point pBorder = border.TransformToAncestor(Map).Transform(new Point(0, 0));
- //var left = pBorder.X - 2;
- //var top = pBorder.Y - 2;
- //var right = left + border.ActualWidth + 4;
- //var bottom = top + border.ActualHeight + 4;
- //ImageryLayer layer = Map.Layers[0] as ImageryLayer;
- //var nw = layer.GetLatLonFromPoint(new Point(left, top));
- //var se = layer.GetLatLonFromPoint(new Point(right, bottom));
- var found = border.DataContext as MapMarker;
- Markers.SelectedItem = found;
- if (e.ClickCount > 1 && e.ButtonState == MouseButtonState.Pressed && e.ChangedButton == MouseButton.Left)
- ZoomToMarker(found);
- //foreach (var marker in viewmodel.Markers)
- //{
- // if (IsMarkerVisible(marker, nw, se))
- // {
- // found = marker;
- // break;
- // }
- //}
- //if (found != null)
- //{
- // Description.Text = found.Description;
- // LastUpdate.Text = String.Format("{0:dd MMM yy hh:mm:ss}", found.Updated);
- // Device.Text = found.UpdatedBy;
- // History.Visibility = String.IsNullOrWhiteSpace(found.DeviceID) ? Visibility.Collapsed : Visibility.Visible;
- // LoadDeviceHistory(found.DeviceID);
- // viewmodel.Selected = found;
- //}
- //else
- //{
- // Description.Text = "";
- // LastUpdate.Text = "";
- // Device.Text = "";
- // History.Visibility = Visibility.Collapsed;
- //}
- }
- private void Date_DateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var marker = Markers.SelectedItem as MapMarker;
- if (marker == null)
- return;
- LoadDeviceHistory(marker.DeviceID);
- }
- private void LoadDeviceHistory(string deviceID)
- {
- History.ItemsSource = null;
- var movements = new ObservableCollection<GPSHistory>();
- History.ItemsSource = movements;
- new Client<GPSTrackerLocation>().Query(
- new Filter<GPSTrackerLocation>(x => x.DeviceID).IsEqualTo(deviceID)
- .And(x => x.Location.Timestamp).IsGreaterThanOrEqualTo(Date.Date.Date)
- .And(x => x.Location.Timestamp).IsLessThan(Date.Date.Date.AddDays(1)),
- null,
- new SortOrder<GPSTrackerLocation>(x => x.Location.Timestamp, SortDirection.Descending),
- (table, error) =>
- {
- if (table != null)
- {
- var updates = new List<GPSTrackerLocation>();
- var last = new Location { Latitude = 0.0F, Longitude = 0.0F };
- var q = new List<CoreRow>();
- for (var i = 0; i < Math.Min(3, table.Rows.Count); i++)
- q.Add(table.Rows[i]);
- if (q.Count > 0)
- AddHistory(movements, updates, q[0]);
- for (var i = 3; i < table.Rows.Count; i++)
- {
- if (Moved(q, 0, 1) || Moved(q, 1, 2))
- AddHistory(movements, updates, q[1]);
- // Slide the window up
- q.RemoveAt(0);
- q.Add(table.Rows[i]);
- }
- // If there are only two records in the table,
- // make sure we add the second
- if (q.Count == 2)
- AddHistory(movements, updates, q[1]);
- // if there are three or more records,
- // make sure we add the last
- if (Moved(q, 2, 3))
- AddHistory(movements, updates, q[2]);
- //foreach (var row in table.Rows)
- //{
- // InABox.Core.Location cur = new InABox.Core.Location() { Latitude = row.Get<GPSTrackerLocation, double>(x => x.Location.Latitude), Longitude = row.Get<GPSTrackerLocation, double>(x => x.Location.Longitude) };
- // if ((cur.Latitude != 0.0F) && (cur.Longitude != 0.0F)) // && (cur.DistanceTo(last, UnitOfLength.Kilometers) * 1000F > 500F))
- // {
- // AddHistory(movements, updates, row);
- // last = cur;
- // }
- //}
- if (updates.Any())
- new Client<GPSTrackerLocation>().Save(updates, "", (o, e) => { });
- if (!movements.Any())
- {
- var history = new GPSHistory
- {
- Date = DateTime.Today,
- Description = "No Activity Recorded",
- Latitude = 0.0F,
- Longitude = 0.0F
- };
- Dispatcher.Invoke(() => { movements.Add(history); });
- }
- }
- }
- );
- }
- private bool Moved(List<CoreRow> rows, int row1, int row2)
- {
- if (rows.Count <= row1)
- return false;
- if (rows.Count <= row2)
- return false;
- //InABox.Core.Location c0 = new InABox.Core.Location() { Latitude = rows[row1].Get<GPSTrackerLocation, double>(x => x.Location.Latitude), Longitude = rows[row1].Get<GPSTrackerLocation, double>(x => x.Location.Longitude) };
- //InABox.Core.Location c1 = new InABox.Core.Location() { Latitude = rows[row2].Get<GPSTrackerLocation, double>(x => x.Location.Latitude), Longitude = rows[row2].Get<GPSTrackerLocation, double>(x => x.Location.Longitude) };
- //return c1.DistanceTo(c0, UnitOfLength.Kilometers) * 1000F > 25F;
- var bDate = DateTime.Equals(
- rows[row1].Get<GPSTrackerLocation, DateTime>(x => x.Created).Date,
- rows[row2].Get<GPSTrackerLocation, DateTime>(x => x.Created).Date
- );
- var bAddress = string.Equals(
- rows[row1].Get<GPSTrackerLocation, string>(x => x.Location.Address),
- rows[row2].Get<GPSTrackerLocation, string>(x => x.Location.Address)
- );
- return /* !bDate || */ !bAddress;
- }
- private void AddHistory(ObservableCollection<GPSHistory> movements, List<GPSTrackerLocation> updates, CoreRow row)
- {
- var cur = new Location
- {
- Latitude = row.Get<GPSTrackerLocation, double>(x => x.Location.Latitude),
- Longitude = row.Get<GPSTrackerLocation, double>(x => x.Location.Longitude)
- };
- var address = row.Get<GPSTrackerLocation, string>(x => x.Location.Address);
- if (string.IsNullOrWhiteSpace(address))
- {
- address = StoreUtils.ReverseGeocode(cur.Latitude, cur.Longitude);
- var update = row.ToObject<GPSTrackerLocation>();
- update.Location.Address = address;
- updates.Add(update);
- }
- var history = new GPSHistory
- {
- Date = row.Get<GPSTrackerLocation, DateTime>(x => x.Location.Timestamp),
- Description = address,
- Latitude = cur.Latitude,
- Longitude = cur.Longitude
- };
- Dispatcher.Invoke(() => { movements.Add(history); });
- }
- private static bool IsMarkerVisible(MapMarker marker, Point northwest, Point southeast)
- {
- if (northwest.X == 0 && northwest.Y == 0)
- return true;
- var lat = double.Parse(marker.Latitude);
- var lon = double.Parse(marker.Longitude);
- var lat1 = northwest.Y < 0 ? lat <= northwest.Y : lat > northwest.Y;
- var lat2 = southeast.Y < 0 ? lat >= southeast.Y : lat <= southeast.Y;
- var lon1 = northwest.X < 0 ? lon <= northwest.X : lon > northwest.X;
- var lon2 = southeast.X < 0 ? lon >= southeast.X : lon <= southeast.X;
- return lat1 && lat2 && lon1 && lon2;
- }
- private void LoadMarkerList()
- {
- //var timer = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(1) };
- //timer.Tick += ((t,e) =>
- //{
- //timer.IsEnabled = false;
- viewmodel.Refresh();
- var layer = Map.Layers[0] as ImageryLayer;
- var nw = layer.GetLatLonFromPoint(new Point(0F, 0F));
- var se = layer.GetLatLonFromPoint(new Point(Map.ActualWidth, Map.ActualHeight));
- var markers = viewmodel.Markers.Where(marker => ShowAll.IsChecked == true ? true : IsMarkerVisible(marker, nw, se))
- .OrderBy(x => x.Description);
- Markers.ItemsSource = markers.ToArray();
- //});
- //timer.IsEnabled = true;
- }
- private void Map_PreviewMouseUp(object sender, MouseButtonEventArgs e)
- {
- LoadMarkerList();
- }
- private void Markers_MouseDoubleClick(object sender, MouseButtonEventArgs e)
- {
- var marker = Markers.SelectedItem as MapMarker;
- ZoomToMarker(marker);
- }
- private void ZoomToMarker(MapMarker marker)
- {
- if (double.Parse(marker.Latitude) != 0.0F && double.Parse(marker.Longitude) != 0.0F)
- {
- var layer = Map.Layers[0] as ImageryLayer;
- layer.Center = new Point(double.Parse(marker.Latitude), double.Parse(marker.Longitude));
- Map.ZoomLevel = ZOOMEDIN;
- }
- }
- private void Markers_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- var marker = Markers.SelectedItem as MapMarker;
- viewmodel.Selected = Markers.SelectedItem as MapMarker;
- if (marker != null)
- {
- Description.Text = marker.Description;
- LastUpdate.Text = string.Format("{0:dd MMM yy hh:mm:ss}", marker.Updated);
- Device.Text = marker.UpdatedBy;
- History.Visibility = string.IsNullOrWhiteSpace(marker.DeviceID) ? Visibility.Collapsed : Visibility.Visible;
- LoadDeviceHistory(marker.DeviceID);
- }
- else
- {
- Description.Text = "";
- LastUpdate.Text = "";
- Device.Text = "";
- History.Visibility = Visibility.Collapsed;
- }
- }
- private void Viewstyle_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- var layer = typeof(ImageryLayer);
- var type = LayerType.OSM;
- var style = BingMapStyle.Road;
- if (Viewstyle.SelectedItem == BingStandardViewStyle)
- {
- layer = typeof(ImageryLayer);
- type = LayerType.Bing;
- style = BingMapStyle.Road;
- }
- else if (Viewstyle.SelectedItem == BingTerrainViewStyle)
- {
- layer = typeof(ImageryLayer);
- type = LayerType.Bing;
- style = BingMapStyle.Aerial;
- }
- else if (Viewstyle.SelectedItem == BingTerrainLabelsViewStyle)
- {
- layer = typeof(ImageryLayer);
- type = LayerType.Bing;
- style = BingMapStyle.AerialWithLabels;
- }
- else if (Viewstyle.SelectedItem == GoogleMapsViewStyle)
- {
- layer = typeof(ImageryLayerExt);
- }
- else
- {
- layer = typeof(ImageryLayer);
- type = LayerType.OSM;
- }
- var cur = Map.Layers[0] as ImageryLayer;
- if (!cur.GetType().Equals(layer))
- {
- var center = cur.Center;
- var radius = cur.Radius;
- var template = cur.MarkerTemplate;
- Map.Layers.Clear();
- if (layer == typeof(ImageryLayer))
- Map.Layers.Add(new ImageryLayer());
- else if (layer == typeof(ImageryLayerExt))
- Map.Layers.Add(new ImageryLayerExt());
- cur = Map.Layers[0] as ImageryLayer;
- cur.Center = center;
- cur.Radius = radius;
- cur.MarkerTemplate = template;
- cur.Markers = viewmodel.Markers;
- cur.BingMapKey = "5kn7uPPBBGKdmWYiwHJL~LjUY7IIgFzGdXpkpWT7Fsw~AmlksNuYOBHNQ3cOa61Nz2ghvK5EbrBZ3aQqvTS4OjcPxTBGNGsj2hKc058CgtgJ";
- }
- if (cur.LayerType != type)
- cur.LayerType = type;
- if (cur.BingMapStyle != style)
- cur.BingMapStyle = style;
- }
- private void IncludeVisibleMarkers_Checked(object sender, RoutedEventArgs e)
- {
- LoadMarkerList();
- }
- private void ResetView_Click(object sender, RoutedEventArgs e)
- {
- ResetZoom();
- }
- private void History_MouseDoubleClick(object sender, MouseButtonEventArgs e)
- {
- var history = History.SelectedItem as GPSHistory;
- if (history == null)
- return;
- if (history.Latitude != 0.0F && history.Longitude != 0.0F)
- {
- var layer = Map.Layers[0] as ImageryLayer;
- layer.Center = new Point(history.Latitude, history.Longitude);
- Map.ZoomLevel = ZOOMEDIN;
- }
- }
- private void Zoom_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- if (Map == null)
- return;
- Map.ZoomLevel = (int)e.NewValue;
- LoadMarkerList();
- }
- private class DbMarker
- {
- public DbMarker(string code, string description, DateTime timestamp, double latitude, double longitude, string updatedby, string deviceid)
- {
- Code = code;
- Description = description;
- TimeStamp = timestamp;
- Latitude = latitude;
- Longitude = longitude;
- UpdatedBy = updatedby;
- DeviceID = deviceid;
- }
- public string Code { get; }
- public string Description { get; }
- public DateTime TimeStamp { get; }
- public double Latitude { get; }
- public double Longitude { get; }
- public string UpdatedBy { get; }
- public string DeviceID { get; }
- }
- }
- }
|