|
|
@@ -2,6 +2,7 @@ using System;
|
|
|
using System.Collections.ObjectModel;
|
|
|
using System.Linq;
|
|
|
using System.Net.Http;
|
|
|
+using System.Runtime.InteropServices;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Input;
|
|
|
@@ -10,11 +11,13 @@ using Geocoding;
|
|
|
using Geocoding.Google;
|
|
|
using InABox.Clients;
|
|
|
using InABox.Core;
|
|
|
+using InABox.WPF;
|
|
|
using Nominatim.API.Geocoders;
|
|
|
using Nominatim.API.Models;
|
|
|
using Nominatim.API.Web;
|
|
|
using Syncfusion.UI.Xaml.Maps;
|
|
|
using Address = InABox.Core.Address;
|
|
|
+using OnAfterLoad = InABox.DynamicGrid.OnAfterLoad;
|
|
|
using Point = System.Windows.Point;
|
|
|
|
|
|
namespace InABox.Wpf.DynamicGrid;
|
|
|
@@ -25,31 +28,37 @@ public partial class GeofenceEditor : Window
|
|
|
|
|
|
GeoFenceDefinition _definition = null;
|
|
|
|
|
|
- private ImageryLayer ImageryLayer;
|
|
|
- public MapPolygon Polygon;
|
|
|
+ //private ImageryLayer ImageryLayer;
|
|
|
+ //private MapPolygon Polygon;
|
|
|
+ //private SubShapeFileLayer ShapeLayer;
|
|
|
+
|
|
|
+ private bool _canEdit = false;
|
|
|
|
|
|
public GeofenceEditor(Address address, bool canEdit)
|
|
|
{
|
|
|
+ _canEdit = canEdit;
|
|
|
Address = address;
|
|
|
_definition = Serialization.Deserialize<GeoFenceDefinition>(address.Geofence) ?? new GeoFenceDefinition();
|
|
|
|
|
|
InitializeComponent();
|
|
|
-
|
|
|
- ImageryLayer = string.IsNullOrWhiteSpace(CoreUtils.GoogleAPIKey)
|
|
|
- ? new ImageryLayer()
|
|
|
- : new GoogleImageryLayer() { Type = GoogleImageryLayerType.Satellite };
|
|
|
- ImageryLayer.Radius = 10;
|
|
|
|
|
|
- Map.Layers.Add(ImageryLayer);
|
|
|
-
|
|
|
- Polygon = new MapPolygon()
|
|
|
- {
|
|
|
- Fill = new SolidColorBrush(Colors.LightSalmon) { Opacity = 0.5 },
|
|
|
- Stroke = new SolidColorBrush(Colors.Firebrick),
|
|
|
- StrokeThickness = 0.75,
|
|
|
- };
|
|
|
- var layer = new SubShapeFileLayer() { MapElements = new ObservableCollection<MapElement>([Polygon]) };
|
|
|
- ImageryLayer.SubShapeFileLayers = new ObservableCollection<SubShapeFileLayer>([layer]);
|
|
|
+ // ImageryLayer = string.IsNullOrWhiteSpace(CoreUtils.GoogleAPIKey)
|
|
|
+ // ? new ImageryLayer()
|
|
|
+ // : new GoogleImageryLayer() { Type = GoogleImageryLayerType.Satellite };
|
|
|
+ // ImageryLayer.Radius = 10;
|
|
|
+ //
|
|
|
+ // Map.Layers.Add(ImageryLayer);
|
|
|
+ //
|
|
|
+ // Polygon = new MapPolygon()
|
|
|
+ // {
|
|
|
+ // Fill = new SolidColorBrush(Colors.LightSalmon) { Opacity = 0.5 },
|
|
|
+ // Stroke = new SolidColorBrush(Colors.Firebrick),
|
|
|
+ // StrokeThickness = 0.75,
|
|
|
+ // Points = new CoreObservableCollection<Point>()
|
|
|
+ // };
|
|
|
+ // ShapeLayer = new SubShapeFileLayer() { MapElements = new ObservableCollection<MapElement>([Polygon]) };
|
|
|
+ //
|
|
|
+ // ImageryLayer.SubShapeFileLayers = new ObservableCollection<SubShapeFileLayer>([ShapeLayer]);
|
|
|
|
|
|
SetGeometry.Visibility = canEdit ? Visibility.Visible : Visibility.Collapsed;
|
|
|
SetRadius.Visibility = canEdit ? Visibility.Visible : Visibility.Collapsed;
|
|
|
@@ -62,13 +71,20 @@ public partial class GeofenceEditor : Window
|
|
|
|
|
|
Title = $"Geofence Editor ({(string.IsNullOrWhiteSpace(CoreUtils.GoogleAPIKey) ? "OSM" : "Google")})";
|
|
|
|
|
|
- if (canEdit && string.IsNullOrWhiteSpace(Address.Geofence))
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public override void OnApplyTemplate()
|
|
|
+ {
|
|
|
+ base.OnApplyTemplate();
|
|
|
+ if (_canEdit && string.IsNullOrWhiteSpace(Address.Geofence))
|
|
|
Task.Run(CheckAddress);
|
|
|
else
|
|
|
SetupMap();
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
private class HttpClientFactory : IHttpClientFactory
|
|
|
{
|
|
|
private static HttpClient? _client;
|
|
|
@@ -242,8 +258,12 @@ public partial class GeofenceEditor : Window
|
|
|
|
|
|
private void UpdateMap()
|
|
|
{
|
|
|
-
|
|
|
- Polygon.Points = new ObservableCollection<Point>(_definition.Coordinates.Select(p => new Point(p.Latitude, p.Longitude)));
|
|
|
+ // if (Polygon.Points is CoreObservableCollection<Point> _pts)
|
|
|
+ // _pts.ReplaceRange(_definition.Coordinates.Select(p => new Point(p.Latitude, p.Longitude)));
|
|
|
+ Polygon.Points.Clear();
|
|
|
+ foreach (var p in _definition.Coordinates)
|
|
|
+ Polygon.Points.Add(new Point(p.Latitude, p.Longitude));
|
|
|
+ ShapeLayer.Refresh();
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -274,18 +294,34 @@ public partial class GeofenceEditor : Window
|
|
|
private void UpdateButtons(GeoFenceAction action)
|
|
|
{
|
|
|
_action = action;
|
|
|
- SetGeometry.IsEnabled = action == GeoFenceAction.None || action == GeoFenceAction.Polygon;
|
|
|
+
|
|
|
+ CloseEditor.Visibility = action == GeoFenceAction.None
|
|
|
+ ? Visibility.Collapsed
|
|
|
+ : Visibility.Visible;
|
|
|
+
|
|
|
+ SetGeometry.Visibility = action == GeoFenceAction.None || action == GeoFenceAction.Polygon
|
|
|
+ ? Visibility.Visible
|
|
|
+ : Visibility.Collapsed;
|
|
|
+ SetGeometry.IsEnabled = action == GeoFenceAction.None;
|
|
|
SetGeometry.Background = action == GeoFenceAction.Polygon
|
|
|
? Brushes.Yellow
|
|
|
: Brushes.Silver;
|
|
|
- SetRadius.IsEnabled = action == GeoFenceAction.None || action == GeoFenceAction.Square;
|
|
|
+
|
|
|
+ SetRadius.Visibility = action == GeoFenceAction.None || action == GeoFenceAction.Square
|
|
|
+ ? Visibility.Visible
|
|
|
+ : Visibility.Collapsed;
|
|
|
+ SetRadius.IsEnabled = action == GeoFenceAction.None;
|
|
|
SetRadius.Background = action == GeoFenceAction.Square
|
|
|
? Brushes.Yellow
|
|
|
: Brushes.Silver;
|
|
|
RadiusSlider.Visibility = action == GeoFenceAction.Square
|
|
|
? Visibility.Visible
|
|
|
: Visibility.Collapsed;
|
|
|
- SetCoordinates.IsEnabled = action == GeoFenceAction.None || action == GeoFenceAction.Coordinates;
|
|
|
+
|
|
|
+ SetCoordinates.Visibility = action == GeoFenceAction.None || action == GeoFenceAction.Coordinates
|
|
|
+ ? Visibility.Visible
|
|
|
+ : Visibility.Collapsed;
|
|
|
+ SetCoordinates.IsEnabled = action == GeoFenceAction.None;
|
|
|
SetCoordinates.Background = action == GeoFenceAction.Coordinates
|
|
|
? Brushes.Yellow
|
|
|
: Brushes.Silver;
|
|
|
@@ -293,49 +329,23 @@ public partial class GeofenceEditor : Window
|
|
|
|
|
|
private void SetGeometry_OnClick(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
- if (_action == GeoFenceAction.Polygon)
|
|
|
- UpdateButtons(GeoFenceAction.None);
|
|
|
- else
|
|
|
- {
|
|
|
+ //if (_action == GeoFenceAction.Polygon)
|
|
|
+ // UpdateButtons(GeoFenceAction.None);
|
|
|
+ //else
|
|
|
+ //{
|
|
|
UpdateButtons(GeoFenceAction.Polygon);
|
|
|
_definition.Coordinates.Clear();
|
|
|
UpdateMap();
|
|
|
- }
|
|
|
+ //}
|
|
|
}
|
|
|
|
|
|
- private void Map_OnMouseUp(object sender, MouseButtonEventArgs e)
|
|
|
- {
|
|
|
- var point = Mouse.GetPosition(Map);
|
|
|
- var latlon = ImageryLayer.GetLatLonFromPoint(point);
|
|
|
- var geopoint = new GeoPoint(latlon.Y, latlon.X);
|
|
|
-
|
|
|
- if (_action == GeoFenceAction.Polygon)
|
|
|
- {
|
|
|
- if (!_definition.Coordinates.Any())
|
|
|
- _definition.Coordinates.Add(geopoint.Copy());
|
|
|
- _definition.Coordinates.Insert(_definition.Coordinates.Count - 1, geopoint.Copy());
|
|
|
- Address.Geofence = Serialization.Serialize(_definition);
|
|
|
- UpdateMap();
|
|
|
- e.Handled = true;
|
|
|
- }
|
|
|
- else if (_action == GeoFenceAction.Coordinates)
|
|
|
- {
|
|
|
- Address.Location.Latitude = geopoint.Latitude;
|
|
|
- Address.Location.Longitude = geopoint.Longitude;
|
|
|
- CenterMap();
|
|
|
- UpdateMap();
|
|
|
- e.Handled = true;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
private void SetRadius_OnClick(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
- if (_action == GeoFenceAction.Square)
|
|
|
- UpdateButtons(GeoFenceAction.None);
|
|
|
- else
|
|
|
- {
|
|
|
+ //if (_action == GeoFenceAction.Square)
|
|
|
+ // UpdateButtons(GeoFenceAction.None);
|
|
|
+ //else
|
|
|
+ //{
|
|
|
UpdateButtons(GeoFenceAction.Square);
|
|
|
|
|
|
RadiusSlider.ValueChanged -= RadiusSliderChanged;
|
|
|
@@ -344,7 +354,7 @@ public partial class GeofenceEditor : Window
|
|
|
RadiusSlider.ValueChanged += RadiusSliderChanged;
|
|
|
RecalculateSquareFence();
|
|
|
UpdateMap();
|
|
|
- }
|
|
|
+ //}
|
|
|
}
|
|
|
|
|
|
private void SearchAddress_Click(object sender, RoutedEventArgs e)
|
|
|
@@ -354,9 +364,48 @@ public partial class GeofenceEditor : Window
|
|
|
|
|
|
private void SetCoordinates_OnClick(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
- if (_action == GeoFenceAction.Coordinates)
|
|
|
- UpdateButtons(GeoFenceAction.None);
|
|
|
- else
|
|
|
+ //if (_action == GeoFenceAction.Coordinates)
|
|
|
+ // UpdateButtons(GeoFenceAction.None);
|
|
|
+ //else
|
|
|
+ {
|
|
|
UpdateButtons(GeoFenceAction.Coordinates);
|
|
|
+ _definition.Coordinates.Clear();
|
|
|
+ UpdateMap();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void CloseEditor_OnClick(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ UpdateButtons(GeoFenceAction.None);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Map_OnPreviewMouseUp(object sender, MouseButtonEventArgs e)
|
|
|
+ {
|
|
|
+ var point = Mouse.GetPosition(Map);
|
|
|
+ System.Console.WriteLine($@"Preview: {point.X}, {point.Y}");
|
|
|
+ Dispatcher.BeginInvoke(() =>
|
|
|
+ {
|
|
|
+ var latlon = ImageryLayer.GetLatLonFromPoint(point);
|
|
|
+ var geopoint = new GeoPoint(latlon.Y, latlon.X);
|
|
|
+
|
|
|
+ if (_action == GeoFenceAction.Polygon)
|
|
|
+ {
|
|
|
+ if (!_definition.Coordinates.Any())
|
|
|
+ _definition.Coordinates.Add(geopoint.Copy());
|
|
|
+ _definition.Coordinates.Insert(_definition.Coordinates.Count - 1, geopoint.Copy());
|
|
|
+ Address.Geofence = Serialization.Serialize(_definition);
|
|
|
+ UpdateMap();
|
|
|
+ e.Handled = true;
|
|
|
+ }
|
|
|
+ else if (_action == GeoFenceAction.Coordinates)
|
|
|
+ {
|
|
|
+ Address.Location.Latitude = geopoint.Latitude;
|
|
|
+ Address.Location.Longitude = geopoint.Longitude;
|
|
|
+ CenterMap();
|
|
|
+ UpdateMap();
|
|
|
+ e.Handled = true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
}
|
|
|
}
|