public static double GetDistance(Position p1, Position p2) { #if __ANDROID__ var location1 = new global::Android.Locations.Location("locationA"); var location2 = new global::Android.Locations.Location("locationB"); location1.Latitude = p1.Latitude; location1.Longitude = p1.Longitude; location2.Latitude = p2.Latitude; location2.Longitude = p2.Longitude; return(location1.DistanceTo(location2) / 1000.0); #elif __IOS__ var l1 = new CoreLocation.CLLocation(p1.Latitude, p1.Longitude); var l2 = new CoreLocation.CLLocation(p2.Latitude, p2.Longitude); return(l1.DistanceFrom(l2) / 1000.0); #else throw new NotImplementedException("Platform not implemented."); #endif }
static double CalculateDistanceInMeters(Position p1, Position p2) { #if __ANDROID__ var location1 = new global::Android.Locations.Location("locationA"); var location2 = new global::Android.Locations.Location("locationB"); location1.Latitude = p1.Latitude; location1.Longitude = p1.Longitude; location2.Latitude = p2.Latitude; location2.Longitude = p2.Longitude; return(location1.DistanceTo(location2)); #elif __IOS__ var l1 = new CoreLocation.CLLocation(p1.Latitude, p1.Longitude); var l2 = new CoreLocation.CLLocation(p2.Latitude, p2.Longitude); return(l1.DistanceFrom(l2)); #else var d1 = p1.Latitude * (Math.PI / 180.0); var num1 = p1.Longitude * (Math.PI / 180.0); var d2 = p2.Latitude * (Math.PI / 180.0); var num2 = p2.Longitude * (Math.PI / 180.0) - num1; var d3 = Math.Pow(Math.Sin((d2 - d1) / 2.0), 2.0) + Math.Cos(d1) * Math.Cos(d2) * Math.Pow(Math.Sin(num2 / 2.0), 2.0); return(6376500.0 * (2.0 * Math.Atan2(Math.Sqrt(d3), Math.Sqrt(1.0 - d3)))); #endif }