# How to calculate distance from target coordinates to different coordinates [Answered]RSS

## 3 replies

Last post Dec 30, 2019 08:44 AM by samwu

• Learning Roc...

Member

8 Points

17 Posts

### How to calculate distance from target coordinates to different coordinates

Dec 27, 2019 05:22 AM|Learning Rocks|LINK

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#)

Learning Rocks

• samwu

Contributor

2890 Points

1213 Posts

### Re: How to calculate distance from target coordinates to different coordinates

Hi Learning Rocks,

#### 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#)

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") });
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:

Best regards,

Sam

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• Learning Roc...

Member

8 Points

17 Posts

### Re: How to calculate distance from target coordinates to different coordinates

Dec 27, 2019 09:21 AM|Learning Rocks|LINK

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

• samwu

Contributor

2890 Points

1213 Posts

### Re: How to calculate distance from target coordinates to different coordinates

Hi Learning Rocks,

#### 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.

```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

MSDN Community Support