summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Maps/Distance.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Maps/Distance.cs')
-rw-r--r--Xamarin.Forms.Maps/Distance.cs78
1 files changed, 78 insertions, 0 deletions
diff --git a/Xamarin.Forms.Maps/Distance.cs b/Xamarin.Forms.Maps/Distance.cs
new file mode 100644
index 00000000..7125f086
--- /dev/null
+++ b/Xamarin.Forms.Maps/Distance.cs
@@ -0,0 +1,78 @@
+´╗┐using System.Diagnostics;
+
+namespace Xamarin.Forms.Maps
+{
+ public struct Distance
+ {
+ const double MetersPerMile = 1609.344;
+ const double MetersPerKilometer = 1000.0;
+
+ public Distance(double meters)
+ {
+ Meters = meters;
+ }
+
+ public double Meters { get; }
+
+ public double Miles => Meters / MetersPerMile;
+
+ public double Kilometers => Meters / MetersPerKilometer;
+
+ public static Distance FromMiles(double miles)
+ {
+ if (miles < 0)
+ {
+ Debug.WriteLine("Negative values for distance not supported");
+ miles = 0;
+ }
+ return new Distance(miles * MetersPerMile);
+ }
+
+ public static Distance FromMeters(double meters)
+ {
+ if (meters < 0)
+ {
+ Debug.WriteLine("Negative values for distance not supported");
+ meters = 0;
+ }
+ return new Distance(meters);
+ }
+
+ public static Distance FromKilometers(double kilometers)
+ {
+ if (kilometers < 0)
+ {
+ Debug.WriteLine("Negative values for distance not supported");
+ kilometers = 0;
+ }
+ return new Distance(kilometers * MetersPerKilometer);
+ }
+
+ public bool Equals(Distance other)
+ {
+ return Meters.Equals(other.Meters);
+ }
+
+ public override bool Equals(object obj)
+ {
+ if (ReferenceEquals(null, obj))
+ return false;
+ return obj is Distance && Equals((Distance)obj);
+ }
+
+ public override int GetHashCode()
+ {
+ return Meters.GetHashCode();
+ }
+
+ public static bool operator ==(Distance left, Distance right)
+ {
+ return left.Equals(right);
+ }
+
+ public static bool operator !=(Distance left, Distance right)
+ {
+ return !left.Equals(right);
+ }
+ }
+} \ No newline at end of file