Location now immutable

This commit is contained in:
ClemensFischer 2024-04-11 13:21:38 +02:00
parent 52d990a89c
commit dc9932a0cf
5 changed files with 18 additions and 32 deletions

View file

@ -10,7 +10,9 @@ namespace MapControl
/// <summary> /// <summary>
/// A geographic location with latitude and longitude values in degrees. /// A geographic location with latitude and longitude values in degrees.
/// </summary> /// </summary>
#if !UWP #if WINUI || UWP
[Windows.Foundation.Metadata.CreateFromString(MethodName = "MapControl.Location.Parse")]
#else
[System.ComponentModel.TypeConverter(typeof(LocationConverter))] [System.ComponentModel.TypeConverter(typeof(LocationConverter))]
#endif #endif
public class Location : IEquatable<Location> public class Location : IEquatable<Location>
@ -21,19 +23,12 @@ namespace MapControl
public Location(double latitude, double longitude) public Location(double latitude, double longitude)
{ {
Latitude = latitude; Latitude = Math.Min(Math.Max(latitude, -90d), 90d);
Longitude = longitude; Longitude = longitude;
} }
private double latitude; public double Latitude { get; }
public double Longitude { get; }
public double Latitude
{
get => latitude;
set => latitude = Math.Min(Math.Max(value, -90d), 90d);
}
public double Longitude { get; set; }
public bool Equals(Location location) public bool Equals(Location location)
{ {

View file

@ -774,14 +774,17 @@ namespace MapControl
if (center != null) if (center != null)
{ {
center.Longitude = Location.NormalizeLongitude(center.Longitude); var centerLatitude = center.Latitude;
var centerLongitude = Location.NormalizeLongitude(center.Longitude);
if (center.Latitude < -maxLatitude || center.Latitude > maxLatitude) if (centerLatitude < -maxLatitude || centerLatitude > maxLatitude)
{ {
center.Latitude = Math.Min(Math.Max(center.Latitude, -maxLatitude), maxLatitude); centerLatitude = Math.Min(Math.Max(centerLatitude, -maxLatitude), maxLatitude);
resetTransformCenter = true; resetTransformCenter = true;
} }
center = new Location(centerLatitude, centerLongitude);
SetValueInternal(CenterProperty, center); SetValueInternal(CenterProperty, center);
if (centerAnimation == null) if (centerAnimation == null)

View file

@ -232,8 +232,8 @@ namespace MapControl
if (location != null) if (location != null)
{ {
location.Longitude = parentMap.ConstrainedLongitude(location.Longitude); var pos = parentMap.LocationToView(
var pos = parentMap.LocationToView(location); new Location(location.Latitude, parentMap.ConstrainedLongitude(location.Longitude)));
if (pos.HasValue) if (pos.HasValue)
{ {

View file

@ -97,14 +97,12 @@
</Grid.DataContext> </Grid.DataContext>
<map:Map x:Name="map" ManipulationMode="All" <map:Map x:Name="map" ManipulationMode="All"
Center="53.5,8.2"
MinZoomLevel="2" MaxZoomLevel="21" ZoomLevel="11" MinZoomLevel="2" MaxZoomLevel="21" ZoomLevel="11"
PointerPressed="MapPointerPressed" PointerPressed="MapPointerPressed"
PointerReleased="MapPointerReleased" PointerReleased="MapPointerReleased"
PointerMoved="MapPointerMoved" PointerMoved="MapPointerMoved"
PointerExited="MapPointerExited"> PointerExited="MapPointerExited">
<map:Map.Center>
<map:Location Latitude="53.5" Longitude="8.2"/>
</map:Map.Center>
<map:MapPolyline x:Name="measurementLine" Visibility="Collapsed" <map:MapPolyline x:Name="measurementLine" Visibility="Collapsed"
Stroke="{Binding Foreground, ElementName=map}" Stroke="{Binding Foreground, ElementName=map}"
@ -122,11 +120,7 @@
ItemContainerStyle="{StaticResource PushpinItemStyle}" ItemContainerStyle="{StaticResource PushpinItemStyle}"
LocationMemberPath="Location"/> LocationMemberPath="Location"/>
<map:Pushpin AutoCollapse="True" Content="N 53°30' E 8°12'"> <map:Pushpin Location="53.5,8.2" AutoCollapse="True" Content="N 53°30' E 8°12'"/>
<map:Pushpin.Location>
<map:Location Latitude="53.5" Longitude="8.2"/>
</map:Pushpin.Location>
</map:Pushpin>
</map:Map> </map:Map>
<Border HorizontalAlignment="Center" VerticalAlignment="Top" Margin="4" IsHitTestVisible="False"> <Border HorizontalAlignment="Center" VerticalAlignment="Top" Margin="4" IsHitTestVisible="False">

View file

@ -112,14 +112,12 @@
</Grid.DataContext> </Grid.DataContext>
<map:Map x:Name="map" ManipulationMode="All" <map:Map x:Name="map" ManipulationMode="All"
Center="53.5,8.2"
MinZoomLevel="2" MaxZoomLevel="21" ZoomLevel="11" MinZoomLevel="2" MaxZoomLevel="21" ZoomLevel="11"
PointerPressed="MapPointerPressed" PointerPressed="MapPointerPressed"
PointerReleased="MapPointerReleased" PointerReleased="MapPointerReleased"
PointerMoved="MapPointerMoved" PointerMoved="MapPointerMoved"
PointerExited="MapPointerExited"> PointerExited="MapPointerExited">
<map:Map.Center>
<map:Location Latitude="53.5" Longitude="8.2"/>
</map:Map.Center>
<map:MapPolyline x:Name="measurementLine" Visibility="Collapsed" <map:MapPolyline x:Name="measurementLine" Visibility="Collapsed"
Stroke="{Binding Foreground, ElementName=map}" Stroke="{Binding Foreground, ElementName=map}"
@ -137,11 +135,7 @@
ItemContainerStyle="{StaticResource PushpinItemStyle}" ItemContainerStyle="{StaticResource PushpinItemStyle}"
LocationMemberPath="Location"/> LocationMemberPath="Location"/>
<map:Pushpin AutoCollapse="True" Content="N 53°30' E 8°12'"> <map:Pushpin Location="53.5,8.2" AutoCollapse="True" Content="N 53°30' E 8°12'"/>
<map:Pushpin.Location>
<map:Location Latitude="53.5" Longitude="8.2"/>
</map:Pushpin.Location>
</map:Pushpin>
</map:Map> </map:Map>
<Border HorizontalAlignment="Center" VerticalAlignment="Top" Margin="4" IsHitTestVisible="False"> <Border HorizontalAlignment="Center" VerticalAlignment="Top" Margin="4" IsHitTestVisible="False">