It is not clear what programming subject(s) you are struggling with because you did not share any code.
Note, the GFG.Distance method is updated to public and place in a service folder within the MVC application.
public class GFG
{
static double toRadians(
double angleIn10thofaDegree)
{
// Angle in 10th
// of a degree
return (angleIn10thofaDegree *
Math.PI) / 180;
}
public static double Distance(double lat1,
double lat2,
double lon1,
double lon2)
{
// The math module contains
// a function named toRadians
// which converts from degrees
// to radians.
lon1 = toRadians(lon1);
lon2 = toRadians(lon2);
lat1 = toRadians(lat1);
lat2 = toRadians(lat2);
// Haversine formula
double dlon = lon2 - lon1;
double dlat = lat2 - lat1;
double a = Math.Pow(Math.Sin(dlat / 2), 2) +
Math.Cos(lat1) * Math.Cos(lat2) *
Math.Pow(Math.Sin(dlon / 2), 2);
double c = 2 * Math.Asin(Math.Sqrt(a));
// Radius of earth in
// kilometers. Use 3956
// for miles
double r = 6371;
// calculate the result
return (c * r);
}
}
A model to hold the user's inputs.
public class DistanceModel
{
public double Fromlongitude { get; set; }
public double FromLatitude { get; set; }
public double Tolongitude { get; set; }
public double ToLatitude { get; set; }
}
The get action populates inputs with test data. The post action reads the input data and calculates the distance.
public class DistanceController : Controller
{
[HttpGet]
public IActionResult Index()
{
//Populate test data
DistanceModel model = new DistanceModel()
{
FromLatitude = 53.32055555555556,
ToLatitude = 53.31861111111111,
Fromlongitude = 1.7297222222222221,
Tolongitude = 1.6997222222222223,
};
return View(model);
}
[HttpPost]
public IActionResult Index(DistanceModel model)
{
ViewData["CalculatedDistance"] = GFG.Distance(model.FromLatitude,
model.ToLatitude,
model.Fromlongitude,
model.Tolongitude);
return View(model);
}
}
The view that displays the inputs and the results if results exist.
@model MvcDemo.Models.DistanceModel
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<h4>DistanceModel</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Index">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Fromlongitude" class="control-label"></label>
<input asp-for="Fromlongitude" class="form-control" />
<span asp-validation-for="Fromlongitude" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FromLatitude" class="control-label"></label>
<input asp-for="FromLatitude" class="form-control" />
<span asp-validation-for="FromLatitude" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Tolongitude" class="control-label"></label>
<input asp-for="Tolongitude" class="form-control" />
<span asp-validation-for="Tolongitude" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ToLatitude" class="control-label"></label>
<input asp-for="ToLatitude" class="form-control" />
<span asp-validation-for="ToLatitude" class="text-danger"></span>
</div>
@if(ViewData["CalculatedDistance"] != null)
{
<div>
Distance = @ViewData["CalculatedDistance"] km
</div>
}
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>