diff --git a/MapControl/Shared/AzimuthalProjection.cs b/MapControl/Shared/AzimuthalProjection.cs
index c24bd069..6f4c9412 100644
--- a/MapControl/Shared/AzimuthalProjection.cs
+++ b/MapControl/Shared/AzimuthalProjection.cs
@@ -8,6 +8,7 @@ namespace MapControl
public abstract class AzimuthalProjection : MapProjection
{
protected AzimuthalProjection()
+ : base(true)
{
Type = MapProjectionType.Azimuthal;
}
diff --git a/MapControl/Shared/MapBase.cs b/MapControl/Shared/MapBase.cs
index 58e4ef42..3fcec022 100644
--- a/MapControl/Shared/MapBase.cs
+++ b/MapControl/Shared/MapBase.cs
@@ -87,8 +87,9 @@ namespace MapControl
}
///
- /// Gets or sets an optional center (reference point) for azimuthal projections.
- /// If ProjectionCenter is null, the Center property value will be used instead.
+ /// Gets or sets a projection center e.g. for azimuthal projections.
+ /// If ProjectionCenter is null, the value of the Center property is used as projection center.
+ /// This behavior is overridden when the Center property of a MapProjection is set explicitly.
///
public Location ProjectionCenter
{
@@ -456,7 +457,7 @@ namespace MapControl
var viewScale = ZoomLevelToScale(ZoomLevel);
var projection = MapProjection;
- projection.Center = ProjectionCenter ?? Center;
+ projection.SetCenter(ProjectionCenter ?? Center);
var mapCenter = projection.LocationToMap(transformCenter ?? Center);
@@ -496,7 +497,7 @@ namespace MapControl
ResetTransformCenter();
- projection.Center = ProjectionCenter ?? Center;
+ projection.SetCenter(ProjectionCenter ?? Center);
mapCenter = projection.LocationToMap(center);
diff --git a/MapControl/Shared/MapProjection.cs b/MapControl/Shared/MapProjection.cs
index 3e9f53eb..189901d8 100644
--- a/MapControl/Shared/MapProjection.cs
+++ b/MapControl/Shared/MapProjection.cs
@@ -25,7 +25,7 @@ namespace MapControl
#else
[System.ComponentModel.TypeConverter(typeof(MapProjectionConverter))]
#endif
- public abstract class MapProjection
+ public abstract class MapProjection(bool hasCenter = false)
{
public const double Wgs84EquatorialRadius = 6378137d;
public const double Wgs84Flattening = 1d / 298.257223563;
@@ -60,19 +60,41 @@ namespace MapControl
///
public string CrsId { get; protected set; } = "";
+ private Location center;
+ private bool updateCenter = hasCenter;
+
///
- /// Gets or sets an optional projection center.
+ /// Gets or sets an optional projection center. If the property is set to a non-null value,
+ /// it overrides the projection center set by MapBase.Center or MapBase.ProjectionCenter.
///
public Location Center
{
- get => field ??= new Location();
- protected internal set
+ get => center ??= new Location();
+ set
{
+ updateCenter = true;
+
+ if (value != null)
+ {
+ SetCenter(value);
+ updateCenter = false;
+ }
+ }
+ }
+
+ ///
+ /// Called by MapBase.UpdateTransform().
+ ///
+ internal void SetCenter(Location value)
+ {
+ if (updateCenter)
+ {
+ var latitude = value.Latitude;
var longitude = Location.NormalizeLongitude(value.Longitude);
- if (field == null || !field.Equals(value.Latitude, longitude))
+ if (center == null || !center.Equals(latitude, longitude))
{
- field = new Location(value.Latitude, longitude);
+ center = new Location(latitude, longitude);
CenterChanged();
}
}
diff --git a/MapProjections/Shared/ProjNetMapProjection.cs b/MapProjections/Shared/ProjNetMapProjection.cs
index 39ecab94..1518df1f 100644
--- a/MapProjections/Shared/ProjNetMapProjection.cs
+++ b/MapProjections/Shared/ProjNetMapProjection.cs
@@ -14,7 +14,8 @@ namespace MapControl.Projections
///
public class ProjNetMapProjection : MapProjection
{
- protected ProjNetMapProjection()
+ protected ProjNetMapProjection(bool hasCenter = false)
+ : base(hasCenter)
{
}
diff --git a/MapProjections/Shared/Wgs84OrthographicProjection.cs b/MapProjections/Shared/Wgs84OrthographicProjection.cs
index 1a22b650..6d608c81 100644
--- a/MapProjections/Shared/Wgs84OrthographicProjection.cs
+++ b/MapProjections/Shared/Wgs84OrthographicProjection.cs
@@ -13,6 +13,7 @@ namespace MapControl.Projections
public class Wgs84OrthographicProjection : ProjNetMapProjection
{
public Wgs84OrthographicProjection()
+ : base(true)
{
CenterChanged();
}
diff --git a/MapProjections/Shared/Wgs84StereographicProjection.cs b/MapProjections/Shared/Wgs84StereographicProjection.cs
index 54408fcf..821fa195 100644
--- a/MapProjections/Shared/Wgs84StereographicProjection.cs
+++ b/MapProjections/Shared/Wgs84StereographicProjection.cs
@@ -12,6 +12,7 @@ namespace MapControl.Projections
public class Wgs84StereographicProjection : ProjNetMapProjection
{
public Wgs84StereographicProjection()
+ : base(true)
{
CenterChanged();
}