locked
How to calculate distance from target coordinates to different coordinates RRS feed

  • Question

  • User339833461 posted

    Hello Everyone,
    I started learning asp.net/c# recently. i need some help to achieve my assignment. I want to show distance of each coordinates from target location in a table like below.

    Id            Target Coordinates(Lat & Long)                 Various Coordinates                Distance
    1001                  43.662352, -79.382951                        43.666293, -79.387973
    1002                  43.662352, -79.382951                        43.668271, -79.376153
    1003                  43.662352, -79.382951                        43.669737, -79.394470

    here how to calculate distance from Target coordinates to each coordinate by passing Id and showing in a table using asp.net web form(c#)

    Thanks in advance
    Learning Rocks

    Friday, December 27, 2019 5:22 AM

Answers

  • User288213138 posted

    Hi Learning Rocks,

    here how to calculate distance from Target coordinates to each coordinate by passing Id and showing in a table using asp.net web form(c#)

    According to your description, I couldn’t understand your requirement clearly.

    But i made demo for you, i put the data to GridView, then calculate distance in RowDataBound event().

    <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"></asp:GridView>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Id"), new DataColumn("Target Coordinates(Lat & Long)"), new DataColumn("Various Coordinates "), new DataColumn("Distance") });
                    dt.Rows.Add(1001, "43.662352, -79.382951", "43.666293, -79.387973");
                    dt.Rows.Add(1002, "43.662352, -79.382951", "43.668271, -79.376153");
                    dt.Rows.Add(1003, "43.662352, -79.382951", "43.669737, -79.394470");
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
    
                }
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                for (int rowIndex = 0; rowIndex < GridView1.Rows.Count; rowIndex++)
                {
                    GridViewRow gvRow = GridView1.Rows[rowIndex];
    
    
                    string[] Target = (gvRow.Cells[1].Text).Split(',');
                    string[] Various= (gvRow.Cells[2].Text).Split(',');
                    
    
                    string d1 = (Convert.ToDecimal(Target[0])- Convert.ToDecimal(Various[0])).ToString();
                    string d2 = (Convert.ToDecimal(Target[1]) - Convert.ToDecimal(Various[1])).ToString();
                    gvRow.Cells[3].Text = d1 +","+ d2;
                   
                }
            }

    The result:

    If I misunderstand your requirement, please post more details information about your requirement.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 27, 2019 8:50 AM
  • User288213138 posted

    Hi Learning Rocks,

    Here i need above result in grid with distance from fixed coordinates (43.662352, -79.382951)

    The GeoCoordinate class has GetDistanceTo() method to calculate the distance between the latitude and longitude coordinates.

    More information about GetDistanceTo method you can refer to this link:https://docs.microsoft.com/en-us/dotnet/api/system.device.location.geocoordinate.getdistanceto?redirectedfrom=MSDN&view=netframework-4.8#remarks

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                for (int rowIndex = 0; rowIndex < GridView1.Rows.Count; rowIndex++)
                {
                    GridViewRow gvRow = GridView1.Rows[rowIndex];
    
    
                    string[] Target = (gvRow.Cells[1].Text).Split(',');
                    string[] Various= (gvRow.Cells[2].Text).Split(',');
    
                    var d1 = new GeoCoordinate(Convert.ToDouble(Target[0]), Convert.ToDouble(Target[1]));
                    var d2 = new GeoCoordinate(Convert.ToDouble(Various[0]), Convert.ToDouble(Various[1]));           
                    gvRow.Cells[3].Text = d1.GetDistanceTo(d2).ToString();
    
                }
                 
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 30, 2019 8:44 AM

All replies

  • User288213138 posted

    Hi Learning Rocks,

    here how to calculate distance from Target coordinates to each coordinate by passing Id and showing in a table using asp.net web form(c#)

    According to your description, I couldn’t understand your requirement clearly.

    But i made demo for you, i put the data to GridView, then calculate distance in RowDataBound event().

    <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"></asp:GridView>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Id"), new DataColumn("Target Coordinates(Lat & Long)"), new DataColumn("Various Coordinates "), new DataColumn("Distance") });
                    dt.Rows.Add(1001, "43.662352, -79.382951", "43.666293, -79.387973");
                    dt.Rows.Add(1002, "43.662352, -79.382951", "43.668271, -79.376153");
                    dt.Rows.Add(1003, "43.662352, -79.382951", "43.669737, -79.394470");
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
    
                }
            }
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                for (int rowIndex = 0; rowIndex < GridView1.Rows.Count; rowIndex++)
                {
                    GridViewRow gvRow = GridView1.Rows[rowIndex];
    
    
                    string[] Target = (gvRow.Cells[1].Text).Split(',');
                    string[] Various= (gvRow.Cells[2].Text).Split(',');
                    
    
                    string d1 = (Convert.ToDecimal(Target[0])- Convert.ToDecimal(Various[0])).ToString();
                    string d2 = (Convert.ToDecimal(Target[1]) - Convert.ToDecimal(Various[1])).ToString();
                    gvRow.Cells[3].Text = d1 +","+ d2;
                   
                }
            }

    The result:

    If I misunderstand your requirement, please post more details information about your requirement.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 27, 2019 8:50 AM
  • User339833461 posted

    Thanx for the response. But i need the distance like how google maps giving for the given coordinates. 

    https://onedrive.live.com/?authkey=%21AG%5FPAIA2S%5FIX75A&cid=CABCF101F24A65C9&id=CABCF101F24A65C9%21106&parId=root&o=OneUp

    Here i need above result in grid with distance from fixed coordinates (43.662352, -79.382951)

    Thanks

    LR

    Friday, December 27, 2019 9:21 AM
  • User288213138 posted

    Hi Learning Rocks,

    Here i need above result in grid with distance from fixed coordinates (43.662352, -79.382951)

    The GeoCoordinate class has GetDistanceTo() method to calculate the distance between the latitude and longitude coordinates.

    More information about GetDistanceTo method you can refer to this link:https://docs.microsoft.com/en-us/dotnet/api/system.device.location.geocoordinate.getdistanceto?redirectedfrom=MSDN&view=netframework-4.8#remarks

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                for (int rowIndex = 0; rowIndex < GridView1.Rows.Count; rowIndex++)
                {
                    GridViewRow gvRow = GridView1.Rows[rowIndex];
    
    
                    string[] Target = (gvRow.Cells[1].Text).Split(',');
                    string[] Various= (gvRow.Cells[2].Text).Split(',');
    
                    var d1 = new GeoCoordinate(Convert.ToDouble(Target[0]), Convert.ToDouble(Target[1]));
                    var d2 = new GeoCoordinate(Convert.ToDouble(Various[0]), Convert.ToDouble(Various[1]));           
                    gvRow.Cells[3].Text = d1.GetDistanceTo(d2).ToString();
    
                }
                 
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 30, 2019 8:44 AM