1using System.Collections.ObjectModel;
2using Mapsui.Extensions;
4using Mapsui.Projections;
11using Map = Mapsui.Map;
15 public partial class GeoMap : ContentView, IDisposable
17 readonly MapControl mapControl;
21 this.mapControl =
new MapControl();
22 this.Content = this.mapControl;
26 map.Layers.Add(OpenStreetMap.CreateTileLayer());
33 this.mapControl.Map = map;
36 PropertyChanged += this.OnPropertyChanged;
39 Map Map => this.mapControl.Map!;
41 #region CurrentCoordinate
43 public static readonly BindableProperty CurrentCoordinateProperty =
44 BindableProperty.Create(
45 nameof(CurrentCoordinate),
53 get => (
GeoPosition)this.GetValue(CurrentCoordinateProperty);
54 set => this.SetValue(CurrentCoordinateProperty, value);
62 this.Map.Navigator.CenterOnAndZoomTo(merc, 10, 1000);
69 public static readonly BindableProperty ItemsProperty =
70 BindableProperty.Create(
72 typeof(ObservableCollection<MapItem>),
74 new ObservableCollection<MapItem>(),
75 propertyChanged: OnItemsChanged);
77 public ObservableCollection<MapItem> Items
79 get => (ObservableCollection<MapItem>)this.GetValue(ItemsProperty);
80 set => this.SetValue(ItemsProperty, value);
83 static void OnItemsChanged(BindableObject Bindable,
object OldVal,
object NewVal)
86 GeoMap.RebuildItemsLayer();
89 void RebuildItemsLayer()
92 ILayer? old = this.Map.Layers.FirstOrDefault(l => l.Name ==
"ItemsLayer");
94 this.Map.Layers.Remove(old);
97 List<PointFeature> features = this.Items.Select(item =>
100 (
double x,
double y) worldPoint = SphericalMercator
101 .FromLonLat(item.Location.Longitude, item.Location.Latitude);
102 PointFeature pf =
new PointFeature(worldPoint.ToMPoint());
104 pf[
"DataUri"] = item.Uri.ToString();
110 MemoryLayer layer =
new MemoryLayer
115 this.Map.Layers.Add(layer);
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);
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); }
135 this.Map.Navigator.RotationLock = this.LockRotation;
136 this.Map.Navigator.PanLock = this.LockPan;
137 this.Map.Navigator.ZoomLock = this.LockZoom;
142 void OnPropertyChanged(
object? Sender,
System.ComponentModel.PropertyChangedEventArgs Event)
144 switch (Event.PropertyName)
146 case nameof(this.CurrentCoordinate):
147 this.CenterOn(this.CurrentCoordinate);
149 case nameof(this.Items):
150 this.RebuildItemsLayer();
152 case nameof(this.LockRotation):
153 case nameof(this.LockPan):
154 case nameof(this.LockZoom):
160 private bool disposed;
168 GC.SuppressFinalize(
this);
175 protected virtual void Dispose(
bool disposing)
183 PropertyChanged -= this.OnPropertyChanged;
184 this.mapControl.Dispose();
189 this.disposed =
true;
204 public Uri Uri {
get;
set; }
Marks a feature that should be rendered by DataUriStyleRenderer.
virtual void Dispose(bool disposing)
Dispose pattern implementation.
void Dispose()
Dispose the underlying MapControl when the GeoMap is disposed.
Contains information about a position in a geo-spatial coordinate system.
double Longitude
Longitude in degrees.
double Latitude
Latitude in degrees.
Holds the position and a Uri to arbitrary data to render later.