Calculate distance between two coordinates lat1,lon1 to lat2,lon2 using mvc core

Anonymous
2023-08-13T07:47:56.36+00:00

Here is code to calculate distance from two coordinates (lat1,lon1

to lat2,lon2)

I want to code a mvc core program so that user inputs lat1,lon1 and lat2,lon2 and gets distance as output , please suggest

using System;

class GFG
{
    static double toRadians(
           double angleIn10thofaDegree)
    {
        // Angle in 10th
        // of a degree
        return (angleIn10thofaDegree *
                       Math.PI) / 180;
    }
    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);
    }

    // Driver code
    static void Main()
    {
        double lat1 = 53.32055555555556;
        double lat2 = 53.31861111111111;
        double lon1 = -1.7297222222222221;
        double lon2 = -1.6997222222222223;
        Console.WriteLine(distance(lat1, lat2,
                          lon1, lon2) + " K.M");
    }
}
Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 1,510 Reputation points
    2023-08-13T11:19:23.7433333+00:00

    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>
    
    
    
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2023-08-13T14:41:47.9433333+00:00

    Thanks @
    AgaveJoe
    ,

    I arrived at below code

    using Microsoft.AspNetCore.Mvc;
    
    namespace LatLongAddress.Controllers
    {
        public class CalcController : Controller
        {
            public IActionResult Index()
            {
                return View();
            }
    
    
            [HttpPost]
            public IActionResult Index(double  Longitude1, double Longitude2 , double Latitude1, double Latitude2)
            {
    
                double lat1 = Latitude1;
                double lat2 = Latitude2;
                double lon1 =   Longitude1;
                double lon2 = Longitude2;
    
            double  disttemp = Math.Round(distance(lat1, lat2, lon1, lon2),1);
    
                string dist = disttemp + "KM";
                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);
                }
    
    
    
                static double toRadians(
               double angleIn10thofaDegree)
                {
                    // Angle in 10th
                    // of a degree
                    return (angleIn10thofaDegree *
                                   Math.PI) / 180;
                }
                ViewData["distance"] = dist;  
                return View();
            }
    
        }
    }
    
    
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        <form method="post" enctype="multipart/form-data" asp-controller="Calc" asp-action="Index">
            <table>
                <tr>
                    <td>Longitude1: </td>
                    <td><input type="number" id="Longitude1" name="Longitude1" step="any" /></td>
                </tr>
                <tr>
                    <td>Longitude2: </td>
                    <td><input type="number" id="Longitude2" name="Longitude2" step="any" /></td>
                </tr>
                <tr>
                    <td>Latitude1: </td>
                    <td><input type="number" id="Latitude1" name="Latitude1" step="any" /></td>
                </tr>
                <tr>
                    <td>Latitude2: </td>
                    <td><input type="number" id="Latitude2" name="Latitude2" step="any" /></td>
                </tr>
    
    
    
                <tr>
                    <td></td>
                    <td><input type="submit" value="Submit" /></td>
                </tr>
            </table>
            <hr />
            @ViewData["distance"]
    
            <h1>This calculates distance between two coordiantes Lat1,Lat2 to Long1, Long2 </h1>
        </form>
    </body>
    </html>
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.