Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GeoMap.cs
1using System.Collections.ObjectModel;
2using Mapsui.Extensions;
3using Mapsui.Layers;
4using Mapsui.Projections;
5using Mapsui.Tiling;
6using Mapsui.UI.Maui;
9
10
11using Map = Mapsui.Map; // for Mapsui.Map
12
14{
15 public partial class GeoMap : ContentView, IDisposable
16 {
17 readonly MapControl mapControl;
18
19 public GeoMap()
20 {
21 this.mapControl = new MapControl();
22 this.Content = this.mapControl;
23
24 // 1) Create underlying Map and add OSM raster tile layer
25 Map map = new Map();
26 map.Layers.Add(OpenStreetMap.CreateTileLayer()); // :contentReference[oaicite:4]{index=4}
27 /*
28 // 2) Register custom style renderer
29 this.mapControl.S
30 this.mapControl.Renderer.StyleRenderers
31 .Add(typeof(DataUriStyle), new DataUriStyleRenderer()); // :contentReference[oaicite:5]{index=5}
32 */
33 this.mapControl.Map = map;
34
35 // 3) React to bindable property changes
36 PropertyChanged += this.OnPropertyChanged;
37 }
38
39 Map Map => this.mapControl.Map!;
40
41 #region CurrentCoordinate
42
43 public static readonly BindableProperty CurrentCoordinateProperty =
44 BindableProperty.Create(
45 nameof(CurrentCoordinate),
46 typeof(GeoPosition),
47 typeof(GeoMap),
48 default(GeoPosition),
49 BindingMode.TwoWay);
50
51 public GeoPosition CurrentCoordinate
52 {
53 get => (GeoPosition)this.GetValue(CurrentCoordinateProperty);
54 set => this.SetValue(CurrentCoordinateProperty, value);
55 }
56
57 void CenterOn(GeoPosition pos)
58 {
59 // Convert lon/lat → spherical mercator MPoint
60 Mapsui.MPoint merc = SphericalMercator.FromLonLat(pos.Longitude, pos.Latitude)
61 .ToMPoint();
62 this.Map.Navigator.CenterOnAndZoomTo(merc, 10, 1000);
63 }
64
65 #endregion
66
67 #region Items
68
69 public static readonly BindableProperty ItemsProperty =
70 BindableProperty.Create(
71 nameof(Items),
72 typeof(ObservableCollection<MapItem>),
73 typeof(GeoMap),
74 new ObservableCollection<MapItem>(),
75 propertyChanged: OnItemsChanged);
76
77 public ObservableCollection<MapItem> Items
78 {
79 get => (ObservableCollection<MapItem>)this.GetValue(ItemsProperty);
80 set => this.SetValue(ItemsProperty, value);
81 }
82
83 static void OnItemsChanged(BindableObject Bindable, object OldVal, object NewVal)
84 {
85 if (Bindable is GeoMap GeoMap)
86 GeoMap.RebuildItemsLayer();
87 }
88
89 void RebuildItemsLayer()
90 {
91 // A) Remove any existing ItemsLayer
92 ILayer? old = this.Map.Layers.FirstOrDefault(l => l.Name == "ItemsLayer");
93 if (old is not null)
94 this.Map.Layers.Remove(old);
95
96 // B) Build new MemoryLayer with a MemoryProvider
97 List<PointFeature> features = this.Items.Select(item =>
98 {
99 // Convert each GeoPosition → MPoint → PointFeature
100 (double x, double y) worldPoint = SphericalMercator
101 .FromLonLat(item.Location.Longitude, item.Location.Latitude);
102 PointFeature pf = new PointFeature(worldPoint.ToMPoint());
103 // Tag your data URI for renderer lookup
104 pf["DataUri"] = item.Uri.ToString();
105 // Attach placeholder style
106 pf.Styles.Add(new DataUriStyle());
107 return pf;
108 }).ToList();
109
110 MemoryLayer layer = new MemoryLayer
111 {
112 Name = "ItemsLayer",
113 Features = features
114 };
115 this.Map.Layers.Add(layer);
116 }
117
118 #endregion
119
120 #region Locks
121
122 public static readonly BindableProperty LockRotationProperty =
123 BindableProperty.Create(nameof(LockRotation), typeof(bool), typeof(GeoMap), false);
124 public static readonly BindableProperty LockPanProperty =
125 BindableProperty.Create(nameof(LockPan), typeof(bool), typeof(GeoMap), false);
126 public static readonly BindableProperty LockZoomProperty =
127 BindableProperty.Create(nameof(LockZoom), typeof(bool), typeof(GeoMap), false);
128
129 public bool LockRotation { get => (bool)this.GetValue(LockRotationProperty); set => this.SetValue(LockRotationProperty, value); }
130 public bool LockPan { get => (bool)this.GetValue(LockPanProperty); set => this.SetValue(LockPanProperty, value); }
131 public bool LockZoom { get => (bool)this.GetValue(LockZoomProperty); set => this.SetValue(LockZoomProperty, value); }
132
133 void ApplyLocks()
134 {
135 this.Map.Navigator.RotationLock = this.LockRotation;
136 this.Map.Navigator.PanLock = this.LockPan;
137 this.Map.Navigator.ZoomLock = this.LockZoom; // :contentReference[oaicite:11]{index=11}
138 }
139
140 #endregion
141
142 void OnPropertyChanged(object? Sender, System.ComponentModel.PropertyChangedEventArgs Event)
143 {
144 switch (Event.PropertyName)
145 {
146 case nameof(this.CurrentCoordinate):
147 this.CenterOn(this.CurrentCoordinate);
148 break;
149 case nameof(this.Items):
150 this.RebuildItemsLayer();
151 break;
152 case nameof(this.LockRotation):
153 case nameof(this.LockPan):
154 case nameof(this.LockZoom):
155 this.ApplyLocks();
156 break;
157 }
158 }
159
160 private bool disposed;
161
165 public void Dispose()
166 {
167 this.Dispose(true);
168 GC.SuppressFinalize(this);
169 }
170
175 protected virtual void Dispose(bool disposing)
176 {
177 if (this.disposed)
178 return;
179
180 if (disposing)
181 {
182 // Dispose managed resources
183 PropertyChanged -= this.OnPropertyChanged;
184 this.mapControl.Dispose();
185 }
186
187 // Clean up unmanaged resources here if any
188
189 this.disposed = true;
190 }
191
192 ~GeoMap()
193 {
194 this.Dispose(false);
195 }
196 }
197
201 public struct MapItem
202 {
203 public GeoPosition Location { get; set; }
204 public Uri Uri { get; set; }
205 }
206}
Marks a feature that should be rendered by DataUriStyleRenderer.
Definition: DataUriStyle.cs:9
virtual void Dispose(bool disposing)
Dispose pattern implementation.
Definition: GeoMap.cs:175
void Dispose()
Dispose the underlying MapControl when the GeoMap is disposed.
Definition: GeoMap.cs:165
Contains information about a position in a geo-spatial coordinate system.
Definition: GeoPosition.cs:17
double Longitude
Longitude in degrees.
Definition: GeoPosition.cs:85
double Latitude
Latitude in degrees.
Definition: GeoPosition.cs:70
Holds the position and a Uri to arbitrary data to render later.
Definition: GeoMap.cs:202